XTools allows an Agent to interact with X, providing functionality for posting, messaging, and searching tweets.
Prerequisites
Install the required library:
Tweepy is a Python library for interacting with the X API.
Setup
- 
Create X Developer Account
- Visit developer.x.com and apply for developer access
- Create a new project and app in your developer portal
 
- 
Generate API Credentials
- Navigate to your app’s “Keys and tokens” section
- Generate and copy these credentials:
- API Key & Secret
- Bearer Token
- Access Token & Secret
 
 
- 
Configure Environment
export X_CONSUMER_KEY=your_api_key
export X_CONSUMER_SECRET=your_api_secret
export X_ACCESS_TOKEN=your_access_token
export X_ACCESS_TOKEN_SECRET=your_access_token_secret
export X_BEARER_TOKEN=your_bearer_token
 
Example
cookbook/tools/x_tools.py
from agno.agent import Agent
from agno.tools.x import XTools
# Initialize the X toolkit
x_tools = XTools(
    wait_on_rate_limit=True # Retry when rate limits are reached
) 
# Create an agent equipped with X toolkit
agent = Agent(
    instructions=[
        "Use X tools to interact as the authorized user",
        "Generate appropriate content when asked to create posts",
        "Only post content when explicitly instructed",
        "Respect X's usage policies and rate limits",
    ],
    tools=[x_tools],
    show_tool_calls=True,
)
# Search for posts
agent.print_response("Search for recent posts about AI agents", markdown=True)
# Create and post a tweet
agent.print_response("Create a post about AI ethics", markdown=True)
# Get user timeline
agent.print_response("Get my timeline", markdown=True)
# Reply to a post
agent.print_response(
    "Can you reply to this [post ID] post as a general message as to how great this project is: https://x.com/AgnoAgi",
    markdown=True,
)
# Get information about a user
agent.print_response("Can you retrieve information about this user https://x.com/AgnoAgi ", markdown=True)
# Send a direct message
agent.print_response(
    "Send direct message to the user @AgnoAgi telling them I want to learn more about them and a link to their community.",
    markdown=True,
)
# Get user profile
agent.print_response("Get my X profile", markdown=True)
| Parameter | Type | Default | Description | 
|---|
| bearer_token | str | None | Bearer token for authentication | 
| consumer_key | str | None | Consumer key for authentication | 
| consumer_secret | str | None | Consumer secret for authentication | 
| access_token | str | None | Access token for authentication | 
| access_token_secret | str | None | Access token secret for authentication | 
| include_post_metrics | bool | False | Include post metrics (likes, retweets, etc.) in search results | 
| wait_on_rate_limit | bool | False | Retry when rate limits are reached | 
| 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 | 
| search_posts | Searches for tweets | 
Developer Resources