Learn how to initialize and configure the Twikit Client for interacting with Twitterâs API
The Client class is the main interface for interacting with Twitterâs API. It handles authentication, request management, and provides access to all Twitter API operations.
The auth_info_1 and auth_info_2 parameters can be a username, email address, or phone number. The order is flexible, and auth_info_2 is optional but recommended.
# Save cookies after loginawait client.login( auth_info_1='example_user', password='your_password', cookies_file='cookies.json')# Or save manuallyclient.save_cookies('cookies.json')
# Load from fileclient.load_cookies('cookies.json')# Or load manually from dictimport jsonwith open('cookies.json', 'r') as f: client.set_cookies(json.load(f))
cookies = client.get_cookies()print(cookies)# {'auth_token': '...', 'ct0': '...', ...}
Store cookies securely and reuse them to skip the login process. If a cookies file is provided to login() and exists, the login will be skipped entirely.
# Set proxy during initializationclient = Client(proxy='http://proxy.example.com:8080')# Or update it laterclient.proxy = 'http://another-proxy.com:3128'# Check current proxyprint(client.proxy)
Requires authentication: Must log in with Twitter credentials
Full API access: Can post tweets, send DMs, follow users, and more
Rate limits: Higher limits for most operations
Use case: Building bots, automation, or apps that need to perform actions
from twikit import Clientclient = Client('en-US')await client.login( auth_info_1='username', password='password')# Full access to API operationsawait client.create_tweet('Hello, world!')
import asynciofrom twikit import Clientclient = Client('en-US')async def setup_client(): """Initialize client with saved cookies or login.""" try: client.load_cookies('cookies.json') print('Loaded cookies from file') except FileNotFoundError: await client.login( auth_info_1='username', password='password' ) client.save_cookies('cookies.json') print('Logged in and saved cookies')async def main(): await setup_client() # Now use the client for API operations user = await client.user() print(f'Logged in as: {user.name}')asyncio.run(main())
Set an account to act as (for authorized delegate access):
# Act as another user accountclient.set_delegate_account('1234567890') # user_id# Make requests as the delegated accountawait client.create_tweet('Posting as delegate')# Clear delegationclient.set_delegate_account(None)
Retrieve information about the authenticated account:
# Get user IDuser_id = await client.user_id()# Get full user objectuser = await client.user()print(f'Name: {user.name}')print(f'Followers: {user.followers_count}')print(f'Bio: {user.description}')