Skip to content

Instantly share code, notes, and snippets.

@kentbrew
Last active March 27, 2025 02:44
Show Gist options
  • Save kentbrew/8942accb5c584f11a775af02d097dd40 to your computer and use it in GitHub Desktop.
Save kentbrew/8942accb5c584f11a775af02d097dd40 to your computer and use it in GitHub Desktop.
Finding Twitter User IDs

Finding Twitter User IDs

User accounts on Twitter are commonly identified by screen name, which may be changed by operators when they take over an account, or have been sitting on an old account for a long time and want to transition it into malicious use.

User IDs, however, are permanent. There are several services out there that will try to find them for you but it seems like a bad idea to me, since you're alerting them to the fact that there's something interesting about this account. There's also plenty of bad advice that uses many long-since-abandoned Twitter API endpoints.

As of this writing (2018-02-18) you can view source and search for /profile_banners/, which will show something like this:

.enhanced-mini-profile .mini-profile .profile-summary {
  background-image: url(https://pbs.twimg.com/profile_banners/2622731/1401943819/mobile);
}

The number after profile_banners/ is the account's Twitter user ID, which won't change if the account's screen name is changed. Here, 2622731 is my personal Twitter ID. (Expect to see much later numbers for more recent accounts; mine is quite old.)

Deleted Accounts?

If someone has just deleted their account it's often possible to find it in Google's most recent cache. Try searching for this:

twitter thatRecentlyDeletedUserName

Click the little down-arrow in the green URL, just below the blue headline, and then choose Cached. If it works, you'll see a page from http://webcache.googleusercontent.com/ with a View Source link somewhere near the top. Search as shown above.

ID to User Name?

To translate an ID to the current user name, visit the Intents url. Here's mine:

https://twitter.com/intent/user?user_id=2622731

This will show my current Twitter user name, which is @kentbrew. You should follow me, of course. :)

@huangjunjie-cs
Copy link

Another url for ID to user name can be https://twitter.com/i/user/2622731.

@github-account1111
Copy link

The profile_banners bit only shows up in the Inspect menu in the browser, not in the actual page source, meaning you have to dig through Dev Tools manually every time.
No way to script a solution.

@kentbrew
Copy link
Author

kentbrew commented Apr 30, 2023

Haven't looked at this in a while. It looks like one of document.scripts has a type of application/ld+json. If you try this on a profile page:

id = JSON.parse(
document.evaluate(
    '//script[@type="application/ld+json"]', 
    document.lastChild, 
    null,
    XPathResult.ANY_TYPE, null).iterateNext().textContent
).author.identifier;

... you ought to get the user ID.

@github-account1111
Copy link

github-account1111 commented May 8, 2023

I wasn't sure how to translate that snippet to Powershell.
I've also tried Twitter's api, all to no avail (apparently it's broken right now).

What did end up working though was gallery-dl:

$Url = $args[0]
$Output = gallery-dl --list-keywords $Url | Select-String -Pattern "user\['id'\]" -Context 1
$Output[0].Context.DisplayPostContext

@Sardonyx001
Copy link

It's possible to get your id from your cookies!
X/Twitter has a cookie field called "twid" which is always u%3D<TWITTERID>, i.e. the URL formatted version of u=<TWITTERID>.
I found something like the following when executed in the console can quickly return your id

((await window.cookieStore.get("twid")).value).slice(4)

@jpluimers
Copy link

Haven't looked at this in a while. It looks like one of document.scripts has a type of application/ld+json. If you try this on a profile page:

Their JSON structures change faster than their URL structures.

At the moment this works:

id = JSON.parse(
document.evaluate(
    '//script[@type="application/ld+json"]', 
    document.lastChild, 
    null,
    XPathResult.ANY_TYPE, null).iterateNext().textContent
).mainEntity.identifier;

This has been working for a much longer time:

id = document.querySelector('img[src*="https://pbs.twimg.com/profile_banners/"]').src.split('https://pbs.twimg.com/profile_banners/')[1].split('/')[0]

Tested on https://twitter.com/i/user/12 and https://twitter.com/jack (and their client-side redirects), the intermediate src returns https://pbs.twimg.com/profile_banners/12/1742427520/600x200 where 12 is the Twitter user ID.

You can also run this from a profile header URL like https://x.com/jack/header_photo where the intermediate src is https://pbs.twimg.com/profile_banners/12/1742427520/1500x500

And @Sardonyx001 : twid does not return 12 here but 39508140 which is my own Twitter user ID instead of the currently displayed user profile.

@Sardonyx001
Copy link

@jpluimers Yes it's saved in your browser cookies so it returns your own ID.
Thank you for your solution it works with any account!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment