Create Your First Tweet Programmatically Using Python
To query the Twitter API and tweet using Python, you need basically three things:
- The Tweepy library,
- A Developer Account,
- Access Keys and Secrets.
The Tweepy library is a Python library built to interact with the Twitter API. It contains functions and methods used to manipulate Twitter data. The Developer Account gives you access to create projects and apps using Twitter API. The Access Keys and Secrets are your unique codes that allow you connect with your account. Let’s get right into it.
Tweepy
Now that we’ve decided that we can only afford the FREE
access level, let’s go right ahead to see a use case of the API in creating a tweet programmatically, considering that’s the only option that’s available to us.
You can access the Twitter API using different programming languages, but for the purpose of this article, we’d be sticking with Python. There’s a popular Python library that’s used to query the Twitter API called Tweepy
. You can check out the official documentation here: https://docs.tweepy.org/en/stable/
Tweepy
supports both Twitter API v1.1 and Twitter API v2, but per sticking to Twitter’s recommendation, we’d use Tweepy
with v2. To use Tweepy
in any of your IDEs, you must first install it. You can do that by simply running the line of code below. (It is believed that you already know how and where to run Python codes. If you don’t, you can check out our Python course).
pip install tweepy
After installing the library, we can then import it this way:
import tweepy
Now that we have the Python library that allows us query Twitter API and create tweets programmatically, we need to understand how the different API versions work. Tweepy
’s interface for making requests to Twitter API v1.1 endpoints is API
, while Tweepy’s interface for making requests to Twitter API v2 endpoints is Client
. We’ll be sticking to Twitter API v2, therefore, the interface we’ll use will be Client
.
Twitter Developer Account (Access Keys and Secrets)
Please read the first article in this series to create a Twitter Developer Account and get your Access Keys and Secrets.
Creating a Tweet via the API (finally)
At this point, we have installed and imported the Tweepy
library, gotten our developer account, created a Project and an App, and finally gotten our Keys, Tokens and Secrets. We have all we need, we can finally see how beautiful it is to create a tweet programmatically via the Twitter API.
To connect to the v1.1 of the Twitter API, we use the interface called API
like so:
tweepy.API()
To connect to the v2, which is what Twitter recommended, we use the Client
interface:
tweepy.Client()
We now need to assign the keys to different variables and use the v2 interface to pass those keys as arguments to the function. The API Key and Secret saved to variables called consumer_key
and consumer_secret
respectively. The Bearer Token to bearer_token
variable and finally the Access Token and Secret mapped to the access_token
and access_secret
variables respectively.
Finally, using the Client
interface because we’re querying the v2 API, we pass the arguments and save it to a variable called client
.
bearer_token = 'xxxxxxxxxxxxxxxxxx'
consumer_key = 'xxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxx'
access_token = 'xxxxxxxxxxxxxxxxx'
access_secret = 'xxxxxxxxxxxxxxxxxx'
client = tweepy.Client(bearer_token=bearer_token, consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token=access_token, access_token_secret=access_secret)
After running the chunk of code above, we’re finally connected to the Twitter API using our account, we can then proceed to creating a tweet programmatically. To do this, we use the create_tweet()
function. The function accepts the argument text
(str | None) – which is the text of the tweet being created. For the body of our text, let’s use “Hello Tweeps, I created this tweet programmatically using Python”.
client.create_tweet(text="Hello Tweeps, I created this tweet programmatically using Python")
Congrats, you’ve written your first tweet programmatically using Python with the Twitter API. Now head over to your Twitter feed and see your new tweet smiling back at you.
If you found any of the codes here difficult or found it hard to understand any part of the article, you can register for our Python bootcamp and learn Python programming from scratch.