Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

XDevelopers profile imageAndy Piper
Andy Piper forXDevelopers

Posted on • Edited on • Originally published atblog.twitter.com

     

Getting to the canonical URL for a Tweet

This tutorial was originally posted to theTwitter 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 areTweet 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 hasid=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, Twitterdeveloped 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 ID1234903157580296192, for example.

The long way to do this would be to use theofficial 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, andyou can even use it to generate embedded Tweets via markup frompublish.twitter.com (or, if you do have an app, you can also grab the HTML markupusing magic directly fromthe 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 usetwitter 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 alsoanswered 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 thedocumentation on the developer site - and don't get tripped up by long integers in JavaScript!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Build What's Next

Questions abouthow to use our APIs, orwant to learn more?

Come join the discussion with other developers using the X platform, andshare your own knowledge!

More fromXDevelopers

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp