Run X API v2 JavaScript examples
mainAfter setting your environment variables, you can run any sample script using the node command. For example, to run a recent search sample:
# Make sure environment variables are set
node posts/search_recent.jsrepository·main·Indexed 25 days ago
https://github.com/xdevplatform/samplesWorking code samples for the X API v2 across multiple programming languages, including Python, JavaScript, Ruby, Java, and R. The repository provides examples for various API functionalities such as Posts, Users, Timelines, Streams, Spaces, Direct Messages, Media, and Compliance, with detailed setup instructions for authentication using Bearer Tokens, OAuth 2.0, and OAuth 1.0a.
After setting your environment variables, you can run any sample script using the node command. For example, to run a recent search sample:
# Make sure environment variables are set
node posts/search_recent.jsSet the required environment variables based on the type of API operation you are performing:
For read-only operations (e.g., search, lookup):
Requires BEARER_TOKEN.
For user actions (e.g., post, like, repost, bookmark):
Requires OAuth 2.0 credentials: CLIENT_ID and CLIENT_SECRET.
# For read-only operations
export BEARER_TOKEN='your_bearer_token'
# For user actions
export CLIENT_ID='your_client_id'
export CLIENT_SECRET='your_client_secret'java --version.java --versionThe Python examples require environment variables for authentication. The required variables depend on the type of operation you are performing:
For operations like search or lookup, set the BEARER_TOKEN:
export BEARER_TOKEN='your_bearer_token'For operations involving user actions (e.g., post, like, repost, bookmark, mute, etc.), you must use OAuth 2.0 authentication by setting CLIENT_ID and CLIENT_SECRET:
export CLIENT_ID='your_client_id'
export CLIENT_SECRET='your_client_secret'# For read-only
export BEARER_TOKEN='your_bearer_token'
# For user actions
export CLIENT_ID='your_client_id'
export CLIENT_SECRET='your_client_secret'package.json file.Add the following dependencies to your pom.xml to support HTTP requests and JSON parsing used in the samples.
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.1</version>
</dependency>
</dependencies>Navigate to the language-specific directory, install dependencies, and run the desired script. Below are the commands for each supported language.
# 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/*" RecentSearchDemoThe examples require different environment variables depending on the type of operation being performed. Set these in your shell before running the scripts.
Use these for search and lookup tasks:
BEARER_TOKENMost user actions (posting, liking, reposting, bookmarking) require OAuth 2.0 authentication:
CLIENT_IDCLIENT_SECRETUse these for examples targeting legacy endpoints:
CONSUMER_KEYCONSUMER_SECRET# For read-only (search, lookup)
export BEARER_TOKEN='your_bearer_token'
# For user actions (post, like, etc.)
export CLIENT_ID='your_client_id'
export CLIENT_SECRET='your_client_secret'
# For OAuth 1.0a
export CONSUMER_KEY='your_consumer_key'
export CONSUMER_SECRET='your_consumer_secret'To use the Python examples, install the required dependencies using pip. This includes the xdk (X Developer Kit for Python) package.
pip install -r requirements.txtOnce dependencies are installed and environment variables are exported, you can run any example script using the ruby command. For example, to run a recent search script:
# Make sure environment variables are set
ruby posts/search_recent.rbOnce dependencies are installed and environment variables are exported, you can run any example script using the Python interpreter. For example, to run a recent search:
python posts/search_recent.py