The Twitter API allows you to stream public Tweets from the platform in real-time so that you can display them and basic metrics about them.
In this tutorial, you will learn how to:
Set an objective for your work
Plan and prepare to process the needed data
Connect and authenticate to the appropriate API endpoint
Handle errors and disconnections
Display Tweets and basic metrics about them
- You must have signed up for a developer account. Access is available with active keys and tokens for a developer App that is attached to a Project created in the developer portal.
Steps to consider
Step 1: Set an objective for your work
First, define what you want to accomplish and decide on the data you need to meet that objective.
Staying informed on a topic of interest: For example, you would like to stay current on updates, news, and events about Twitter’s API.
Detecting current trends: You have a hard time detecting current trends and would like to extract signals about what people are discussing in the world.
Step 2: Plan and prepare to process the needed data
Staying informed on a topic of interest
For the example of staying informed on a topic of interest, you will need to decide on what type of data you need and how to go about getting it in real time.
For example, in continuing with the example on keeping current with updates to the Twitter API, you will need to make sure you get Tweets from the @TwitterDev and the @TwitterAPI accounts the moment they are posted. You will also only want Tweets that contain links so that you can read up on any further context provided with a Tweet.
To get this kind of data in real time, you can use the filtered stream endpoints. The filtered stream endpoints requires you to define filtering criteria in order for it to know what kind of Tweets to send to you.
After defining your filtering criteria, you will need to apply this to the filtered stream endpoints.
Filtering criteria are applied to the filtered stream endpoints in the form of rules. Rules allow you
to narrow down to only the Tweets you are looking for by using a set of operators.
While creating a filter it is often helpful to think about what type of data you don’t want to receive and work backward from there. You will need to make sure you have the right keyword and adjust your rule to prevent unwanted Tweets from entering into your stream. For examples of how you can build more complex rules, see our documentation on building a rule.
Based on the filtering criteria just defined, you can create a rule containing the following operators.
The “from:” operator matches any Tweet from a specific user and the “has:links” operator matches Tweets containing links in the Tweet body. Together, these operators form the following rule which instructs the filtered stream endpoints to filter for Tweets from the accounts @Twitterdev and @TwitterApi that contain links.
“from:twitterdev from:twitterapi has:links”
To add this rule, issue a POST request to The filtered stream rules endpoint with an added payload as an array of rules and operators. The example below shows how you can use cURL to do so. To authenticate, replace
$BEARER_TOKEN (including the dollar sign) with the Bearer Token from your App in the developer portal.