# Twitter API get user ID from a username (X API v2)

> X's v2 lookup returns a user ID from a username but bills each read at $0.010. Ours does it for 1 credit, with no X account.

- URL: https://toolzerhub.com/blog/twitter-api-get-user-id
- Published: 2026-11-27
- Publisher: ToolzerHub (we sell the API this post describes)

## Short answer

- X API v2 returns a user's numeric ID from GET /2/users/by/username/:username, and /2/users/by takes up to 100 usernames per request.
- X lists User: Read at $0.010 per resource on its pay-per-use plan, and its pricing page shows no free tier.
- ToolzerHub's X profile route returns the numeric ID, name and followers for a handle at 1 credit a call, with 100 free credits a month and no X developer account.

To get a Twitter user ID from a username, call X API v2 `GET /2/users/by/username/:username` with a bearer token. The response includes the numeric `id`. It needs an X developer account with credits, and X lists a user read at $0.010 per resource. Our `/v1/x/profile` route takes a handle and returns the same ID for 1 credit, with no X account.

## What X's lookup endpoints do

X's [user lookup](https://docs.x.com/x-api/users/lookup/introduction) has two username endpoints: one for a single handle and `GET /2/users/by`, which takes up to 100 usernames in one request. X's example response shows the user's `id`, 2244994945 for XDevelopers, with the token in an `Authorization: Bearer` header. X's [pay-per-use pricing](https://docs.x.com/x-api/getting-started/pricing) lists User: Read at $0.010 per resource and shows no free tier, only $20 in credits after you save a payment card and make a first credit purchase. At that rate 1,000 users cost $10.

Use X's API when you already pay for it and want 100 usernames per request, or when you also post or read DMs.

## X user ID with ToolzerHub

We make ToolzerHub. Our [X API](https://toolzerhub.com/apis/x) returns public profiles, posts and follower lists as JSON with one API key and no X developer account. `/v1/x/profile` takes a handle without the `@` and costs 1 credit per handle.

```bash
curl "https://api.toolzerhub.com/v1/x/profile?handle=NASA" \
  -H "x-api-key: $TOOLZERHUB_API_KEY"
```

The ID and a few of the other fields in the response:

```json
{
  "data": {
    "id": "11348282",
    "handle": "NASA",
    "name": "NASA",
    "followers_count": 92078787,
    "is_blue_verified": true,
    "created_at": "2007-12-19T20:20:32.000Z"
  }
}
```

Handles can change, so store the ID rather than the handle. To read the account next, see [Twitter API get user tweets](https://toolzerhub.com/blog/twitter-api-get-user-tweets) and the [Twitter API followers list](https://toolzerhub.com/blog/twitter-api-followers-list); for what X charges for posts and followers, see [is the Twitter API free](https://toolzerhub.com/blog/is-twitter-api-free).

## Look up a list of handles

The script takes handles, `@handles` or profile URLs, separated by commas, and writes a CSV with the ID, name, follower count and creation date of each.

```python
import csv
import os
import sys
import time

import requests

API = "https://api.toolzerhub.com/v1/x/profile"
HEADERS = {"x-api-key": os.environ.get("TOOLZERHUB_API_KEY", "")}
FIELDS = ["input", "handle", "user_id", "name", "followers", "verified", "created"]


def cell(value):
    # A leading = + - or @ would run as a formula when the CSV opens in a spreadsheet.
    return "'" + value if isinstance(value, str) and value[:1] in ("=", "+", "-", "@") else value


def lookup(name, tries=4):
    # Accepts NASA, @NASA or https://x.com/NASA.
    handle = name.strip().split("?")[0].split("#")[0].rstrip("/").rsplit("/", 1)[-1].lstrip("@")
    for attempt in range(tries):
        r = requests.get(API, params={"handle": handle}, headers=HEADERS, timeout=90)
        if r.status_code in (429, 503) and attempt < tries - 1:
            time.sleep(5 * (attempt + 1))
            continue
        if r.status_code == 400:  # not a valid handle
            return None
        r.raise_for_status()
        return r.json()["data"]  # None when no account has that handle


def main(names, out="x_user_ids.csv"):
    found = 0
    with open(out, "w", newline="", encoding="utf-8") as f:
        writer = csv.DictWriter(f, fieldnames=FIELDS)
        writer.writeheader()
        for name in [n for n in names.split(",") if n.strip()]:
            p = lookup(name) or {}
            found += bool(p)
            writer.writerow({
                "input": cell(name.strip()), "handle": p.get("handle"), "user_id": p.get("id"),
                "name": cell(p.get("name")), "followers": p.get("followers_count"),
                "verified": p.get("is_blue_verified"), "created": p.get("created_at"),
            })
            print(name.strip(), "->", p.get("id") or "not found")
    print(f"found {found} user IDs, saved to {out}")


if __name__ == "__main__":
    if not HEADERS["x-api-key"] or len(sys.argv) < 2:
        sys.exit("usage: TOOLZERHUB_API_KEY=... python x_user_ids.py NASA,@jack,https://x.com/github")
    main(*sys.argv[1:])
```

```bash
export TOOLZERHUB_API_KEY=your_key
python x_user_ids.py "NASA,@jack,https://x.com/github,Stripe,TwitterDev"
```

## What five handles returned

Five inputs gave four IDs in five calls. The fifth, `TwitterDev`, returned no account (`data: null`); the handle was renamed, and XDevelopers is the current one.

| Handle | User ID |
|---|---:|
| NASA | 11,348,282 |
| jack | 12 |
| github | 13,334,762 |
| stripe | 102,812,444 |

`XDevelopers` returns 2244994945, the ID in X's lookup docs. Older accounts have short IDs: jack's is 12.

## 1,000 lookups cost $1.90

ToolzerHub's X API charges 1 credit per handle, so 1,000 lookups are 1,000 credits, about $1.90. X's pay-per-use page lists User: Read at $0.010 per resource, which puts 1,000 user resources at $10. The first 100 credits each month are free, a $19 pack adds 10,000 that never expire, and [bigger plans](https://toolzerhub.com/pricing) cover long handle lists. Run the script on the first five handles in your own list.

We are not affiliated with X Corp.
