Getting to the canonical URL for a Tweet
Published: 2020-06-10 17:56:47

*This tutorial was originally posted to the [Twitter Developer Blog](https://blog.twitter.com/developer/en_us/topics/tips/2020/getting-to-the-canonical-url-for-a-tweet.html)*

A surprisingly-common question I hear from developers, bloggers and researchers using Twitter is, *"how can I get the original URL for a Tweet"*?

You've probably seen those long numbers in the URLs in your web browser when you click into a Tweet's detail page. Those are `Tweet IDs`. Every one of them is unique. When Twitter started out, the system was sufficiently small that it was easy enough to just have a low, incrementing number. Fun fact! `@jack's` very first Tweet has `id=20`.

{% twitter 0000000000000020 %}

Over time, and as the platform scaled to require multiple and parallel backend services, a system that consistently generated unique IDs across servers and datastores was needed - so, Twitter [developed Snowflake](https://blog.twitter.com/engineering/en_us/a/2010/announcing-snowflake.html), which is a service and method for doing just that.<sup><a href="#note1">1</a></sup>

That's how we end up with those long ID numbers - but what if you only have a Tweet ID, and you want to see the original Tweet on the website? You don't know which Twitter user originally Tweeted it, so you don't have the username, and can't make a URL from it easily...

Well, if the Tweet is available (that means, it has not been deleted and is not a protected Tweet belonging to an account you do not follow), there's a trick that you can use here.

Let's pick a recent Tweet with ID `1234903157580296192`, for example.

The long way to do this would be to use the [official Twitter API](https://developer.twitter.com/), and pull back the Tweet object and associated user information. Then, we'd pick out the screen name of the user that posted the Tweet from the JSON response, and do something like this, in pseudo-code:

`URL = "https://twitter.com/[screen name]/status/[Tweet ID]"`

*"... oh! But I don't have a Twitter Developer Account / I don't want to build an app just to do this simple thing"*

No problem! There's a handy trick, and *you can even use it to generate embedded Tweets* via markup from [publish.twitter.com](https://publish.twitter.com) (or, if you do have an app, you can also grab the HTML markup ~~using magic~~ directly from [the oEmbed API endpoint](https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/get-statuses-oembed), without ever having to know the user's original screen name)

Here's the secret... 

Each Tweet ID is already unique, so you don't need to know the user that posted it! It turns out, you can add any username to the URL, and Twitter will work it out for you. I always like to use `twitter` itself, because that one is likely to be around for a while.

So now, with our previous example:

`URL = "https://twitter.com/twitter/status/1234903157580296192"`

{% twitter 1234903157580296192 %}

I've also [answered a few questions about this over on Stack Overflow](https://stackoverflow.com/questions/41090108/how-to-embed-a-tweet-on-a-page-if-i-only-know-its-id/41103552#41103552), in case you want to dig a bit deeper into alternative examples.

<p id="note1"><sup>1</sup>  if you'd like to learn even more about Tweet IDs, check out the (long-since-retired!) <a href="https://github.com/twitter-archive/snowflake/releases/tag/snowflake-2010">original Snowflake code on GitHub</a>, or look at the <a href="https://developer.twitter.com/en/docs/basics/twitter-ids">documentation on the developer site</a> - and don't get tripped up by long integers in JavaScript!</p>