Toolkits
X (Twitter)
Prerequisites
The following example requires the tweepy
library.
To set up an X developer account and obtain the necessary keys, follow these steps:
-
Create an X Developer Account:
- Go to the X Developer website: https://developer.x.com/
- Sign in with your X account or create a new one if you don’t have an account.
- Apply for a developer account by providing the required information about your intended use of the X API.
-
Create a Project and App:
- Once your developer account is approved, log in to the X Developer portal.
- Navigate to the “Projects & Apps” section and create a new project.
- Within the project, create a new app. This app will be used to generate the necessary API keys and tokens.
- You’ll get a client id and client secret, but you can ignore them.
-
Generate API Keys, Tokens, and Client Credentials:
- After creating the app, navigate to the “Keys and tokens” tab.
- Generate the following keys, tokens, and client credentials:
- API Key (Consumer Key)
- API Secret Key (Consumer Secret)
- Bearer Token
- Access Token
- Access Token Secret
-
Set Environment Variables:
- Export the generated keys, tokens, and client credentials as environment variables in your system or provide them as arguments to the
XTools
constructor.X_CONSUMER_KEY
X_CONSUMER_SECRET
X_ACCESS_TOKEN
X_ACCESS_TOKEN_SECRET
X_BEARER_TOKEN
- Export the generated keys, tokens, and client credentials as environment variables in your system or provide them as arguments to the
Example
The following example demonstrates how to use the X toolkit to interact with X (formerly Twitter) API:
cookbook/tools/x_tools.py
Toolkit Params
Parameter | Type | Default | Description |
---|---|---|---|
bearer_token | str | None | The bearer token for X API authentication |
consumer_key | str | None | The consumer key for X API authentication |
consumer_secret | str | None | The consumer secret for X API authentication |
access_token | str | None | The access token for X API authentication |
access_token_secret | str | None | The access token secret for X API authentication |
Toolkit Functions
Function | Description |
---|---|
create_post | Creates and posts a new post |
reply_to_post | Replies to an existing post |
send_dm | Sends a direct message to a X user |
get_user_info | Retrieves information about a X user |
get_home_timeline | Gets the authenticated user’s home timeline |