Overview
Twikit provides a simple interface for creating tweets with various types of content. You can post text-only tweets, attach media files (images, videos), or create polls to engage with your audience.
All tweet creation methods require authentication. Make sure you’ve logged in to the client before attempting to create tweets.
The simplest way to create a tweet is with just text content:
import asyncio
from twikit import Client
client = Client('en-US')
async def main():
await client.login(
auth_info_1='USERNAME',
auth_info_2='EMAIL',
password='PASSWORD'
)
# Create a simple text tweet
await client.create_tweet('Hello, Twitter!')
asyncio.run(main())
To attach images or videos to your tweet, you need to upload the media files first and then include their IDs when creating the tweet:
import asyncio
from twikit import Client
client = Client('en-US')
async def main():
await client.login(
auth_info_1='USERNAME',
auth_info_2='EMAIL',
password='PASSWORD'
)
# Upload media files
media_ids = [
await client.upload_media('./media1.png', 0),
await client.upload_media('./media2.png', 1),
await client.upload_media('./media3.png', 2)
]
# Create tweet with media
await client.create_tweet('Check out these images!', media_ids)
asyncio.run(main())
The second parameter in upload_media() is the media index, which determines the order of media in your tweet. Start with 0 for the first media file.
Create engaging polls by first creating a poll URI, then attaching it to your tweet:
import asyncio
from twikit import Client
client = Client('en-US')
async def main():
await client.login(
auth_info_1='USERNAME',
auth_info_2='EMAIL',
password='PASSWORD'
)
# Create a poll with multiple options
poll_uri = await client.create_poll(
['Option 1', 'Option 2', 'Option 3']
)
# Create tweet with poll
await client.create_tweet(
'What do you think?',
poll_uri=poll_uri
)
asyncio.run(main())
You can reply to existing tweets using the reply() method on a tweet object:
import asyncio
from twikit import Client
client = Client('en-US')
async def main():
await client.login(
auth_info_1='USERNAME',
auth_info_2='EMAIL',
password='PASSWORD'
)
# Get a tweet by ID
tweet = await client.get_tweet_by_id('1234567890')
# Reply to the tweet
await tweet.reply('Great tweet!')
asyncio.run(main())
Complete working example
Here’s a comprehensive example demonstrating different tweet creation methods:
import asyncio
from twikit import Client
# Enter your account information
USERNAME = '...'
EMAIL = '...'
PASSWORD = '...'
client = Client('en-US')
async def main():
# Login
await client.login(
auth_info_1=USERNAME,
auth_info_2=EMAIL,
password=PASSWORD
)
# Create simple text tweet
await client.create_tweet('Hello from Twikit!')
# Create tweet with media
media_ids = [
await client.upload_media('./media1.png', 0),
await client.upload_media('./media2.png', 1),
await client.upload_media('./media3.png', 2)
]
await client.create_tweet('Check out these images!', media_ids)
# Create tweet with poll
poll_uri = await client.create_poll(
['Option 1', 'Option 2', 'Option 3']
)
await client.create_tweet('Vote now!', poll_uri=poll_uri)
# Get a tweet and reply to it
tweet = await client.get_tweet_by_id('1234567890')
await tweet.reply('Interesting tweet!')
asyncio.run(main())
Be mindful of Twitter’s rate limits when creating tweets. Creating too many tweets in a short period may result in temporary restrictions on your account.
Key points
Authentication required
Always authenticate with client.login() before creating tweets
Upload media first
Media files must be uploaded using upload_media() before attaching to tweets
Use async/await
All client methods are coroutines and must be called with await
Respect rate limits
Be aware of Twitter’s API rate limits to avoid account restrictions