DEV Community 👩‍💻👨‍💻

Jessica Garson for TwitterDev

Posted on • Updated on

How to explore a user's Tweets using v2 of the Twitter API

This tutorial was originally posted to

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


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 for Python or 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. 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.

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:
Enter fullscreen mode Exit fullscreen mode

You may want to check out our sample code for recent search at this point:

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. 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 and expansions 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. 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 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 or Insomnia can be helpful for seeing what data you get back and making adjustments before you start writing code. Our Postman collection 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 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 walking you through how to create this sample step by step or take a look at the code.

Next steps

Let us know on the forums if you run into any troubles along the way or Tweet us at @TwitterDev if this tutorial inspires you to create anything.

Top comments (6)

charlesr1971 profile image
Charles Robertson • Edited on

Great. Article. I am trying to migrate to v2 and am trying to emulate statuses/user_timeline with /2/users/:id/tweets

The problem is I don’t understand how to include the user [the person who has made the Tweet: sometimes me and sometimes a user, I am replying to] object inside each Tweet object.

The V2 API allows me to include the user object as a separate root property like meta.
But, not all Tweets are linked to the same user object, so I am not sure what the purpose of having the user object outside the data array, is?

Ultimately, I want to get hold of the user.following property. Unfortunately, V1.1 has deprecated the user.following property.

divyajyotiuk profile image
Divyajyoti Ukirde

I just joined Twitter developers, and wanted this! Thanks a bunch !!

jessicagarson profile image
Jessica Garson

Aww, thanks! I can't wait to see what you build!

divyajyotiuk profile image
Divyajyoti Ukirde

I did actually! Used Twitter APIs to update followers count in my name on twitter!

Thread Thread
jessicagarson profile image
Jessica Garson

So awesome!

oihamza profile image

Bookmarked! 👏🏽

Want to Create an Account?
Now it's your turn!
🗒 Share a tutorial
🤔 Reflect on your coding journey
❓ Ask a question

Create an account to join hundreds of thousands of DEV members on their journey.