Top Solutions for Twitter API Integration: A Comprehensive Guide

Jennie Lee
14 min readApr 7, 2024

--

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

Introduction to Tweepy and the Twitter API v2

Tweepy is a powerful Python package that enables users to interact with the Twitter API. Recently, Tweepy version 4.0 was released, which supports the Twitter API v2 and the academic research product track. This comprehensive guide will walk you through various features and functionalities of Tweepy and the Twitter API v2, providing detailed explanations and actual working sample codes.

Prerequisites for using the Twitter API

Before diving into the functionalities of Tweepy and the Twitter API v2, there are a few prerequisites that need to be met. Firstly, you need to have a developer account on Twitter and obtain API keys and tokens. These keys and tokens are necessary to authenticate your requests to the Twitter API and access its functionalities.

In addition to the developer account and API keys, you will also need to have Python installed on your machine. Python is a versatile programming language widely used for web development and data manipulation. Once you have Python installed, you can easily install Tweepy using pip, a popular package manager for Python.

Searching for tweets from the last 7 days

One of the most common use cases for using the Twitter API is to search for tweets based on specific keywords or hashtags. With Tweepy, you can easily search for tweets from the last 7 days using the search_recent_tweets function. By specifying search queries, you can retrieve tweets that match your criteria.

To demonstrate this functionality, here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Search for tweets
tweets = api.search_recent_tweets(q='python', tweet_fields=['created_at', 'author_id'], max_results=10)

# Print tweet text and author ID
for tweet in tweets:
print(f'Tweet: {tweet.text}')
print(f'Author ID: {tweet.author_id}')
print('---')

In the above code, we authenticate ourselves using the API keys and tokens obtained from the Twitter developer account. Then, we create an API object using the tweepy.API class. Finally, we use the search_recent_tweets function to search for tweets containing the keyword 'python' and retrieve the tweet text and author ID.

Searching for tweets from the full-archive of public tweets

Apart from searching for tweets from the last 7 days, you may also have access to the academic research product track, which allows you to search for tweets from the full-archive of public tweets. In Tweepy, you can utilize the search_all_tweets function to access this data.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Search for tweets from full-archive
tweets = api.search_all_tweets(query='bitcoin', tweet_fields=['created_at', 'author_id'], max_results=10)

# Print tweet text and author ID
for tweet in tweets:
print(f'Tweet: {tweet.text}')
print(f'Author ID: {tweet.author_id}')
print('---')

In the above code, we authenticate ourselves and create an API object similar to the previous example. Then, we use the search_all_tweets function to search for tweets containing the keyword 'bitcoin' from the full-archive of public tweets. We retrieve the tweet text and author ID and print them.

Getting tweets from the full-archive of public tweets for a specific timeframe

In addition to searching for tweets from the full-archive of public tweets, you may also want to retrieve tweets from a specific timeframe. Tweepy provides the ability to specify the start time and end time parameters to narrow down your search results.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Retrieve tweets from a specific timeframe
tweets = api.search_all_tweets(query='python', start_time='2022-01-01T00:00:00Z', end_time='2022-01-02T00:00:00Z', tweet_fields=['created_at', 'author_id'], max_results=10)

# Print tweet text and author ID
for tweet in tweets:
print(f'Tweet: {tweet.text}')
print(f'Author ID: {tweet.author_id}')
print('---')

In the above code, we authenticate ourselves and create an API object as before. We then use the search_all_tweets function and specify the query, start time, end time, and other parameters. This allows us to retrieve tweets containing the keyword 'python' from January 1, 2022, to January 2, 2022. We print the tweet text and author ID for each retrieved tweet.

Retrieving more than 100 tweets at a time using Paginator

By default, the Twitter API only allows you to retrieve a maximum of 100 tweets per request. However, you can overcome this limitation by using the Paginator class provided by Tweepy. The Paginator class allows you to efficiently retrieve more than 100 tweets at a time.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Retrieve more than 100 tweets at a time
paginator = tweepy.Cursor(api.search_recent_tweets, q='python', tweet_fields=['created_at', 'author_id']).items(200)

# Print tweet text and author ID
for tweet in paginator:
print(f'Tweet: {tweet.text}')
print(f'Author ID: {tweet.author_id}')
print('---')

In the above code, we authenticate ourselves and create an API object as usual. We then use the Cursor class along with the search_recent_tweets function to retrieve more than 100 tweets at a time. In this example, we retrieve 200 tweets containing the keyword 'python' and print the tweet text and author ID for each tweet.

Writing tweets to a text file

In some cases, you may want to store the tweet IDs of your search results for further analysis or processing. Tweepy allows you to easily write the tweet IDs to a text file.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Search for tweets
tweets = api.search_recent_tweets(q='python', tweet_fields=['created_at', 'author_id'], max_results=10)

# Write tweet IDs to a text file
with open('tweet_ids.txt', 'w') as file:
for tweet in tweets:
file.write(f'{tweet.id}\n')

In the above code, we authenticate ourselves and create an API object as usual. We then use the search_recent_tweets function to search for tweets containing the keyword 'python'. Finally, we open a text file in write mode using the open function and write the tweet IDs to the file.

Getting tweets with user information for each tweet

In addition to retrieving tweet data, you may also want to retrieve user information associated with each tweet. Tweepy allows you to expand the response to include user information using the expansions parameter and the includes object.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object with user expansion
api = tweepy.API(auth, tweet_mode='extended', expansions=['author_id'], user_fields=['id', 'username'])

# Search for tweets with user information
tweets = api.search_recent_tweets(q='python', tweet_fields=['created_at'], max_results=10)

# Print tweet information with user details
for tweet in tweets:
user_id = tweet.author_id
user = api.get_users(ids=[user_id])[0]
print(f'Tweet: {tweet.full_text}')
print(f'Author ID: {user.id}')
print(f'Author Username: {user.username}')
print('---')

In the above code, we authenticate ourselves and create an API object with the user expansion using the expansions parameter and the user_fields parameter. We then use the search_recent_tweets function to search for tweets containing the keyword 'python', only retrieving the tweet creation time. For each tweet, we retrieve the author ID and use the get_users function to get user details using the ids parameter. We print the tweet text, author ID, and author username.

Getting tweets with media information for each tweet

If you are interested in retrieving media information associated with tweets, such as preview image URLs, Tweepy provides a way to expand the response to include media information using the expansions parameter and the includes object.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object with media expansion
api = tweepy.API(auth, tweet_mode='extended', expansions=['attachments.media_keys'], media_fields=['preview_image_url'])

# Search for tweets with media information
tweets = api.search_recent_tweets(q='python', tweet_fields=['created_at'], max_results=10)

# Print tweet information with media details
for tweet in tweets:
print(f'Tweet: {tweet.full_text}')
if 'attachments' in tweet.__dict__ and 'media_keys' in tweet.attachments:
media_keys = tweet.attachments['media_keys']
media = api.get_media(media_keys=[media_keys[0]])
print(f'Media URL: {media[0].preview_image_url}')
print('---')

In the above code, we authenticate ourselves and create an API object with the media expansion using the expansions parameter and the media_fields parameter. We then use the search_recent_tweets function to search for tweets containing the keyword 'python', only retrieving the tweet creation time. For each tweet, we check if it has attachments and media keys. If so, we retrieve the media details using the get_media function and print the tweet text and media URL.

Searching for geotagged tweets

If you want to search for tweets based on geographic information, such as location or country, the Twitter API v2 provides several operators that can filter the search results. The search_recent_tweets function in Tweepy allows you to use these operators to search for geotagged tweets.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Search for geotagged tweets
tweets = api.search_recent_tweets(q='place:24fbd131b7d84252', tweet_fields=['created_at', 'author_id'], max_results=10)

# Print tweet text and author ID
for tweet in tweets:
print(f'Tweet: {tweet.text}')
print(f'Author ID: {tweet.author_id}')
print('---')

In the above code, we authenticate ourselves and create an API object as before. We then use the search_recent_tweets function to search for tweets with the operator place:24fbd131b7d84252, which represents a specific place. You can replace this operator with a different place ID or use other operators like place_country or bounding_box to filter tweets based on geographic information.

Getting tweet counts (volume) for a search query

Sometimes, you may want to know the number of tweets that match a specific search term. The get_recent_tweets_count function in Tweepy allows you to retrieve tweet counts (volume) for a search query, aggregated over a specific time period.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Get tweet count for a search query
tweet_count = api.get_recent_tweets_count(query='python', granularity='day')

# Print tweet count
print(f'Tweet count: {tweet_count}')

In the above code, we authenticate ourselves and create an API object as usual. We then use the get_recent_tweets_count function to get the tweet count for the search query 'python'. In this example, we specify the granularity parameter as 'day' to get daily tweet counts. You can modify this parameter to get hourly or 15-minute counts.

Getting a user’s timeline

In addition to searching for tweets, Tweepy also allows you to retrieve a user’s timeline, which represents the most recent tweets posted by the user. The get_users_tweets method in Tweepy allows you to easily retrieve a user's timeline along with additional tweet fields of interest.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Get a user's timeline
tweets = api.get_users_tweets(user_id='123456789', tweet_fields=['created_at', 'author_id'], max_results=10)

# Print tweet text and author ID
for tweet in tweets:
print(f'Tweet: {tweet.text}')
print(f'Author ID: {tweet.author_id}')
print('---')

In the above code, we authenticate ourselves and create an API object as usual. We then use the get_users_tweets method to retrieve the most recent 10 tweets from the user with the user ID '123456789'. You can replace this user ID with the desired user ID. We retrieve the tweet text and author ID and print them.

Getting a user’s mentions

In addition to a user’s timeline, you may also be interested in retrieving mentions of a specific user. Mentions represent tweets in which a user is mentioned or tagged. Tweepy provides the get_users_mentions method to easily retrieve a user's mentions along with additional tweet fields.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Get a user's mentions
mentions = api.get_users_mentions(user_id='123456789', tweet_fields=['created_at', 'author_id'], max_results=10)

# Print mentioned tweet text and author ID
for mention in mentions:
print(f'Tweet: {mention.text}')
print(f'Author ID: {mention.author_id}')
print('---')

In the above code, we authenticate ourselves and create an API object as before. We then use the get_users_mentions method to retrieve the most recent 10 mentions of the user with the user ID '123456789'. You can replace this user ID with the desired user ID. We retrieve the mentioned tweet text and author ID and print them.

Getting a user’s followers

In addition to retrieving a user’s timeline and mentions, you may also want to retrieve a user’s followers. The get_users_followers function in Tweepy allows you to easily retrieve a user's followers along with additional user fields.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Get a user's followers
followers = api.get_users_followers(user_id='123456789', user_fields=['id', 'username'], max_results=10)

# Print followers' user IDs and usernames
for follower in followers:
print(f'Follower ID: {follower.id}')
print(f'Follower Username: {follower.username}')
print('---')

In the above code, we authenticate ourselves and create an API object as before. We then use the get_users_followers function to retrieve the most recent 10 followers of the user with the user ID '123456789'. You can replace this user ID with the desired user ID. We retrieve the followers' user IDs and usernames and print them.

Getting users that a user follows

Similarly, you may want to retrieve the list of users that a specific user follows. Tweepy provides the get_users_following function to easily retrieve this information along with additional user fields.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Get users that a user follows
following = api.get_users_following(user_id='123456789', user_fields=['id', 'username'], max_results=10)

# Print following users' user IDs and usernames
for user in following:
print(f'Following ID: {user.id}')
print(f'Following Username: {user.username}')
print('---')

In the above code, we authenticate ourselves and create an API object as before. We then use the get_users_following function to retrieve the most recent 10 users followed by the user with the user ID '123456789'. You can replace this user ID with the desired user ID. We retrieve the following users' user IDs and usernames and print them.

Getting users that like a tweet

You may also be interested in finding out the users who have liked a specific tweet. Tweepy provides the get_liking_users function to easily retrieve this information along with additional user fields.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Get users that like a tweet
liking_users = api.get_liking_users(tweet_id='123456789', user_fields=['id', 'username'], max_results=10)

# Print liking users' user IDs and usernames
for user in liking_users:
print(f'Liking User ID: {user.id}')
print(f'Liking User Username: {user.username}')
print('---')

In the above code, we authenticate ourselves and create an API object as before. We then use the get_liking_users function to retrieve the most recent 10 users who have liked the tweet with the tweet ID '123456789'. You can replace this tweet ID with the desired tweet ID. We retrieve the liking users' user IDs and usernames and print them.

Getting users that retweeted a tweet

In addition to finding users who have liked a tweet, you may also want to find users who have retweeted a specific tweet. Tweepy provides the get_retweeters function to easily retrieve this information along with additional user fields.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Get users that retweeted a tweet
retweeters = api.get_retweeters(tweet_id='123456789', user_fields=['id', 'username'], max_results=10)

# Print retweeters' user IDs and usernames
for user in retweeters:
print(f'Retweeter ID: {user.id}')
print(f'Retweeter Username: {user.username}')
print('---')

In the above code, we authenticate ourselves and create an API object as before. We then use the get_retweeters function to retrieve the most recent 10 users who have retweeted the tweet with the tweet ID '123456789'. You can replace this tweet ID with the desired tweet ID. We retrieve the retweeters' user IDs and usernames and print them.

Getting tweets that a user liked

If you want to retrieve the list of tweets that a specific user has liked, Tweepy provides the get_liked_tweets function to easily retrieve this information along with additional tweet fields.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Get tweets that a user liked
liked_tweets = api.get_liked_tweets(user_id='123456789', tweet_fields=['created_at', 'author_id'], max_results=10)

# Print liked tweets' text and author ID
for tweet in liked_tweets:
print(f'Tweet: {tweet.text}')
print(f'Author ID: {tweet.author_id}')
print('---')

In the above code, we authenticate ourselves and create an API object as before. We then use the get_liked_tweets function to retrieve the most recent 10 tweets liked by the user with the user ID '123456789'. You can replace this user ID with the desired user ID. We retrieve the liked tweets' text and author ID and print them.

Looking up tweets using tweet IDs

If you have a list of tweet IDs and want to retrieve the corresponding tweets, Tweepy provides the get_tweets function to easily look up tweets using their IDs along with additional tweet fields.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Look up tweets using tweet IDs
tweet_ids = ['123456789', '987654321']
tweets = api.get_tweets(tweet_ids=tweet_ids, tweet_fields=['created_at', 'author_id'])

# Print tweet text and author ID
for tweet in tweets:
print(f'Tweet: {tweet.text}')
print(f'Author ID: {tweet.author_id}')
print('---')

In the above code, we authenticate ourselves and create an API object as before. We then use the get_tweets function to look up tweets using a list of tweet IDs, in this case, ['123456789', '987654321']. You can replace this list with your own tweet IDs. We retrieve the tweet text and author ID and print them.

Looking up users using user IDs

Similarly, if you have a list of user IDs and want to retrieve the corresponding user details, Tweepy provides the get_users function to easily look up users using their IDs along with additional user fields.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create API object
api = tweepy.API(auth)

# Look up users using user IDs
user_ids = ['123456789', '987654321']
users = api.get_users(ids=user_ids, user_fields=['id', 'username'])

# Print users' user IDs and usernames
for user in users:
print(f'User ID: {user.id}')
print(f'Username: {user.username}')
print('---')

In the above code, we authenticate ourselves and create an API object as before. We then use the get_users function to look up users using a list of user IDs, in this case, ['123456789', '987654321']. You can replace this list with your own user IDs. We retrieve the users' user IDs and usernames and print them.

Creating a tweet

Interacting with the Twitter API also includes the ability to create tweets programmatically. Tweepy allows you to create tweets using the create_tweet method, making it easy to post tweets on behalf of a user.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create an API object
api = tweepy.API(auth)

# Create a tweet
tweet_text = "Hello, Twitter! This is a sample tweet created using Tweepy."
api.create_tweet(text=tweet_text)

In the above code, we authenticate ourselves and create an API object as usual. We then use the create_tweet method to create a tweet with the given tweet text. You can modify the tweet text to post your own tweets programmatically.

Retweeting a tweet

Apart from creating tweets, you may want to retweet an existing tweet. Tweepy allows you to retweet a tweet using the retweet method, making it easy to retweet on behalf of a user.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create an API object
api = tweepy.API(auth)

# Retweet a tweet
tweet_id = '123456789'
api.retweet(id=tweet_id)

In the above code, we authenticate ourselves and create an API object as before. We then use the retweet method to retweet the tweet with the given tweet ID. You can modify the tweet ID to retweet your desired tweet.

Replying to a tweet

In addition to creating tweets and retweeting, you may also want to reply to an existing tweet. Tweepy allows you to reply to a tweet using the create_tweet method and specifying the tweet ID you want to reply to.

Here’s an example code snippet:

import tweepy

# Authenticate to Twitter API
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)

# Create an API object
api = tweepy.API(auth)

# Reply to a tweet
tweet_id = '123456789'
tweet_text = "Hello, Twitter! This is a reply to a tweet created using Tweepy."
api.create_tweet(in_reply_to=tweet_id, text=tweet_text)

In the above code, we authenticate ourselves and create an API object as usual. We then use the create_tweet method to reply to the tweet with the given tweet ID. The in_reply_to parameter is used to specify the tweet ID we want to reply to. You can modify the tweet ID and tweet text to reply to your desired tweet.

Conclusion

In this comprehensive guide, we have explored various functionalities and features of Tweepy and the Twitter API v2. We have covered topics such as searching for tweets, retrieving user information, creating tweets, and interacting with different aspects of the Twitter API. The sample codes provided throughout the guide serve as practical examples to help you get started and utilize the power of Tweepy and the Twitter API effectively.

Looking for a Postman alternative?

Try APIDog, the Most Customizable Postman Alternative, where you can connect to thousands of APIs right now!

--

--