xdevplatform
samples
Python

Sample code for the X API v2 endpoints

Last updated Jul 3, 2026
3.2k
Stars
1.1k
Forks
108
Issues
+1
Stars/day
Attention Score
79
Language breakdown
Python 33.7%
JavaScript 27.2%
Ruby 26.4%
Java 12.1%
R 0.5%
โ–ธ Files click to expand
README

X API v2 Sample Code

X API v2

Working code samples for the X API v2 in Python, JavaScript, Ruby, Java, and R.

๐Ÿ“ Repository Structure

โ”œโ”€โ”€ python/           # 65 Python examples
โ”œโ”€โ”€ javascript/       # 59 JavaScript examples  
โ”œโ”€โ”€ ruby/             # 58 Ruby examples
โ”œโ”€โ”€ java/             # 19 Java examples
โ”œโ”€โ”€ r/                # 5 R examples
โ”œโ”€โ”€ llms.txt          # LLM-friendly documentation
โ””โ”€โ”€ api-index.json    # Machine-readable endpoint catalog

๐Ÿš€ Quick Start

1. Get API Credentials

Sign up at the X Developer Portal.

2. Set Environment Variables

For read-only operations (search, lookup):

export BEARERTOKEN='yourbearer_token'

For user actions (post, like, repost, bookmark, etc.):

export CLIENTID='yourclient_id' export CLIENTSECRET='yourclient_secret'

For OAuth 1.0a (legacy endpoints):

export C export C

3. Run an Example

# Python
cd python && pip install -r requirements.txt
python posts/search_recent.py

JavaScript

cd javascript node posts/search_recent.js

Ruby

cd ruby && bundle install ruby posts/search_recent.rb

Java

cd java javac -cp ".:lib/*" posts/RecentSearchDemo.java java -cp ".:lib/*" RecentSearchDemo

๐Ÿ“š Examples by Category

| Category | Python | JavaScript | Ruby | Java | R | |----------|--------|------------|------|------|---| | Posts (search, create, delete, likes, reposts) | โœ… | โœ… | โœ… | โœ… | โœ… | | Users (lookup, followers, following, blocks, mutes) | โœ… | โœ… | โœ… | โœ… | โœ… | | Timelines (user, mentions, home) | โœ… | โœ… | โœ… | โœ… | | | Streams (filtered, sampled) | โœ… | โœ… | โœ… | โœ… | | | Lists (lookup, manage, members, follow) | โœ… | โœ… | โœ… | | | | Spaces (lookup, search) | โœ… | โœ… | โœ… | โœ… | | | Bookmarks | โœ… | โœ… | โœ… | | | | Direct Messages | โœ… | | | | | | Media Upload | โœ… | | | | | | Compliance | โœ… | โœ… | | | | | Usage | โœ… | โœ… | | โœ… | |

๐Ÿ” Authentication

| Type | Use Case | Env Vars | |------|----------|----------| | Bearer Token | Read-only (search, lookup) | BEARER_TOKEN | | OAuth 2.0 PKCE | User actions (post, like, repost, bookmark, mute, etc.) | CLIENTID, CLIENTSECRET | | OAuth 1.0a | Legacy endpoints (if applicable) | CONSUMERKEY, CONSUMERSECRET |

๐Ÿค– For LLMs

  • llms.txt - Context file for AI assistants
  • api-index.json - Machine-readable endpoint catalog

๐Ÿ”— Resources

๐Ÿ“„ License

Apache 2.0

ยฉ 2026 GitRepoTrend ยท xdevplatform/samples ยท Updated daily from GitHub