How to Use Twitter API

Esther Vaati
4 min readJun 1, 2022

An introduction to Rest API’s

Photo by Souvik Banerjee on Unsplash

What is an API

An API(Application programmable interface) is a way in which web clients communicate with web servers. For example, when you are using your favorite social media app or doing online shopping, you use an interface to search for something, the requested data may be in a database but there is a middleman between you and the database.

API’s also provide a way for developers to make applications , for example, the most commonly used API’s are

  • Google maps API
  • Facebook API
  • Instagram API
  • Telegram API

API’s can also be used to make bots. In this section, we will cover how to use the twitter API to do the following:

  • Read Tweets
  • Post Tweets
  • Fetch followers, etc

Gettting Started

The first step is to sign up for a developer account. Navigate to https://developer.twitter.com/en/docs/developer-portal/overview and sign up with your Twitter account. You should be logged in to Twitter.

Once you have signed in, you should see something like this:

Create an app

From the dashboard above, click on App app,

Add your app name and click next.

Your keys and tokens will be generated, copy and paste them somewhere safe. You can regenerate them again but you should not share them with anyone.

Explore the twitter API

Some of the capabilities of the Twitter API include

  • post tweets
  • manage likes i.e delete liked tweets or post
  • follow users
  • block users
  • mute users
  • search twitter spaces
  • hide replies
  • manage twitter bookmarks
  • manage Twittter lists

Tweepy

Tweepy is a python library that allows you to access the Twitter api. In this section, we will cover how to use tweepy to authenticate and use the Twitter API.

Create a python file twitter.py .

touch twitter.py

install Tweepy with pip in a virtual environment

pip install tweepy

Tweepy OAuth1

The first step is to perform authentication, to do that, add the following code in twitter.py file

import tweepy
consumer_key= "your_secret_key"consumer_secret = "your-api-secret-key"access_token = "your-access_token"access_token_secret ="your-access_token_secret"auth = tweepy.OAuth1UserHandler(consumer_key, consumer_secret, access_token, access_token_secret)api = tweepy.API(auth)

Here we use the token we obtained from the Twitter developer portal to perform authentication with Tweepy. the auth object will allow us to do everything, consumer_key == secret key while consumer_secret ==API SECRET KEY
We then create an api object from the Tweepy api class. The api object gives access to all the Twitter RESTFUL API methods.

Get user timeline

To get the user timeline, we use the API.home_timeline() This will get the last 20 tweets from your timeline

public_tweets = api.home_timeline()for tweet in public_tweets:    print(tweet.text)

This is a truncated result of my twitter timeline :

RT @miss_she_du: The only thing that is allowed to wound you this week is enjoyment. https://t.co/vrSc311D9TNew Remote Job! Defiant, Inc.: Enterprise Account Executive - Wordfence IntelligenceApply here:… https://t.co/LXBfsvE2MEA huge part of early stage VC is investing in companies to learn. My web3 friends will call it pay to learn.Ask around😂😂😂😂 https://t.co/WA5vyrJKjM@catalinmpit @AgoraIO This looks like a really helpful tool!@chiziaruhoma 🔥🔥🔥🙌🏻🙌🏻🙌🏻

Update status

Lets update status with a quote from Jamie Paolinetti

api.update_status("Limitations live only in our minds Jamie Paolinetti.")

Get followers of a certain account.

Suppose you want to get all the people following the official Twitter account, you would use api.get_user() method to first get the user. Then apply the follower attribute to get the last 20 followers.

user = api.get_user(screen_name='twitter')for follower in user.followers():    print(follower.screen_name)

The result will be:

Richard75452164
ClaarColligan
JohnnyHart7Tix
9Xmhrni
Ahmed15338416
SundusBuyuk
LORD_COMMANDR88
BilalSa17460209
Mdalamin0951539
Quickleeindia
JhomarAndres1
AltinSemiz
mdEnamul55
W1390730392
Aft0nR0btics
nigelbarbara4
marco_desouza2
macia_extra19
lixinghai02
penghuanghe1
61905114

Follow another user

To follow another user, you use api.create_friendship() method. For example, if you want to follow the official twitter page

api.create_friendship("twitter")

Like a tweet

To like a tweet, you use the api.create_favorite() and specify the tweet id

api.create_favorite(tweet.id)

Update profile

You can also update your profile

api.update_profile(description="New description")

Block a user

A blocked user will not be able to see or interact with you

api.create_block(screen_name)

Conclusion

This tutorial has covered everything you need to get started with creating Twitter bots that can automate your Twitter activities.

If you enjoyed this, you might relish?

--

--