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

Jessica Garson - Aug 28 '20 - - Dev Community

This tutorial was originally posted to developer.twitter.com

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

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:

https://api.twitter.com/2/tweets/search/recent?query=from:TwitterDev
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.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .