This tutorial is written at a beginner level, and uses cURL and the command line to make a request to the Twitter API v2 recent search endpoint.
Twitter data is used by developers, students, and researchers to study various topics. You may want to study the conversation around a topic using hashtags, set of keywords, etc. from the last few days. For example, you may want to study the sentiment of Tweets about a baseball game over the weekend. To retrieve data such as this, you will use the recent search endpoint, which provides Tweets that match a query that you submit from over the last 7 days.
In this guide, you will learn how to:
- Identify which past conversation you wish to study
- Connect and authenticate to the recent search endpoint to receive Tweets
- Analyze the data for past conversations
Before you start, you will need the following:
Steps to consider
Step 1: Identify which past conversation you wish to study
As a first step, you need to identify the type of conversation you wish to study. For example, you may decide to study all Tweets about the climate for the last few days. In order to do this, you might be looking for Tweets with a certain hashtag or words. Once you have identified these words, you will need to convert these into a query (also referred to as rule or filter). You can define the rule to include/exclude keywords, images, etc. You can also specify certain account names to get Tweets from. Check out this guide to see which operators are available, and to learn how to build a query to get that data you need.
In this example, we will search for all Tweets with images about the hashtag #Caturday, excluding retweets. Your query will look something like this:
#caturday has:images -is:retweet
Step 2: Build your query, and make your request
Once you have decided what data you want and what operators you will use, you can connect to the recent search endpoint. The example is a good starting place. To properly authenticate this request, replace
$BEARER_TOKEN with your Bearer Token. You will also need to replace
$QUERY , which we'll describe next.