22
33[ ![ X API v2] ( https://img.shields.io/endpoint?url=https%3A%2F%2Ftwbadges.glitch.me%2Fbadges%2Fv2 )] ( https://developer.x.com/en/docs/twitter-api )
44
5- Working code samples for the ** X (formerly Twitter) API v2** in Python, JavaScript, Ruby, and Java .
5+ Working code samples for the ** X API v2** in Python, JavaScript, Ruby, Java, and R .
66
77## 📁 Repository Structure
88
99```
10- ├── python/ # Python examples (most complete)
11- ├── javascript/ # JavaScript (Node.js) examples
12- ├── ruby/ # Ruby examples
13- ├── java/ # Java examples
10+ ├── python/ # 108 Python examples
11+ ├── javascript/ # 70 JavaScript examples
12+ ├── ruby/ # 58 Ruby examples
13+ ├── java/ # 19 Java examples
14+ ├── r/ # 5 R examples
1415├── llms.txt # LLM-friendly documentation
1516└── api-index.json # Machine-readable endpoint catalog
1617```
@@ -19,110 +20,66 @@ Working code samples for the **X (formerly Twitter) API v2** in Python, JavaScri
1920
2021### 1. Get API Credentials
2122
22- Sign up at the [ X Developer Portal] ( https://developer.x.com/en/portal/dashboard ) and create a project/app .
23+ Sign up at the [ X Developer Portal] ( https://developer.x.com/en/portal/dashboard ) .
2324
2425### 2. Set Environment Variables
2526
2627``` bash
27- # For app-only authentication (read-only endpoints)
2828export BEARER_TOKEN=' your_bearer_token'
29-
30- # For user context authentication (actions on behalf of users)
3129export CONSUMER_KEY=' your_consumer_key'
3230export CONSUMER_SECRET=' your_consumer_secret'
3331```
3432
35- ### 3. Choose Your Language
36-
37- | Language | Setup | Run Example |
38- | ----------| -------| -------------|
39- | ** Python** | ` pip install -r python/requirements.txt ` | ` python python/posts/search_recent.py ` |
40- | ** JavaScript** | ` cd javascript && npm install ` | ` node javascript/posts/search_recent.js ` |
41- | ** Ruby** | ` gem install typhoeus ` | ` ruby ruby/posts/search_recent.rb ` |
42- | ** Java** | Add dependencies (see java/README.md) | ` javac && java SearchRecent ` |
43-
44- ## 📚 Available Examples
45-
46- ### Posts (formerly Tweets)
47-
48- | Operation | Python | JavaScript | Ruby | Java |
49- | -----------| --------| ------------| ------| ------|
50- | Create Post | ✅ | ✅ | | |
51- | Delete Post | ✅ | ✅ | | |
52- | Lookup Posts | ✅ | ✅ | ✅ | |
53- | Search Recent | ✅ | ✅ | ✅ | ✅ |
54- | Search Full Archive | ✅ | | | |
55- | Post Counts | ✅ | | | |
56- | Quote Posts | ✅ | | | |
57- | Repost | ✅ | | | |
58- | Like/Unlike | ✅ | | | |
59-
60- ### Users
61-
62- | Operation | Python | JavaScript | Ruby | Java |
63- | -----------| --------| ------------| ------| ------|
64- | Lookup Users | ✅ | ✅ | ✅ | ✅ |
65- | Get Me | ✅ | | | |
66- | Followers | ✅ | ✅ | ✅ | |
67- | Following | ✅ | | | |
68- | Block/Unblock | ✅ | | | |
69- | Mute/Unmute | ✅ | | | |
70-
71- ### Timelines
72-
73- | Operation | Python | JavaScript | Ruby |
74- | -----------| --------| ------------| ------|
75- | User Posts | ✅ | ✅ | ✅ |
76- | User Mentions | ✅ | | |
77- | Home Timeline | ✅ | | |
78-
79- ### Streams
80-
81- | Operation | Python | JavaScript |
82- | -----------| --------| ------------|
83- | Filtered Stream | ✅ | ✅ |
84- | Sampled Stream | ✅ | |
85-
86- ### Other
87-
88- | Category | Python | JavaScript | Ruby |
89- | ----------| --------| ------------| ------|
90- | Bookmarks | ✅ | | |
91- | Spaces | ✅ | ✅ | |
92- | Lists | ✅ | ✅ | ✅ |
93- | Direct Messages | ✅ | | |
94- | Media Upload | ✅ | | |
95- | Compliance | ✅ | | |
96- | Usage Stats | ✅ | | |
97-
98- ## 🔐 Authentication Types
99-
100- | Type | Use Case | Required Env Vars |
101- | ------| ----------| -------------------|
102- | ** Bearer Token** | Read-only endpoints (search, lookup) | ` BEARER_TOKEN ` |
103- | ** OAuth 1.0a** | User actions (post, like, follow) | ` CONSUMER_KEY ` , ` CONSUMER_SECRET ` |
104- | ** OAuth 2.0 PKCE** | Newer endpoints (bookmarks) | OAuth flow required |
33+ ### 3. Run an Example
10534
106- ## 🔗 Resources
35+ ``` bash
36+ # Python
37+ cd python && pip install -r requirements.txt
38+ python posts/recent_search.py
10739
108- - [ X API Documentation] ( https://developer.x.com/en/docs/twitter-api )
109- - [ Developer Portal] ( https://developer.x.com/en/portal/dashboard )
110- - [ API Reference Index] ( https://developer.x.com/en/docs/api-reference-index )
111- - [ Postman Collection] ( https://t.co/twitter-api-postman )
40+ # JavaScript
41+ cd javascript && npm install
42+ node posts/recent_search.js
43+
44+ # Ruby
45+ cd ruby && bundle install
46+ ruby posts/recent_search.rb
47+ ```
48+
49+ ## 📚 Examples by Category
50+
51+ | Category | Python | JavaScript | Ruby | Java | R |
52+ | ----------| --------| ------------| ------| ------| ---|
53+ | Posts (search, create, delete, likes, retweets) | ✅ | ✅ | ✅ | ✅ | ✅ |
54+ | Users (lookup, followers, blocks, mutes) | ✅ | ✅ | ✅ | ✅ | ✅ |
55+ | Timelines (user, mentions, home) | ✅ | ✅ | ✅ | ✅ | |
56+ | Streams (filtered, sampled) | ✅ | ✅ | ✅ | ✅ | |
57+ | Lists (lookup, manage, members) | ✅ | ✅ | ✅ | | |
58+ | Spaces (lookup, search) | ✅ | ✅ | ✅ | ✅ | |
59+ | Bookmarks | ✅ | ✅ | ✅ | | |
60+ | Direct Messages | ✅ | | | | |
61+ | Media Upload | ✅ | | | | |
62+ | Compliance | ✅ | ✅ | | ✅ | |
63+ | Usage | ✅ | ✅ | | ✅ | |
64+
65+ ## 🔐 Authentication
66+
67+ | Type | Use Case | Env Vars |
68+ | ------| ----------| ----------|
69+ | Bearer Token | Read-only (search, lookup) | ` BEARER_TOKEN ` |
70+ | OAuth 1.0a | User actions (post, like) | ` CONSUMER_KEY ` , ` CONSUMER_SECRET ` |
71+ | OAuth 2.0 PKCE | Bookmarks, newer endpoints | OAuth flow |
11272
11373## 🤖 For LLMs
11474
115- This repository includes:
116- - ** ` llms.txt ` ** - Comprehensive context file for AI assistants
75+ - ** ` llms.txt ` ** - Context file for AI assistants
11776- ** ` api-index.json ` ** - Machine-readable endpoint catalog
11877
119- ## 🤝 Contributing
78+ ## 🔗 Resources
12079
121- We welcome contributions! Please:
122- 1 . Follow existing code patterns
123- 2 . Include proper documentation headers
124- 3 . Test your examples before submitting
80+ - [ X API Documentation] ( https://developer.x.com/en/docs/twitter-api )
81+ - [ Developer Portal] ( https://developer.x.com/en/portal/dashboard )
12582
12683## 📄 License
12784
128- Apache 2.0 - See [ LICENSE ] ( LICENSE )
85+ Apache 2.0
0 commit comments