Authentication
All X API v2 endpoints require authentication. Choose the method that fits your use case:| Method | Best for | Can access private metrics? |
|---|---|---|
| OAuth 2.0 App-Only | Server-to-server, public data | No |
| OAuth 2.0 Authorization Code with PKCE | User-facing apps | Yes (for authorized user’s data) |
| OAuth 1.0a User Context | Legacy integrations | Yes (for authorized user’s data) |
App-Only authentication
For public user data, use a Bearer Token:cURL
User Context authentication
Required for the authenticated user endpoint (/2/users/me):
cURL
Fields and expansions
The X API v2 returns minimal data by default. Usefields and expansions to request exactly what you need.
Default response
Available fields
user.fields
user.fields
| Field | Description |
|---|---|
created_at | Account creation timestamp |
description | User bio |
entities | Parsed URLs in bio |
location | User-defined location |
pinned_tweet_id | Pinned Post ID |
profile_image_url | Avatar URL |
protected | Whether account is protected |
public_metrics | Follower/following counts |
url | Website URL |
verified | Verification status |
withheld | Withholding information |
tweet.fields (requires pinned_tweet_id expansion)
tweet.fields (requires pinned_tweet_id expansion)
| Field | Description |
|---|---|
created_at | Post creation timestamp |
text | Post content |
public_metrics | Engagement counts |
entities | Hashtags, mentions, URLs |
Example with fields
cURL
Response with expansions
Fields and expansions guide
Learn more about customizing responses
Batch lookups
Look up multiple users in a single request:cURL (by IDs)
Error handling
Common errors
| Status | Error | Solution |
|---|---|---|
| 400 | Invalid request | Check parameter formatting |
| 401 | Unauthorized | Verify authentication credentials |
| 403 | Forbidden | Check App permissions |
| 404 | Not Found | User doesn’t exist or was suspended |
| 429 | Too Many Requests | Wait and retry (see rate limits) |
Suspended or deleted users
If a user is suspended or deleted:- Single user lookup returns
404 - Multi-user lookup omits the user from results with an
errorsarray
Protected users
For protected accounts you don’t follow:- Basic info (id, name, username) is available
- Protected content (pinned Post) may be restricted
protected: trueindicates the account status
Best practices
Batch requests
Use multi-user endpoints to fetch up to 100 users at once, reducing API calls.
Request only needed fields
Specify only the fields you need to minimize response size.
Cache user data
Cache user profiles locally to reduce repeated requests.
Handle errors gracefully
Check for partial errors in batch responses.