DEV Community

Cover image for How to create a Twitter bot for free?
d60
d60

Posted on

How to create a Twitter bot for free?

Have you ever wanted to create a Twitter bot but were put off by the high cost of API access or the hassle of obtaining API keys? However, you don't have to give up on it. That's because with the Python library "Twikit," you can use the API for free without obtaining API keys. This article introduces Twikit and explains how to use it.

GitHub logo d60 / twikit

Twitter API wrapper for Python with **no API key required** | Without api key | Free | Twitter scraper | Twitter Bot

Number of GitHub stars GitHub commit activity

Twikit

A simple API wrapper to interact with twitter's internal API.

If you have any questions, please ask on Reddit or Discord.

Features

No API Key Required

The library uses an unofficial API and therefore does not require an API key.

Completely Free

The service is entirely free to use.

Both Synchronous and Asynchronous Support

Whether you prefer synchronous or asynchronous programming Twikit supports both, providing flexibility for different use cases.

Functionality

This library allows you to perform various Twitter-related actions, including:

  • Create tweets
  • Search tweets
  • Retrieve trending topics
  • etc...

Installing

pip install twikit

Quick Example

Define a client and log in to the account.

from twikit import Client
USERNAME = 'example_user'
EMAIL = 'email@example.com'
PASSWORD = 'password0000'

# Initialize client
client = Client('en-US')
# Login to the service with provided user credentials
client.login(
    auth_info_1=USERNAME ,
    
Enter fullscreen mode Exit fullscreen mode

What is Twikit?

Twikit is a library that allows you to perform most basic Twitter operations, such as creating tweets, searching, sending direct messages, and getting trends, without the need for an API key. It only requires your account's password, email address, and username. It supports both synchronous and asynchronous operations, making it suitable for various situations.

Installation

Install using pip:

pip install twikit
Enter fullscreen mode Exit fullscreen mode

Quick Example

from twikit import Client

# Authentication information
AUTH_INFO_1 = 'email@example.com'
AUTH_INFO_2 = 'username'
PASSWORD = 'pass0000'

# Create a client object and log in
client = Client('en-US')
client.login(
    auth_info_1=AUTH_INFO_1,
    auth_info_2=AUTH_INFO_2,
    password=PASSWORD
)

# Upload the image and get the media ID
media_id = client.upload_media('image.png', 0)
# Create a tweet and attach the image
client.create_tweet(
    'Hello, world!',
    media_ids=[media_id]
)

# Search for tweets containing the keyword 'python'
# and retrieve the latest ones
tweets = client.search_tweet('python', 'Latest')

# Iterate over the search results and print each tweet's text
for tweet in tweets:
    print(tweet)
    # <Tweet id="00000">
    # <Tweet id="00001">
    # <Tweet id="00002">
    # ...
Enter fullscreen mode Exit fullscreen mode

Conclusion

In addition to the features introduced here, there are many other functions available, so be sure to check out the GitHub repository and documentation. Thank you!

Top comments (1)