How To Make A Twitter Bot In Python Using Tweepy Essential Access (API v2)

If you’re looking to build a Python Twitter bot with the newly updated API v2 Essential access features and Tweepy 4.0, you’ve come to the right place.

Twitter is one of the most widely used social networks, building a bot with Twitter is a great way to learn python and experiment with the intricacies of python programming.

However, the recent changes in the Twitter API and the company as a whole, it has changed a lot with how Tweepy interacts with Twitter. As a result, it has caused a lot of bots on Twitter to become inactive and several popular youtube videos and blogs to be outdated.

For those who have been testing out bots lately using the  API v1, you may have noticed that there has been a slight confusion in the programming community on overstack, with the all too familiar “ApiResponseError: Request failed with code 403 – You currently have Essential access, which includes access to Twitter API v2 endpoints only.

That is why in this article, we will provide an updated tutorial on interacting Twitter with the python tweepy 4.0 package using the API v2 Essential Access, which is pretty much the basic access that everyone can freely use and avoid errors like the one above.

In this article, you’ll learn how to:

  • Create a twitter developer account and get twitter API
  • Access and use tweepy with the API v2 Essential Access
  • Make your Python twitter bot create a tweet
  • Automatically Follow users with the tweepy bot

Free Bonus: Click Here To Get A FREE Introduction To Python Course and learn the basics of Python 3, such as Lists, NumPy, Functions and Packages.

What is Tweepy

Tweepy is a free python package that allows you to interact with the Twitter API using python. Within tweepy, prebuilt functions allow you to interact with Twitter, such as creating tweets and many more.

About the Twitter API (Essential and Elevated access)

In this article, we’ll have to create the twitter API for your twitter account (which i will show you later). However, getting the context behind the updates is necessary before moving forward.

Previously, Twitter allows you to access their API and functions using the API v1, this is where most of the tutorials assist you. However, with the newly updated API, essential accounts is strictly only available to use API v2. Therefore, if you use functions with the old Tweepy API v1 code, it will throw you an error (if you do not request for special permissions from Twitter).

To fix this, you’ll need the elevated access (which requires special permission from Twitter), or just simply use the essential access (which i will show you how to do below). However, the only problem is essential to access features are still continuously being built and upgraded, so you may not find as many functions compared to elevated access features.

How To Get Your Twitter Developer API

Create your Twitter developer account

Before we do any coding, we need to have a Twitter account created firstly, you can do so by clicking here.

Next, we need to go on Twitter developer and click “Sign Up” at the top right of the screen. You’ll probably be promoted with the following details:

Twitter account: This is the username of your Twitter account, it should be prefilled for you

Email: Your email address for the twitter account, it should be prefilled aswell

Country: The country you’re located in

Use case: For me, I just picked student

Will you make Twitter content or derived information available to a government entity or a government-affiliated entity: For this part, I just picked “No”.

After you’ve agreed to the terms and conditions, you now need to verify your email address.

Note: If you do not have a verified phone number in your Twitter account, you’ll probably get a warning such as “There was a problem completing your request”. You can verify your phone number in the settings of your account in a new Twitter tab and click on “submit” again in your Developer Agreement & policy, which should work for you.

Make your Twitter app and get API

making a twitter app with python

If you’ve clicked on the email confirmation link during registration, you’re now probably sitting at the app name developer section. The app name is not important, you can name it whatever you’d like.

If you can’t find the section, you can click on this link:

Next, you’ll be prompted with the API keys, for the sake of congruity in this tutorial, we can just skip this section by clicking on “dashboard” and saying that “you’ve saved the keys”. Don’t worry, we can come back and regenerate our keys afterwards.

Once you’re on the dashboard, you need to follow these steps:

  1. Click on the cog wheel near your project app
  2. Click on the setup button on the settings section
getting the api of your twitter developer account

In the user authentication section settings, this is where we need to set up the permissions of our APP.

Below we have listed the settings that we have chosen for our app.

App permissions: Read and write, which allows us to Read and Post Tweets and profile information.

Type of app: Web App, Automated App or Bot

App info (Call back URL and Website URL): Since we are just building a bot for our twitter, the website section is not too important, we can just put “” for these sections.

Next we can just do these steps:

  1. Click on Save
  2. Click on Yes for “Changing permissions”
  3. Click on Done (With the client ID and Client secret, we can generate those later)
  4. Click on “Yes, I’ve Done it” for saving the ID

Afterwards, we can now regenerate and gather our API keys.

To get our keys we can follow these steps:

1 Click on Keys and Tokens
2. Click on Regenerate for all the tokens and keys below (except for OAuth 2.0 Client ID)
3. Copy and paste keys into a notepad

regenerating your api on twitter developer

Once you’ve done so, you now have the following keys and tokens:

  • API key
  • API key Secret
  • Bearer Token
  • Access Token
  • Access Token Secret

Note: These keys and tokens should be treated as a password, if someone accesses these details, they can create a bot using your Twitter account! So keep these safe.

How To Make A Python Twitter Bot With Tweepy (Essential Access API v2)

Below we have shown you a few simple codes that you can do to get started with your python twitter bot using tweepy. Since we only have essential access, we need to use the dedicated API v2 Client functions. This can be found using the link below:

Installing Tweepy

Before we can use Tweepy, we need to firstly install tweepy. This can be simply done by either

pip install tweepy

or Git to clone the repository from GitHub to install the latest development version

git clone
cd tweepy
pip install

Making Your first “Hello, World!” Tweet with Tweepy API v2

import tweepy

API_KEY = "Insert your API Key Here"
API_KEY_SECRET = "Insert API Key Secret Here"
BEARER_TOKEN = "Insert Bearer Token Here"
ACCESS_TOKEN = "Insert Access Token Here"
ACCESS_TOKEN_SECRET = "Insert Access Token Secret Here"

client = tweepy.Client(
consumer_key= API_KEY, 
consumer_secret= API_KEY_SECRET, 
bearer_token= BEARER_TOKEN, 

client.create_tweet(text = "Hello, World!")

If you run the code above with your inputted keys and tokens, Tweepy should instantly tweet “Hello, World!” on your twitter account.

  • Line 1: Import our tweepy package
  • Line 3 – 7: Insert our API and Token Keys
  • Line 9 – 14: tweepy.Client() Initializes our Session to make requests to the API and Tokens
  • Line 16: Using our session “client”, we use the create_tweet() function with the text attribute to print “Hello, World!” on our twitter

Getting the ID of a Twitter user in tweepy

One of the most common things that you’ll be doing with Tweepy is getting the user ID’s, to interact with other functions such as liking or following certain individuals, tweepy will need the integer ID rather than the username of a twitter user.

twitter_user = client.get_user(username="Twitter")
user_id =["id"]
  • Line 1: Get Twitter’s user data using client.get_user()
  • Line 2: Get the user ID number of Twitter

If you print out both the variables “twitter_user” and user_id, you’ll get the following:

Response(data=<User id=783214 name=Twitter username=Twitter>, includes={}, errors=[], meta={})


As you can see, the client.get_user() function outputs a chunk of data that we do not need, since we only need the “id” of the values, we can just choose the field by using["id"].

Follow a Twitter User’s Followers Using Tweepy

The simplest way to follow a user using tweepy is using the client.follow_user() function and inputting our user_id as the input.


However, following a Twitter user’s followers is a bit harder and also a very similar process. Since our function client.follow_user() only follows one user at a time.

For us to follow more than one user, we need to use a for loop to iterate over several user’s IDs, store the ID in a list then use another for loop to iterate over our list and follow each user.

This can be done by using the code below.

twitter_followers = client.get_users_followers(user_id, max_results=100)

followers_id = []

for follower in

for follower in followers_id:

      print("I have followed User ID: " + str(follower))    

Line 1: Using the function client.get_users_followers() , we can get the recent 100 followers of twitter’s user_id.
Line 3: Create an empty list to store our follower’s user id
Line 5 – 6: Using a for loop, we use the append function to store each follower’s id inside our followers_id list.
Line 8 – 12: Using a for loop, we use our client.follow_user() function to follow each user id iterablely inside our followers_id list, all together with a print() function to print each member we followed.

Free Bonus: There will be times when Tweepy will throw exception errors, this is because of Twitter users blocking, changing usernames and more. This will completely halt your code.

If you’d like to know how to fix this, There’s a FREE course on Datacamp which teaches you how to handle these errors.

Unfollowing Users On Tweepy

There will come a time where you’ve followed too much people and hit the maximum threshold which is around 5,000 following (as of writing this article), once you’ve hit this number Twitter won’t let you follow any more people. Therefore, you’ll need to reduce your following count.

To do this, we need to use a combination of client.unfollow_user() and client.get_users_following() from our twitter account.

Tweepy Common Errors and Solutions

403 Forbidden Error

This error occurs when you’re trying to access a feature your API currently does not have access to. Most commonly, it happens when people use the API v1 functions with only an “essential access” account. Therefore, you’ll need to use the API v2 tweepy functions.

If you’ve recently changed your API permissions, re-generating the key will use the new permissions also, if you are trying to access API v2 with essential access features.

Final Thoughts: Twitter’s Essential access (API v2) and Python Twitter bot

Building your own python bot is a great way to spark motivation and learn by doing, especially with the tweepy package where it has done a lot of the hard work for us.

However, the recent changes from API v1 and API v2 and user access requirements it has made things a bit harder and outdated a lot of bots on Twitter and some tutorials online.

Moving forward, if you’d like to learn more about building python twitter bots with essential access only, you’ll need to read more into the tweepy manual.

FAQ: Python Twitter Bot

How do you make a Twitter bot in python?

To make a twitter bot in python, you can use the “Tweepy” package, which includes useful functions that allow you to follow, like and interact with users in twitter and a solution for connecting with the twitter API.