Understanding the entire conversation around a Tweet with the Twitter API v2
Tags: python, api, twitter, tutorial
Published: 2022-05-06 16:31:44

The Twitter API allows developers and researchers to get Twitter data programmatically. Researchers interested in studying the conversation around a topic often use the [search endpoints](https://developer.twitter.com/en/docs/twitter-api/tweets/search/introduction) to get Tweets on a topic. Each of these Tweets returned from the [search endpoints](https://developer.twitter.com/en/docs/twitter-api/tweets/search/introduction) may have important follow-up conversations happening in replies to them, and in the Tweets quoting them, which may provide additional information to better understand the entire conversation. In this tutorial, you will learn how to get the entire conversation for a Tweet.

## Step one: Search for Tweets about a topic

In the first step, you can search for Tweets on a topic, containing certain keywords and meeting certain criteria using the [search endpoints](https://developer.twitter.com/en/docs/twitter-api/tweets/search/introduction). Currently, there are 2 [search endpoints](https://developer.twitter.com/en/docs/twitter-api/tweets/search/introduction) available in the Twitter API v2:

* [Recent search](https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference/get-tweets-search-recent), available to all developers and gives Tweets from the last 7 days
* [Full-archive search](https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference/get-tweets-search-all), currently only available to academic researchers and gives Tweets from the entire archive of public Tweets

The search endpoints require developers to specify a search query to return Tweets meeting a certain criteria. Optionally, you can specify the time period from which you want the Tweets for.

By default, you will get 10 Tweets (Tweet ID and Tweet text) per request. Per request, using the max_results parameter, you can get upto:

* 100 Tweets for the [recent search](https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference/get-tweets-search-recent) and [quote Tweets lookup](https://developer.twitter.com/en/docs/twitter-api/tweets/quote-tweets/introduction) endpoints
* 500 Tweets for the [full-archive search endpoint](https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference/get-tweets-search-all)

If you want additional Tweets, you can use [pagination with the next_token](https://developer.twitter.com/en/docs/twitter-api/pagination).

Below is an example of searching for Tweets from the [TwitterDev account](https://twitter.com/TwitterDev), using the [recent search endpoint](https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference/get-tweets-search-recent) in Python using the [Tweepy package](https://docs.tweepy.org/en/latest/client.html#).  

**Note:** You will first need to [sign up for a developer account](https://t.co/signup) and have your bearer token to use in the code samples below. Follow the steps in [this tutorial](https://developer.twitter.com/en/docs/tutorials/step-by-step-guide-to-making-your-first-request-to-the-twitter-api-v2) to get your bearer token. Also, you will need to install the Tweepy package in Python to run the code sample below. Follow the instructions in [this guide](https://dev.to/twitterdev/a-comprehensive-guide-for-using-the-twitter-api-v2-using-tweepy-in-python-15d9) to install Tweepy.

```python
import tweepy

client = tweepy.Client(bearer_token='REPLACE_ME')

# Replace with your own search query
query = 'from:TwitterDev -is:retweet'

# Replace the limit=1000 with the maximum number of Tweets you want
for tweet in tweepy.Paginator(client.search_recent_tweets, query=query,
                              tweet_fields=['context_annotations', 'created_at'], max_results=100).flatten(limit=1000):
    print(tweet.id)

```

Once you have the Tweets on the topic of your choice, for each Tweet you can get all its replies.

## Step two: For a Tweet, get all replies to it

You can get all replies to a Tweet using the conversation_id parameter available in the search endpoints. The value for this conversation_id is the Tweet ID of the original Tweet. The example below shows how to get all replies for the Tweet with ID 1503863303709286407

```python
import tweepy

client = tweepy.Client(bearer_token='REPLACE_ME')

# Replace with your own search query
query = 'conversation_id:1503863303709286407 -is:retweet'

# Replace the limit=1000 with the maximum number of Tweets you want
for tweet in tweepy.Paginator(client.search_recent_tweets, query=query,
                              tweet_fields=['context_annotations', 'created_at'], max_results=100).flatten(limit=1000):
    print(tweet.id)

```

## Step three: For a Tweet, get all its Quote Tweets

To get Quote Tweets for a Tweet, you can use the [Quote Tweets lookup endpoint](https://developer.twitter.com/en/docs/twitter-api/tweets/quote-tweets/introduction). You can pass it the Tweet ID and set the exclude parameter to retweet. This will give you all original Quote Tweets of a Tweet. The example below shows how to get all Quote Tweets for the Tweet with ID 1503863303709286407.

```python
import tweepy

client = tweepy.Client(bearer_token='REPLACE_ME')

# Replace the limit=1000 with the maximum number of Tweets you want
for tweet in tweepy.Paginator(client.get_quote_tweets, id=1503863303709286407, max_results=100,
                              exclude='retweets').flatten(limit=1000):
    print(tweet.id)

```

That’s it. You now have the entire conversation for a Tweet including all replies and quote Tweets. The [conversation_id](https://developer.twitter.com/en/docs/twitter-api/conversation-id) and the quote Tweets lookup endpoints make it really easy for developers and researchers to understand the entire conversation around a Tweet. If you have any questions or feedback about this tutorial, please reach out to me on [Twitter](https://twitter.com/suhemparack).

## Resources

* [Quote Tweets Lookup endpoint](https://developer.twitter.com/en/docs/twitter-api/tweets/quote-tweets/introduction)
* [Search endpoints in the Twitter API v2](https://developer.twitter.com/en/docs/twitter-api/tweets/search/introduction)
* [Tweepy Documentation](https://docs.tweepy.org/en/latest/client.html#)