# How do I convert an X (Twitter) username to its user ID (rest_id)?

Look the user up by handle and read the numeric ID from the result: X's official API does it with `GET /2/users/by/username/:username`, which needs a developer account, and in twscrape the maintainer pointed to `api.user_by_login("name")` and its `id` (issue 26, July 2023). XTikAPI's `x.accounts.detail` takes `screen_name` and returns the account with its ID, $0.002 a call; it also takes `rest_id` to go the other way.

Updated 2026-11-23

## Three ways

| Way | Call | Needs |
| --- | --- | --- |
| X API v2 | `GET /2/users/by/username/:username` | A developer app and bearer token; counts against your plan |
| twscrape | `user = await api.user_by_login("x")`, then `user.id` | Logged-in X accounts in a pool |
| XTikAPI | `x.accounts.detail` with `screen_name` | An API key |

The user ID never changes when someone renames their handle, so store the ID, not the name.

## Bulk conversion

1. Call `x.accounts.detail` once per handle with `screen_name` (no @).
2. Save the returned `id` with the handle.
3. Use the ID for later calls: `x.posts.list` takes `rest_id`, so a renamed account still resolves.

Cost: 5,000 handles × $0.002 = $10. A handle that doesn't exist fails and isn't charged.

## Why XTikAPI

- XTikAPI reads X with 16 endpoints from $0.002 a call, and a failed call is not charged.
- Fewer tokens for AI: across 10 recorded live X calls (2026-11-18), raw platform JSON is ~63k tokens vs ~21k via XTikAPI REST (66.7% fewer) and ~23k via MCP (63.7% fewer).
- See before you pay: 192 of 229 endpoints show a recorded real response on their page (10 of 16 for X).
- Spending you control: a failed call or a timeout is not charged (charged: false), X-Max-Price caps one call, and the same Idempotency-Key within 24 hours is never billed twice.
- Free trial: a verified account gets $0.50, no card.

## Endpoints

- [X account details API](https://xtikapi.com/apis/x/accounts/detail) ($0.002 a call): An X (Twitter) account profile.
- [X post list API](https://xtikapi.com/apis/x/posts/list) ($0.002 a call): An X account's posts, newest first.

## More about X

- [X API](https://xtikapi.com/apis/x)
- [Agent Reach Twitter/X channel stopped working: what changed?](https://xtikapi.com/answers/agent-reach-twitter-channel-broken)
- [twikit errors (Cloudflare 403, KEY_BYTE, login 399, rate limits, bans): how do I fix them?](https://xtikapi.com/answers/twikit-errors-and-fixes)
- [twscrape errors (404, No account available, XClIdParseError, ct0, bans): how do I fix them?](https://xtikapi.com/answers/twscrape-errors-and-fixes)
- [snscrape returns blocked (404) for Twitter: what still works?](https://xtikapi.com/answers/snscrape-twitter-not-working)
- [Is it legal to collect public social media data?](https://xtikapi.com/answers/is-scraping-public-social-media-data-legal)

## How to do it

- [How to get a user's posts from X (Twitter) with Python](https://xtikapi.com/guides/x-user-posts-python)

## Sources

What this page says about other products comes from their own pages and docs, read on 2026-11-23.

- [X API docs: user lookup by username](https://docs.x.com/x-api/users/get-user-by-username)
- [twscrape issue #26 (get ID from username)](https://github.com/vladkens/twscrape/issues/26)

## Questions

### Is rest_id the same as the user ID?

Yes: rest_id is the name X's web GraphQL uses for the numeric user ID.

---

XTikAPI: one API key for public social media data, prepaid per call. [HTML version](https://xtikapi.com/answers/x-username-to-user-id) · [llms.txt](https://xtikapi.com/llms.txt) · [OpenAPI](https://xtikapi.com/openapi.json)
