Skip to main content

llama-index-tools-twittrapi: Twitter/X tools for LlamaIndex agents

Twitter/X tools for LlamaIndex agents – TwittrAPI

PyPI test Python License: MIT

llama-index-tools-twittrapi is a set of LlamaIndex tools for TwittrAPI, a REST API for public Twitter/X data. LlamaIndex agents can read tweets, profiles, followers, search results, lists, communities and trends. Pay as you go from $0.065 per 1,000 calls, no X developer account. Not affiliated with X Corp.

Get an API key: twittrapi.com/start · Pricing · Docs · Status · also available on RapidAPI

SDKs and integrations: Python · Node.js · Go · MCP server · n8n · Make · Apify · LangChain · Agent skill · Examples

All tools are read-only. They are built on the official twittrapi Python SDK.

Install

pip install -U llama-index-tools-twittrapi

Python 3.10 or newer. Works with llama-index-core 0.13 and 0.14.

Authentication

Use either a direct TwittrAPI key or a RapidAPI key.

export TWITTRAPI_API_KEY="your-twittrapi-key"   # direct API
# or
export RAPIDAPI_KEY="your-rapidapi-key"         # RapidAPI

Or pass the key in code:

from llama_index.tools.twittrapi import TwittrAPIToolSpec

spec = TwittrAPIToolSpec(api_key="your-twittrapi-key")      # direct API
spec = TwittrAPIToolSpec(rapidapi_key="your-rapidapi-key")  # RapidAPI

If both environment variables are set, TWITTRAPI_API_KEY wins.

Subscribed to another TwittrAPI listing on RapidAPI? Set its host: TwittrAPIToolSpec(rapidapi_key="...", rapidapi_host="twttr.p.rapidapi.com"), or the RAPIDAPI_HOST environment variable.

Quickstart

Call a tool directly

from llama_index.tools.twittrapi import TwittrAPIToolSpec

spec = TwittrAPIToolSpec()
print(spec.get_user_by_username("jack"))

Give an agent all the tools

pip install -U llama-index-llms-anthropic
import asyncio

from llama_index.core.agent.workflow import FunctionAgent
from llama_index.llms.anthropic import Anthropic
from llama_index.tools.twittrapi import TwittrAPIToolSpec

agent = FunctionAgent(
    tools=TwittrAPIToolSpec().to_tool_list(),
    llm=Anthropic(model="claude-opus-5-5"),
    system_prompt="You research X (Twitter) with the tools you have. Cite tweet IDs.",
)


async def main() -> None:
    response = await agent.run("What has @jack posted this week? Summarize it.")
    print(response)


asyncio.run(main())

Any LLM that supports function calling works. To pick a subset of tools:

tools = TwittrAPIToolSpec().to_tool_list(
    spec_functions=[("search_tweets", "asearch_tweets"), ("get_tweet", "aget_tweet")]
)

Async

Every tool has a sync method (get_tweet) and an async one (aget_tweet). Agents that run tools asynchronously, such as FunctionAgent, use the async methods, which call the SDK's AsyncTwittrAPI client and do not block the event loop.

Tools

Tool What it does
get_user_by_username Profile by handle, including the numeric user ID
get_user_by_id Profile by numeric user ID
get_user_tweets A user's recent tweets (by user_id or username)
get_followers Accounts that follow a user
get_following Accounts a user follows
search_tweets Search with X operators (from:, lang:, since:, min_faves: ...)
get_tweet A tweet with its reply thread
get_tweet_retweets Users who retweeted a tweet
get_tweet_quotes Tweets that quote a tweet
get_list_tweets Tweets from an X list
get_community_tweets Tweets posted in an X Community
get_trends Trending topics by WOEID (1 = worldwide)
get_trend_locations Locations that have trends, with their WOEIDs

What the tools return

The tools return the API's simple format as compact JSON text: flat, camelCase tweets and users with a fixed schema (id, text, author, counts, media, ...). It uses far fewer tokens than X's raw JSON. Lists come as {"data": [...], "nextCursor": ..., "previousCursor": ..., "hasNextPage": ...}, single objects as {"data": {...}}.

For X's raw JSON (every field X sends: cards, polls, edit history, ...), opt out:

TwittrAPIToolSpec(api_response_format="raw")

Responses can be large, so each result is cut at max_chars characters (default 20,000). When that happens the text ends with a [TRUNCATED: ...] marker that says how much was cut.

Paginated tools print the next-page cursor on the first line, before the JSON, so it is never lost to truncation:

next_cursor: DAABCgABF...
Pass next_cursor as `cursor` to get the next page.

{"data":[...],"nextCursor":"DAABCgABF...","previousCursor":"...","hasNextPage":true}

On the last page (hasNextPage is false) the first line says so instead.

Change the limit:

TwittrAPIToolSpec(max_chars=8_000)

Errors

API errors (bad key, no balance, not found, rate limit) and invalid arguments raise TwittrAPIToolError with a message that starts with TwittrAPI error: or Invalid arguments:. LlamaIndex agents catch it and pass the message to the model as a tool error (ToolOutput.is_error), so the model can correct itself.

Requests that fail with 429, 5xx or a network error are retried by the SDK.

Bring your own client

To change timeouts or retries, build the SDK clients yourself and pass them in:

from twittrapi import AsyncTwittrAPI, TwittrAPI
from llama_index.tools.twittrapi import TwittrAPIToolSpec

spec = TwittrAPIToolSpec(
    client=TwittrAPI(api_key="...", timeout=30, max_retries=4),
    async_client=AsyncTwittrAPI(api_key="...", timeout=30, max_retries=4),
)

If you pass only client, the async methods run it in a worker thread.

Development

python -m venv .venv && source .venv/bin/activate
pip install -e . pytest pytest-asyncio
pytest

Support

Questions, bugs or billing: support@twittrapi.com. Issues with this package: GitHub issues.

License

MIT. See LICENSE.

Not affiliated with X Corp.

Metadata

Release files for llama-index-tools-twittrapi 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llama-index-tools-twittrapi 1.0.0
File Size Uploaded Provenance Actions
llama_index_tools_twittrapi-1.0.0.tar.gz 30.8 kB With provenance Details for llama_index_tools_twittrapi-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for llama-index-tools-twittrapi 1.0.0
File Interpreter ABI Platform Actions
llama_index_tools_twittrapi-1.0.0-py3-none-any.whl Python 3 none any Details for llama_index_tools_twittrapi-1.0.0-py3-none-any.whl

Total release size: 42.5 kB

Release files / llama_index_tools_twittrapi-1.0.0.tar.gz

Download URL llama_index_tools_twittrapi-1.0.0.tar.gz
Size 30.8 kB
Tags Source
SHA-256 checksum
How to use checksums
6f5a2a6aca4fbafbd9f6745d012f95528afb8fee4e07784649d7bb4e7633f45d
BLAKE2b-256 checksum
How to use checksums
40f1ba10c119c97edc1128e0902105d6235627c2dabcc7eb9323e46d60087f3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.

Transparency log

Release files / llama_index_tools_twittrapi-1.0.0-py3-none-any.whl

Download URL llama_index_tools_twittrapi-1.0.0-py3-none-any.whl
Size 11.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
99d9102145a72499327417cdb47cabd6a14db56cbb1227e14a4eb021b1281b5f
BLAKE2b-256 checksum
How to use checksums
c8ae0797f9c3eb328c795e5e0aa79b1bb87758da423996c4b334852320470786
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.

Transparency log

Security for llama-index-tools-twittrapi 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for llama_index_tools_twittrapi-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 15, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page