X API Skill
Product summary
The X API provides programmatic access to X's public conversation through modern REST endpoints. Agents use it to read posts, publish content, manage users, search historical data, and stream near real-time posts. Key files: Developer Console credentials (API keys, Bearer Token, OAuth tokens), API endpoints at https://api.x.com/2/, authentication headers. Primary docs: https://docs.x.com/x-api/introduction
When to use
Reach for this skill when:
- Building applications that need to search, retrieve, or publish posts
- Streaming near real-time posts matching specific criteria (filtered stream)
- Looking up user profiles, managing follows, blocks, or mutes
- Accessing trends, spaces, direct messages, or lists
- Analyzing engagement metrics or post analytics
- Integrating X data into dashboards, research tools, or monitoring systems
- User requests mention "X API", "Twitter API", "posts", "tweets", "users", or "streaming"
Quick reference
Authentication methods
| Method |
Use case |
Credentials |
| Bearer Token (App-only) |
Public data, no user context |
API Key + Secret → Bearer Token |
| OAuth 1.0a |
User-context requests, signing requests |
API Key, Secret, Access Token, Token Secret |
| OAuth 2.0 |
Modern user-context, fine-grained scopes |
Client ID, Client Secret, Authorization Code |
| Basic Auth |
Enterprise APIs only |
Email + password (HTTPS only) |
Essential endpoints
| Resource |
Endpoint |
Method |
Purpose |
| User lookup |
/2/users/by/username/{username} |
GET |
Get user by username |
| Post lookup |
/2/tweets/{id} |
GET |
Get post by ID |
| Recent search |
/2/tweets/search/recent |
GET |
Search last 7 days |
| Full-archive search |
/2/tweets/search/all |
GET |
Search all posts (paid) |
| Filtered stream |
/2/tweets/search/stream |
GET |
Stream matching posts |
| Stream rules |
/2/tweets/search/stream/rules |
POST/GET |
Manage stream filters |
| Create post |
/2/tweets |
POST |
Publish a post |
| Delete post |
/2/tweets/{id} |
DELETE |
Remove a post |
Field parameters
Request additional data with field parameters:
# Posts
?tweet.fields=created_at,public_metrics,lang,author_id
# Users
?user.fields=created_at,description,public_metrics,verified
# Media
?media.fields=url,preview_image_url,alt_text,public_metrics
Rate limit headers
Every response includes:
x-rate-limit-limit — Max requests in window
x-rate-limit-remaining — Requests left
x-rate-limit-reset — Unix timestamp when window resets
Decision guidance
When to use Bearer Token vs OAuth
| Scenario |
Use Bearer Token |
Use OAuth |
| Public data only |
✓ |
|
| Need user context |
|
✓ |
| App-only requests |
✓ |
|
| User authorization required |
|
✓ |
| Simpler setup |
✓ |
|
| Fine-grained scopes needed |
|
✓ |
When to use search vs filtered stream
| Need |
Use search |
Use filtered stream |
| Historical data |
✓ |
|
| Real-time delivery |
|
✓ |
| One-time query |
✓ |
|
| Continuous monitoring |
|
✓ |
| Full archive access |
✓ (paid) |
|
| Lower latency |
|
✓ |
| Complex queries |
✓ |
✓ |
When to use SDKs vs direct HTTP
| Scenario |
Use SDK |
Use cURL/HTTP |
| Production code |
✓ |
|
| Quick testing |
|
✓ |
| Type safety needed |
✓ (TypeScript) |
|
| Python async |
✓ |
|
| Simple one-off request |
|
✓ |
| Automatic pagination |
✓ |
|
Workflow
Verify credentials exist
- Check Developer Console at console.x.com
- Confirm app has Bearer Token or OAuth tokens
- Verify app has access to required endpoints
Choose authentication method
- Public data only → Bearer Token (simplest)
- User-context needed → OAuth 1.0a or 2.0
- Enterprise APIs → Basic Auth
Build the request
- Select endpoint from API reference
- Add required parameters (query, user ID, etc.)
- Add field parameters to request specific data
- Add expansions for related objects
- Set Authorization header with credentials
Handle the response
- Check HTTP status code (200 = success, 4xx/5xx = error)
- Parse JSON response; primary data in
data field
- Check for
errors array even in 200 responses (partial errors)
- Extract rate limit headers for monitoring
Implement error handling
- 401: Check authentication credentials
- 403: Verify app has endpoint access
- 429: Implement exponential backoff, check
x-rate-limit-reset
- 400: Validate request syntax and parameters
For streaming
- Create rules with operators (keywords, hashtags, users)
- POST rules to
/2/tweets/search/stream/rules
- GET
/2/tweets/search/stream to connect
- Handle keep-alive signals (blank lines every 20 seconds)
- Implement reconnection logic for disconnects
Common gotchas
- Bearer Token vs OAuth confusion: Bearer Token is app-only (public data). OAuth is for user-context. Don't mix them up.
- Missing fields in response: Fields are opt-in. Default responses are minimal. Always add
?tweet.fields= or ?user.fields= for the data you need.
- Rate limits reset every 15 minutes: Not per hour. Check
x-rate-limit-reset header, not clock time.
- Stream requires at least one rule: Connecting to
/2/tweets/search/stream without rules returns 409 Conflict.
- Callback URLs must match exactly: Trailing slashes, protocol, and case matter. Use
http://127.0.0.1 for local dev, not localhost.
- Partial errors in 200 responses: When requesting multiple resources, some may fail. Check the
errors array even if status is 200.
- Search query syntax is strict: Operators like
from:, has:images, lang: are case-sensitive. Quotes required for phrases.
- Deleted posts return 404: Don't retry; the post is gone.
- Protected accounts' posts need authorization: Can't access with Bearer Token alone.
- Stream disconnects after 20 seconds of silence: If no data or keep-alive received, reconnect immediately.
- Expansions require field parameters: Adding
expansions=author_id doesn't return author data without user.fields=.
Verification checklist
Before submitting work with X API:
Resources
Comprehensive navigation: https://docs.x.com/llms.txt
Critical pages:
- X API Introduction — Overview of all endpoints and features
- Make Your First Request — Quick start with cURL examples
- Authentication Overview — All auth methods explained
For additional documentation and navigation, see: https://docs.x.com/llms.txt
1---2name: x-api3description: Use when building applications that access X's public data, including posts, users, trends, spaces, and direct messages. Reach for this skill when integrating X data into applications, building search or streaming solutions, managing user interactions, or analyzing X platform data.4---56# X API Skill78## Product summary910The X API provides programmatic access to X's public conversation through modern REST endpoints. Agents use it to read posts, publish content, manage users, search historical data, and stream near real-time posts. Key files: Developer Console credentials (API keys, Bearer Token, OAuth tokens), API endpoints at `https://api.x.com/2/`, authentication headers. Primary docs: https://docs.x.com/x-api/introduction1112## When to use1314Reach for this skill when:15- Building applications that need to search, retrieve, or publish posts16- Streaming near real-time posts matching specific criteria (filtered stream)17- Looking up user profiles, managing follows, blocks, or mutes18- Accessing trends, spaces, direct messages, or lists19- Analyzing engagement metrics or post analytics20- Integrating X data into dashboards, research tools, or monitoring systems21- User requests mention "X API", "Twitter API", "posts", "tweets", "users", or "streaming"2223## Quick reference2425### Authentication methods2627| Method | Use case | Credentials |28|:-------|:---------|:------------|29| **Bearer Token (App-only)** | Public data, no user context | API Key + Secret → Bearer Token |30| **OAuth 1.0a** | User-context requests, signing requests | API Key, Secret, Access Token, Token Secret |31| **OAuth 2.0** | Modern user-context, fine-grained scopes | Client ID, Client Secret, Authorization Code |32| **Basic Auth** | Enterprise APIs only | Email + password (HTTPS only) |3334### Essential endpoints3536| Resource | Endpoint | Method | Purpose |37|:---------|:---------|:-------|:--------|38| User lookup | `/2/users/by/username/{username}` | GET | Get user by username |39| Post lookup | `/2/tweets/{id}` | GET | Get post by ID |40| Recent search | `/2/tweets/search/recent` | GET | Search last 7 days |41| Full-archive search | `/2/tweets/search/all` | GET | Search all posts (paid) |42| Filtered stream | `/2/tweets/search/stream` | GET | Stream matching posts |43| Stream rules | `/2/tweets/search/stream/rules` | POST/GET | Manage stream filters |44| Create post | `/2/tweets` | POST | Publish a post |45| Delete post | `/2/tweets/{id}` | DELETE | Remove a post |4647### Field parameters4849Request additional data with field parameters:5051```bash52# Posts53?tweet.fields=created_at,public_metrics,lang,author_id5455# Users56?user.fields=created_at,description,public_metrics,verified5758# Media59?media.fields=url,preview_image_url,alt_text,public_metrics60```6162### Rate limit headers6364Every response includes:65- `x-rate-limit-limit` — Max requests in window66- `x-rate-limit-remaining` — Requests left67- `x-rate-limit-reset` — Unix timestamp when window resets6869## Decision guidance7071### When to use Bearer Token vs OAuth7273| Scenario | Use Bearer Token | Use OAuth |74|:---------|:-----------------|:----------|75| Public data only | ✓ | |76| Need user context | | ✓ |77| App-only requests | ✓ | |78| User authorization required | | ✓ |79| Simpler setup | ✓ | |80| Fine-grained scopes needed | | ✓ |8182### When to use search vs filtered stream8384| Need | Use search | Use filtered stream |85|:-----|:-----------|:-------------------|86| Historical data | ✓ | |87| Real-time delivery | | ✓ |88| One-time query | ✓ | |89| Continuous monitoring | | ✓ |90| Full archive access | ✓ (paid) | |91| Lower latency | | ✓ |92| Complex queries | ✓ | ✓ |9394### When to use SDKs vs direct HTTP9596| Scenario | Use SDK | Use cURL/HTTP |97|:---------|:--------|:--------------|98| Production code | ✓ | |99| Quick testing | | ✓ |100| Type safety needed | ✓ (TypeScript) | |101| Python async | ✓ | |102| Simple one-off request | | ✓ |103| Automatic pagination | ✓ | |104105## Workflow1061071. **Verify credentials exist**108 - Check Developer Console at console.x.com109 - Confirm app has Bearer Token or OAuth tokens110 - Verify app has access to required endpoints1111122. **Choose authentication method**113 - Public data only → Bearer Token (simplest)114 - User-context needed → OAuth 1.0a or 2.0115 - Enterprise APIs → Basic Auth1161173. **Build the request**118 - Select endpoint from API reference119 - Add required parameters (query, user ID, etc.)120 - Add field parameters to request specific data121 - Add expansions for related objects122 - Set Authorization header with credentials1231244. **Handle the response**125 - Check HTTP status code (200 = success, 4xx/5xx = error)126 - Parse JSON response; primary data in `data` field127 - Check for `errors` array even in 200 responses (partial errors)128 - Extract rate limit headers for monitoring1291305. **Implement error handling**131 - 401: Check authentication credentials132 - 403: Verify app has endpoint access133 - 429: Implement exponential backoff, check `x-rate-limit-reset`134 - 400: Validate request syntax and parameters1351366. **For streaming**137 - Create rules with operators (keywords, hashtags, users)138 - POST rules to `/2/tweets/search/stream/rules`139 - GET `/2/tweets/search/stream` to connect140 - Handle keep-alive signals (blank lines every 20 seconds)141 - Implement reconnection logic for disconnects142143## Common gotchas144145- **Bearer Token vs OAuth confusion**: Bearer Token is app-only (public data). OAuth is for user-context. Don't mix them up.146- **Missing fields in response**: Fields are opt-in. Default responses are minimal. Always add `?tweet.fields=` or `?user.fields=` for the data you need.147- **Rate limits reset every 15 minutes**: Not per hour. Check `x-rate-limit-reset` header, not clock time.148- **Stream requires at least one rule**: Connecting to `/2/tweets/search/stream` without rules returns 409 Conflict.149- **Callback URLs must match exactly**: Trailing slashes, protocol, and case matter. Use `http://127.0.0.1` for local dev, not `localhost`.150- **Partial errors in 200 responses**: When requesting multiple resources, some may fail. Check the `errors` array even if status is 200.151- **Search query syntax is strict**: Operators like `from:`, `has:images`, `lang:` are case-sensitive. Quotes required for phrases.152- **Deleted posts return 404**: Don't retry; the post is gone.153- **Protected accounts' posts need authorization**: Can't access with Bearer Token alone.154- **Stream disconnects after 20 seconds of silence**: If no data or keep-alive received, reconnect immediately.155- **Expansions require field parameters**: Adding `expansions=author_id` doesn't return author data without `user.fields=`.156157## Verification checklist158159Before submitting work with X API:160161- [ ] Authentication credentials are valid and not hardcoded162- [ ] Correct authentication method chosen (Bearer vs OAuth)163- [ ] All required parameters included in request164- [ ] Field parameters added for needed data (not relying on defaults)165- [ ] Error handling implemented (401, 403, 429, 400, 5xx)166- [ ] Rate limit headers checked in response167- [ ] For streaming: rules created before connecting to stream168- [ ] For streaming: reconnection logic handles disconnects169- [ ] Response parsing handles both success and error cases170- [ ] Partial errors checked (errors array in 200 responses)171- [ ] No credentials in code or logs172- [ ] Tested with actual API (not just documentation)173174## Resources175176**Comprehensive navigation**: https://docs.x.com/llms.txt177178**Critical pages**:1791. [X API Introduction](https://docs.x.com/x-api/introduction) — Overview of all endpoints and features1802. [Make Your First Request](https://docs.x.com/make-your-first-request) — Quick start with cURL examples1813. [Authentication Overview](https://docs.x.com/fundamentals/authentication/overview) — All auth methods explained182183---184185> For additional documentation and navigation, see: https://docs.x.com/llms.txt