How to explore a user's Tweets using v2 of the Twitter API
Tags: twitter, tutorial
Published: 2020-08-28 20:31:28

---
title: How to explore a user's Tweets using v2 of the Twitter API
published: true
description: 
tags: #twitter, #tutorial
// cover_image:
---
![](https://dev-to-uploads.s3.amazonaws.com/i/avh7i3gcuvggk0h8lzwc.png)

_This tutorial was originally posted to [developer.twitter.com](https://developer.twitter.com/en/docs/tutorials/explore-a-users-tweets)_

The Twitter API allows you to retrieve and explore the timeline of public Tweets a user has posted. To explore a user’s Tweets you can use recent search to get Tweets from the past seven days.

In this tutorial, you will learn how to:

* Set an objective for your work
* Connect and authenticate to the Twitter API
* Shape your data
* Build any additional logic or connect to other APIs you are using

## Prerequisites
* You must have a [developer account](https://developer.twitter.com/en/portal/petition/essential/basic-info). Access is available with active keys and tokens for a developer App that is attached to a [Project](https://developer.twitter.com/en/docs/projects.html) created in the [developer portal](https://developer.twitter.com/en/docs/developer-portal.html).
* A Bearer Token from your App in the [Twitter developer portal](https://developer.twitter.com/en/docs/developer-portal/overview).

## Steps to consider
### Step 1: Set an objective for your work
While exploring the Tweets of a user you may want to take a moment to figure out exactly what you will want to accomplish. 
* Creating workflow triggers: A user Tweets x and from there y happens such as if the handle TwitterDev Tweets the words “important change” you can get an email to your work account letting you know what the important change would be.
* Performing Tweet sentiment: You can also do tasks such as performing Tweet sentiment on your timeline so you can analyze your own Tweets to let you know how positive your week was.
 
### Step 2: Connect and authenticate to the Twitter API
To connect to the Twitter API, you will need to authenticate by passing in your credentials before you can get any data back. At this point, you may want to consider using a library to help you with OAuth such as [requests](https://requests.readthedocs.io/en/master/) for Python or [httparty](https://github.com/jnunemaker/httparty) for Ruby. You should also consider how you want to store your credentials securely. Some options include using environment variables or setting up a configuration file that you can store in your .gitignore file on GitHub. 

Be sure to take a look at the [authentication section of our documentation](https://developer.twitter.com/en/docs/authentication.html). If you lose your credentials or think they may be compromised, you can always regenerate them by navigating to the App in question’s keys and tokens section. Additionally, you can [read more about security best practices](https://developer.twitter.com/en/docs/authentication/guides/authentication-best-practices.html).

### Step 3: Shape your data
You will need to adjust your query to get the right Tweets you want. For example, for the TwitterDev example, you’d want to make sure the endpoint you are making a GET request to is:

```
https://api.twitter.com/2/tweets/search/recent?query=from:TwitterDev
```

You may want to check out our sample code for recent search at this point:
* [Python](https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/master/Recent-Search/recent_search.py)
* [Node.js](https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/master/Recent-Search/recent_search.js)
* [Ruby](https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/master/Recent-Search/recent_search.rb)

To adjust this for your handle, you would change where it says TwitterDev to another. You can also adjust the query to include only Tweets with images or a certain hashtag. You can learn more about how to [craft your query](https://developer.twitter.com/en/docs/twitter-api/tweets/search/integrate/build-a-rule.html). It’s often helpful to take a step back and figure out which Tweets you wouldn’t want to get back. From there, you can make sure your query includes the appropriate logic to exclude unwanted Tweets.

You also will want to consider how many Tweets you get back, and what fields the data you are looking for is located in. By default, you will get back the id and text of each Tweet. You can make adjustments to this payload by adding additional [fields](https://developer.twitter.com/en/docs/twitter-api/fields.html) and [expansions](https://developer.twitter.com/en/docs/twitter-api/expansions.html) to your query. 

You will also need to consider how many Tweets you will need to perform the task you have in mind. The first 10 Tweets are found in an object called  `data`. To obtain additional Tweets, you need to handle [pagination](https://developer.twitter.com/en/docs/twitter-api/tweets/search/integrate/paginate.html). To do so, you will need to access the  `next_token`  from the object called meta and connect to get the additional Tweet objects back for the next 10 Tweets until there is no longer a  `next_token`  available. Some [libraries and third-party tools](https://developer.twitter.com/en/docs/twitter-api/tools-and-libraries.html) may have pagination built-in.

In the process of shaping your data, you may need to take an iterative approach to adjust your query until it returns a payload that meets your objective. Sometimes using a REST client such as [Postman](https://www.postman.com/) or [Insomnia](https://insomnia.rest/) can be helpful for seeing what data you get back and making adjustments before you start writing code. [Our Postman collection](https://t.co/twitter-api-postman) may also be helpful.

### Step 4: Build any additional logic or connect to other APIs you are using
After you have a payload that contains exactly what you are looking for you can now make sure your code has built-in logic, timing, or API connections needed to perform the task you identified in your objective. 

For the example of creating a workflow trigger for when TwitterDev Tweets the words “important” and “change”, you will need to see if those two words appear in the text of the Tweet and connect to an API or library to send an email to your work account that contains the text of the Tweet when those two words appear. You will also need to set up a time for this action to happen regularly such as using a Cron Job or AWS Lambda. For this example, you may want to do this on a weekly basis to see if any Tweets from this past week mentioned those two words. We have a [similar code sample](https://github.com/twitterdev/parking/tree/master/recent_search) which looks to see if the words “suspended” and “tomorrow” appear in the same Tweet by the handle @NYCASP and sends you a text.

With the example of performing sentiment analysis on your Tweets from the past week, you will need to create your own logic for using a library such as TensorFlow or connect to a sentiment analysis API. From there, you may want to create a data visualization of your Tweet sentiment from the past week to determine how positive of a week it was. You can check out [a blog post](https://blog.twitter.com/developer/en_us/topics/tips/2020/how-to-analyze-the-sentiment-of-your-own-tweets.html) walking you through how to create this sample step by step or [take a look at the code](https://github.com/twitterdev/weekly-tweet-sentiment).

## Next steps
* Learn more about the [recent search endpoint.](https://developer.twitter.com/en/docs/twitter-api/tweets/search.html)
* Get started building with our [quick start guide](https://developer.twitter.com/en/docs/twitter-api/tweets/search/quick-start.html).
* Check out our [API reference](https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference.html) to learn more about what’s available.
* Learn more about [building search queries](https://developer.twitter.com/en/docs/twitter-api/tweets/search/integrate/build-a-rule.html).
* Get inspired by reading our other [tutorials](https://developer.twitter.com/en/docs/twitter-api/tutorials.html).

Let us know on the [forums](http://twittercommunity.com/) if you run into any troubles along the way or Tweet us at @TwitterDev if this tutorial inspires you to create anything.