Перейти к содержимому

Snowflakes

A snowflake is the 64-bit ID of a Squad resource, such as a user, guild, channel, message or role. The HTTP API, the Admin API and the Gateway all use the same IDs, and an ID is unique only within the instance that issued it.

A snowflake packs a timestamp, a worker ID and a sequence into 64 bits. Bit 63 is always 0.

FieldBitsDescription
Timestamp62 to 22Milliseconds since the Squad epoch, 2015-01-01T00:00:00.000Z
Worker ID21 to 12The worker that issued the ID, from 0 through 1023
Sequence11 to 0A per-worker counter, from 0 through 4095

Every instance uses the same epoch. Don’t rely on the worker ID or sequence.

To get the creation time in Unix milliseconds, shift the snowflake right by 22 bits and add the epoch. To build a cursor for a given time, reverse that to get its smallest snowflake.

timestamp_ms = (snowflake >> 22) + 1420070400000
snowflake = (timestamp_ms - 1420070400000) << 22

Snowflakes cover 2015-01-01T00:00:00.000Z through 2084-09-06T15:47:35.551Z.

A thread started from a message has that message’s ID, so its timestamp is the message’s. The thread’s own creation time is create_timestamp in its thread metadata.

Send snowflakes as strings too. The HTTP API and Gateway also take a JSON integer.

SurfaceAccepts
HTTP requestA string or JSON integer from 0 through 9223372036854775807, with no leading zero
Gateway commandThe same, except 0 is rejected

The HTTP API reports a malformed value as INVALID_SNOWFLAKE_FORMAT and a value that is too large as SNOWFLAKE_OUT_OF_RANGE.

Snowflakes sorted as integers are in creation order, and list endpoints use them as cursors. You can also build a cursor from a timestamp.

CursorSelects
beforeIDs lower than the cursor
afterIDs higher than the cursor
aroundA window centred on the cursor, including it

Only List channel messages and List messages from multiple channels accept around. Each endpoint lists the cursors it accepts, and a few use cursors that aren’t snowflakes.