Official sample code for the X API v2 across Python, TypeScript, JavaScript, Java, C# / .NET, Ruby, and R.
First-party SDK samples use the official XDK libraries:
- Python:
xdk(xdevplatform/xdk-python) - TypeScript / JavaScript:
@xdevplatform/xdk(xdevplatform/xdk-typescript) - Java:
com.x:xdk(xdevplatform/xdk-java) - C# / .NET:
XDevPlatform.Xdk(xdevplatform/xdk-csharp)
├── python/ # 80 Python samples (xdk)
├── typescript/ # 84 TypeScript ESM samples (@xdevplatform/xdk, strict tsc)
├── javascript/ # 84 JavaScript / Node.js CommonJS samples (@xdevplatform/xdk)
├── java/ # 80 Java 17+ samples (com.x:xdk)
├── csharp/ # 80 C# / .NET 8+ samples (XDevPlatform.Xdk)
├── ruby/ # 58 Ruby samples
├── r/ # 5 R samples
├── llms.txt # LLM-friendly documentation & SDK conventions
└── api-index.json # Machine-readable endpoint-to-sample catalog
Sign up and create an App in the X Developer Console.
For App-only read operations (search, lookup, streams, webhooks, activity):
export BEARER_TOKEN='your_bearer_token'For User-context actions (post, like, repost, bookmark, DMs, media upload, etc.):
export CLIENT_ID='your_oauth2_client_id'
export CLIENT_SECRET='your_oauth2_client_secret'# Python (xdk)
cd python && pip install xdk
python posts/search_recent.py
# TypeScript (@xdevplatform/xdk)
cd typescript && npm install
npx tsx posts/search_recent.ts
# JavaScript / Node.js (@xdevplatform/xdk)
cd javascript && npm install
node posts/search_recent.js
# Java 17+ (com.x:xdk)
cd java
mvn compile exec:java -Dexec.mainClass="posts.SearchRecent"
# C# / .NET 8+ (XDevPlatform.Xdk)
cd csharp
dotnet run -- posts/search_recent
# Ruby
cd ruby && bundle install
ruby posts/search_recent.rb| Category | Python (xdk) |
TypeScript (@xdevplatform/xdk) |
JavaScript (@xdevplatform/xdk) |
Java (com.x:xdk) |
C# (XDevPlatform.Xdk) |
Ruby | R |
|---|---|---|---|---|---|---|---|
| Activity (subscriptions & stream) | ✅ | ✅ | ✅ | ✅ | ✅ | ||
| Compliance (batch jobs) | ✅ | ✅ | ✅ | ✅ | ✅ | ||
| Direct Messages (1:1 & group DMs) | ✅ | ✅ | ✅ | ✅ | ✅ | ||
| Lists (CRUD, members, followers, posts) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |
| Media Upload (single & chunked v2) | ✅ | ✅ | ✅ | ✅ | ✅ | ||
| Posts (search, counts, create, delete, hide reply, quotes) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Spaces (lookup & search) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |
| Streams (filtered rules & 1% sampled) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | |
| Usage (daily Post usage) | ✅ | ✅ | ✅ | ✅ | ✅ | ||
| Users (lookup, timelines, likes, bookmarks, follows, mutes, blocks, reposts) | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
| Webhooks (register, list, validate CRC, delete, server) | ✅ | ✅ | ✅ | ✅ | ✅ |
| Type | Use Case | Env Vars |
|---|---|---|
| Bearer Token | App-only endpoints (search, lookup, streams, webhooks, activity) | BEARER_TOKEN |
| OAuth 2.0 PKCE | User-context actions (post, like, repost, bookmark, mute, follow, DMs, media) | CLIENT_ID, CLIENT_SECRET (or ACCESS_TOKEN) |
| Webhook HMAC | Webhook CRC verification (webhook_server) |
CONSUMER_SECRET |
llms.txt- Concise context file covering SDK packages, auth patterns, pagination, and streaming across all languagesapi-index.json- Machine-readable catalog mapping every endpoint to its exact file path inpython/,typescript/,javascript/,java/,csharp/,ruby/, andr/
Apache 2.0