| name | X |
| description | Use when building applications that interact with X (formerly Twitter) data. Reach for this skill when agents need to search posts, stream real-time data, manage user relationships, handle authentication, work with API fundamentals like rate limits and pagination, or integrate X data into applications. |
| metadata | {"mintlify-proj":"x","version":"1.0"} |
X API Skill Reference
Product summary
The X API is a REST and streaming API for accessing X's public data: posts, users, trends, spaces, direct messages, and more. Agents use it to search historical or recent posts, stream real-time data matching filter rules, look up user profiles, manage relationships (follows, likes, bookmarks), and build applications powered by X data.
Key files and endpoints:
- Base URL:
https://api.x.com/2
- Authentication: Bearer Token (app-only), OAuth 1.0a (user context), OAuth 2.0, or Basic Auth (enterprise)
- Developer Console: https://console.x.com (manage apps, credentials, billing)
- Official SDKs: Python (
xdk) and TypeScript (@xdevplatform/xdk)
- Primary docs: https://docs.x.com/x-api/introduction
When to use
Reach for this skill when:
- Searching posts โ Find posts by keyword, hashtag, user, engagement metrics, or date range (recent 7 days or full archive)
- Streaming real-time data โ Set up filtered stream rules to receive matching posts as they're published
- Looking up users or posts โ Retrieve user profiles, post details, or relationships by ID or username
- Managing user actions โ Create/delete posts, like, repost, bookmark, follow, mute, or block
- Handling authentication โ Choose the right auth method (Bearer Token for app-only, OAuth for user context)
- Understanding API fundamentals โ Rate limits, pagination, fields, expansions, error handling
- Building with SDKs โ Use Python or TypeScript SDKs to avoid manual authentication and pagination
- Monitoring usage and costs โ Track API calls and credits in the Developer Console
Quick reference
Authentication methods
| Method | Use case | Credentials |
|---|
| Bearer Token | App-only, public data | API Key + API Secret โ Bearer Token |
| OAuth 1.0a | User context, private data | API Key, Secret, Access Token, Token Secret |
| OAuth 2.0 | User context with PKCE | Client ID, Client Secret, Authorization Code |
| Basic Auth | Enterprise APIs only | Email + Password (HTTP Basic header) |
Get credentials from Developer Console โ Your App โ Keys and Tokens.
Common endpoints
| Task | Endpoint | Method |
|---|
| Search recent posts | /2/tweets/search/recent | GET |
| Search full archive | /2/tweets/search/all | GET |
| Get post by ID | /2/tweets/{id} | GET |
| Create post | /2/tweets | POST |
| Delete post | /2/tweets/{id} | DELETE |
| Get user by username | /2/users/by/username/{username} | GET |
| Get user by ID | /2/users/{id} | GET |
| Follow user | /2/users/{id}/following | POST |
| Get followers | /2/users/{id}/followers | GET |
| Filtered stream rules | /2/tweets/search/stream/rules | GET/POST |
| Connect to stream | /2/tweets/search/stream | GET |
Field and expansion parameters
Fields โ Request additional data for each object type:
?tweet.fields=created_at,public_metrics,lang
?user.fields=description,public_metrics,verified
?media.fields=url,alt_text,type
Expansions โ Include related objects (author, media, referenced posts):
?expansions=author_id,attachments.media_keys,referenced_tweets.id
Combine both to get full context in one request.
Rate limit headers
Every response includes:
x-rate-limit-limit: 900
x-rate-limit-remaining: 847
x-rate-limit-reset: 1705420800
Check x-rate-limit-remaining before making requests. When you hit a limit (429), wait until x-rate-limit-reset (Unix timestamp).
Pagination
Use pagination_token from response to fetch next page:
?max_results=100&pagination_token=NEXT_TOKEN
SDKs handle pagination automatically with iterators.
Decision guidance
When to use Bearer Token vs OAuth
| Scenario | Use Bearer Token | Use OAuth 1.0a/2.0 |
|---|
| Access public data only | โ | โ |
| Access user's private posts/bookmarks | โ | โ |
| Post on behalf of user | โ | โ |
| Build a web app with login | โ | โ (OAuth 2.0) |
| Desktop/CLI tool | โ | โ (OAuth 1.0a) |
| Server-to-server automation | โ | โ |
When to use search vs filtered stream
| Need | Use search | Use filtered stream |
|---|
| Historical data (past 7 days or archive) | โ | โ |
| Real-time matching posts | โ | โ |
| One-time lookup | โ | โ |
| Continuous monitoring | โ | โ |
| Complex queries | โ | โ |
| Persistent connection acceptable | โ | โ |
When to use fields vs expansions
| Goal | Use fields | Use expansions |
|---|
| Get more data on primary object | โ | โ |
| Include related objects (author, media) | โ | โ |
| Reduce API calls | โ | โ |
| Get specific metrics (likes, reposts) | โ | โ |
Workflow
1. Set up authentication
- Go to https://console.x.com and sign in with X account
- Create a new App (or use existing)
- Navigate to Keys and Tokens tab
- Copy Bearer Token (for app-only) or API Key/Secret (for OAuth)
- Store securely in environment variable:
export BEARER_TOKEN="your_token"
2. Make your first request
curl "https://api.x.com/2/users/by/username/xdevelopers" \
-H "Authorization: Bearer $BEARER_TOKEN"
Expected response:
{
"data": {
"id": "2244994945",
"name": "X Developers",
"username": "xdevelopers"
}
}
3. Request additional fields
Add field parameters to get more data:
curl "https://api.x.com/2/users/by/username/xdevelopers?user.fields=created_at,description,public_metrics" \
-H "Authorization: Bearer $BEARER_TOKEN"
4. Include related objects with expansions
Combine expansions and fields to get full context:
curl "https://api.x.com/2/tweets/1234567890?expansions=author_id&user.fields=username,name" \
-H "Authorization: Bearer $BEARER_TOKEN"
Response includes both the post and author in includes.users.
5. Handle pagination
For endpoints returning multiple results:
curl "https://api.x.com/2/tweets/search/recent?query=api&max_results=10&pagination_token=NEXT_TOKEN" \
-H "Authorization: Bearer $BEARER_TOKEN"
6. Check rate limits
Before making requests, check remaining quota:
x-rate-limit-remaining: 847
If approaching zero, wait until x-rate-limit-reset before retrying.
7. Use SDKs for production
Instead of manual cURL, use official SDKs:
Python:
from xdk import Client
client = Client(bearer_token="YOUR_TOKEN")
user = client.users.get_by_username("xdevelopers")
print(user.data.username)
TypeScript:
import { Client } from '@xdevplatform/xdk';
const client = new Client({ bearerToken: 'YOUR_TOKEN' });
const user = await client.users.getByUsername('xdevelopers');
console.log(user.data?.username);
Common gotchas
-
Bearer Token not working for user context โ Bearer Token only accesses public data. For private posts, bookmarks, or actions on behalf of a user, use OAuth 1.0a or OAuth 2.0.
-
Missing fields in response โ By default, endpoints return minimal fields (e.g., post returns only id, text, edit_history_tweet_ids). Always add tweet.fields, user.fields, etc. to get additional data.
-
Forgetting expansions โ If you request author_id but don't add expansions=author_id, the author object won't be included. Expansions are required to fetch related objects.
-
Rate limit surprises โ Each endpoint has its own rate limit. Check the endpoint's documentation for the specific limit (e.g., 450/15min vs 900/15min). Monitor x-rate-limit-remaining header.
-
429 errors without waiting โ When you hit a rate limit, the response includes x-rate-limit-reset (Unix timestamp). Don't retry immediately; wait until that time.
-
Streaming connection drops โ Filtered stream sends keep-alive signals (blank lines) every 20 seconds. If you don't receive data or keep-alive for 20 seconds, reconnect. Implement exponential backoff.
-
Search query syntax errors โ Search operators are case-sensitive and require proper escaping. Use quotes for phrases: "machine learning" not machine learning. Test queries in the API reference first.
-
Partial errors in batch requests โ When looking up multiple posts/users, some may fail while others succeed. Check the errors array even in 200 responses.
-
Protected accounts โ Posts from protected accounts are only visible if you're authorized. Unauthenticated requests won't see them.
-
Deleted posts return 404 โ Don't assume a post exists just because you have its ID. Always handle 404 responses.
-
Credentials in code โ Never hardcode API keys or tokens. Use environment variables or secure vaults. If exposed, regenerate immediately in the Developer Console.
Verification checklist
Before submitting work with the X API:
Resources
Comprehensive navigation: https://docs.x.com/llms.txt
Critical documentation pages:
- X API Introduction โ Overview of all endpoints and features
- Make Your First Request โ Quickstart with cURL examples
- Authentication Overview โ All auth methods explained
- Rate Limits โ Per-endpoint limits and headers
- Fields & Expansions โ Customize response data
- Search Posts โ Recent and full-archive search
- Filtered Stream โ Real-time streaming
- Response Codes & Errors โ Error handling
- Official SDKs โ Python and TypeScript libraries
- Developer Console โ Manage apps and credentials
For additional documentation and navigation, see: https://docs.x.com/llms.txt