An unofficial X (Twitter) SDK for Python.
Corvx is a Python library for interacting with X's (formerly Twitter) GraphQL API. It provides a simple interface for searching and retrieving tweets programmatically.
- Python >= 3.10
- Dependencies:
requests- HTTP librarybeautifulsoup4- HTML parsingordered-set- Data structure utilitiesxclienttransaction- X-Client-Transaction-Id generation
pip install corvxFor development:
pip install corvx[dev]Corvx requires two authentication tokens from X:
- auth_token: Your X authentication token (cookie)
- csrf_token (ct0): Your CSRF token (cookie)
- Log in to X (twitter.com/x.com) in your browser
- Open Developer Tools (F12)
- Go to Application/Storage → Cookies → https://x.com
- Copy the values for:
auth_tokenct0(this is your CSRF token)
Option 1: Environment Variables
export X_AUTH_TOKEN="your_auth_token_here"
export X_CSRF_TOKEN="your_csrf_token_here"Option 2: Pass to Constructor
from corvx import Corvx
corvx = Corvx(
auth_token="your_auth_token_here",
csrf_token="your_csrf_token_here"
)from corvx import Corvx
# Initialize
corvx = Corvx(
auth_token="your_auth_token",
csrf_token="your_csrf_token"
)
# Simple search - returns first 20 tweets
for tweet in corvx.search("python", limit=20):
print(f"@{tweet['username']}: {tweet['text']}")
print(f"Tweet ID: {tweet['id']}")
print()corvx.search(
query="keyword", # Search query (string or dict)
deep=False, # If True, paginate backwards until no results
fast=False, # Stop when cursor repeats (use with deep=True)
limit=None, # Max tweets to retrieve (None = unlimited)
sleep_time=20 # Delay between requests (seconds)
)# Get as many tweets as possible going back in time
for tweet in corvx.search("climate change", deep=True, limit=1000):
print(tweet['text'])# Use dictionary for more control
query = {
'rawQuery': 'python programming',
'count': 20,
'querySource': 'typed_query',
'product': 'Latest'
}
for tweet in corvx.search(query, limit=100):
print(tweet)# Search multiple keywords
queries = ["python", "javascript", "rust"]
for tweet in corvx.search(queries, limit=50):
print(f"Query: {tweet['raw_query']}")
print(f"Tweet: {tweet['text']}")
print()Each tweet dictionary contains:
{
'id': '1234567890',
'username': 'example_user',
'text': 'Tweet content here...',
'created_at': '2024-01-01T12:00:00.000Z',
'raw_query': 'python', # The query that returned this tweet
# ... additional fields
}from corvx import Corvx, NoResultsError
corvx = Corvx(auth_token="...", csrf_token="...")
try:
results = list(corvx.search("obscure_query", limit=10))
if not results:
print("No tweets found")
except NoResultsError:
print("Query returned no results")
except Exception as e:
print(f"Error: {e}")X enforces rate limits. Corvx handles this automatically by:
- Sleeping 15 minutes when rate limited (HTTP 429)
- Default 20-second delay between requests
- Customizable via
sleep_timeparameter
# Faster requests (higher risk of rate limiting)
corvx.search("query", sleep_time=5, limit=100)
# Slower, safer requests
corvx.search("query", sleep_time=60, limit=1000)Corvx uses X's GraphQL SearchTimeline endpoint:
- Endpoint ID:
7r8ibjHuK3MWUyzkzHNMYQ - Base URL:
https://x.com/i/api/graphql/7r8ibjHuK3MWUyzkzHNMYQ/SearchTimeline
The library automatically includes required headers:
Authorization: Bearer tokenX-Csrf-Token: CSRF token from cookiesX-Twitter-Active-User: yesX-Twitter-Auth-Type: OAuth2SessionX-Client-Transaction-Id: Generated per requestSec-Fetch-*: Browser security headersUser-Agent: Browser identification
Corvx uses the xclienttransaction library to generate fresh transaction IDs for each request, matching X's browser behavior.
Enable debug logging to see request details:
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger('corvx')
logger.setLevel(logging.DEBUG)
# Now search will log detailed information
corvx.search("test", limit=5)- Invalid or expired
auth_token/csrf_token - Solution: Get fresh tokens from your browser
- Too many requests
- Solution: Increase
sleep_timeor wait 15 minutes
- API endpoint changed (rare)
- Check library version is up to date
# Clone repository
git clone https://github.com/labteral/corvx.git
cd corvx
# Install development dependencies
pip install -e .[dev]
# Run tests
python test.pyGNU General Public License v3 (GPLv3)
Rodrigo Martínez ([email protected])
https://github.com/labteral/corvx
This is an unofficial library and is not affiliated with X Corp. Use at your own risk. Respect X's Terms of Service and API usage policies.