Skip to content

UTC

Guide

How X (Twitter) Snowflake IDs Work

A post ID looks like a random 19-digit number. It's actually a timestamp with a few extra bits bolted on, and you can read it with one line of code.

TimeTweets team · updated · 3 min read

Short answer

An X Snowflake ID is a 64-bit number: one unused sign bit, 41 bits of milliseconds since 4 November 2010 01:42:54.657 UTC, 10 bits identifying the machine and 12 bits of sequence; (ID >> 22) + 1288834974657 gives the Unix time in milliseconds.

Key takeaways

  • Every X post, reply, DM event and media item since November 2010 has a 64-bit Snowflake ID.
  • The top 41 bits (after the sign bit) are milliseconds since X's epoch, 1288834974657 in Unix time.
  • IDs grow with time, so sorting by ID is sorting by creation time, to the millisecond.
  • IDs are bigger than JavaScript's safe integer limit; always handle them as strings or BigInt.

What is a Snowflake ID?

A Snowflake ID is a 64-bit number that X generates for every new object, built so that it's unique across many servers and sorts by creation time. Twitter designed it in 2010 when a single database counter could no longer keep up. Each server could now produce IDs on its own, without asking a central counter, and the IDs would still be roughly in time order.

Which bits mean what?

Reading from the left, the 64 bits split into four fields.

Bits Width Holds
63 1 bit Always 0, so the number stays positive in signed 64-bit types
62 to 22 41 bits Milliseconds since X's epoch
21 to 12 10 bits Machine ID (often described as 5 bits data center, 5 bits worker)
11 to 0 12 bits Sequence: counts IDs a machine made within the same millisecond

41 bits of milliseconds cover about 69.7 years, so the scheme runs out around 2080. Twelve sequence bits let one machine produce 4,096 IDs per millisecond.

How do I decode one?

Shift right by 22, add 1288834974657, and treat the result as Unix milliseconds. Here's a worked example using an ID that appears in X's API documentation:

ID           1212092628029698048
ID >> 22     288985402114
+ epoch      1288834974657
= Unix ms    1577820376771   ->  2019-12-31 19:26:16.771 UTC

X's documentation lists created_at for that post as 2019-12-31T19:26:16.000Z, the same second. In JavaScript the whole thing is one line:

new Date(Number((BigInt(id) >> 22n) + 1288834974657n)).toISOString()

You can skip the arithmetic and paste the ID into the post time decoder.

Why do IDs break in JavaScript and spreadsheets?

They're larger than 2^53, the biggest whole number a JavaScript number or a spreadsheet cell stores exactly. A 19-digit ID quietly loses its last digits when parsed as a regular number: 1212092628029698048 can come back as 1212092628029698000. That's why Twitter's API added a string version of every ID, and why X's current API returns IDs as strings. In code, use strings or BigInt. In a spreadsheet, format the column as text before pasting.

What can the other bits tell you?

Very little that's useful outside X. The machine field tells you which ID generator made the number, and the sequence tells you how many IDs it had already made in that millisecond. They're interesting for curiosity, and they explain why two posts in the same millisecond still have different IDs. They don't reveal the author, the device or the location.

What about posts from before November 2010?

Those IDs came from a plain counter and carry no time at all. They run up to roughly 29.7 billion; the first Snowflake post IDs are just above that. If an ID is small, the only way to date it is from the post itself or from an archived copy. The decoder tells you when an ID is from that era rather than guessing.

Questions people ask

When did Twitter switch to Snowflake IDs?

Twitter announced the format in mid-2010 and post IDs switched in November 2010. X's epoch, the zero point of the timestamp, is 4 November 2010 at 01:42:54.657 UTC.

Are user IDs Snowflakes too?

Newer ones are. Accounts created in the early years have short sequential IDs, usually ten digits or fewer, which hold no timestamp. Long account IDs decode like post IDs and give the account's creation time.

Can I make up a valid-looking ID?

You can compute a number with a plausible timestamp, as the Date to post ID tool does, but that doesn't mean a post with that ID exists. Only X issues real ones.

Do other services use the same scheme?

Several use the same idea with their own epoch and bit split. Discord's IDs, for example, count from the start of 2015. A Discord ID decoded with X's epoch gives a wrong date, so always use the right epoch.

Sources

Pass it on X LinkedIn Reddit