DEV Community

Andy Piper for XDevelopers

Posted on • Edited on • Originally published at blog.twitter.com

Getting to the canonical URL for a Tweet

This tutorial was originally posted to the Twitter Developer Blog

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.

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, which is a service and method for doing just that.1

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, 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 (or, if you do have an app, you can also grab the HTML markup using magic directly from the oEmbed API endpoint, 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"

I've also answered a few questions about this over on Stack Overflow, in case you want to dig a bit deeper into alternative examples.

1 if you'd like to learn even more about Tweet IDs, check out the (long-since-retired!) original Snowflake code on GitHub, or look at the documentation on the developer site - and don't get tripped up by long integers in JavaScript!

Top comments (0)