Getting started with the filtered stream endpoints
This quick start guide will help you make your first request to the filtered stream endpoint group using a cURL request. cURL is a command line tool which allows you to make requests with minimal configuration. If you would like to see sample code in different languages, please visit ourX API v2 sample code GitHub repository.PrerequisitesTo complete this guide, you will need to have a set of keys and tokens to authenticate your request. You can generate these keys and tokens by following these steps:
- Sign up for a developer account and receive approval.
- Create a Project and an associated developer App in the developer portal.
- Navigate to your App’s “Keys and tokens” page to generate the required credentials. Make sure to save all credentials in a secure location.
Steps to build a filtered stream request using cURL
Step one: Create a rule Rules are made up of one or many different operators that are combined using boolean logic and parentheses to help define which Posts will deliver to your stream. In this guide, we will filter the stream to find Posts that contain both the keyword “cat” and images. Here is our rule: cat has:images Step two: Add a tag to your rule You can add multiple concurrent rules to your stream. When you open your streaming connection, Posts that match any of these rules will flow through the same streaming connection. To ensure that you know which Post matches which rule, you can pass a tag along with your rule creation request. Each Post that matches that rule will then include a tag field within the Post payload noting which rule it matched. For this rule, we are going to assign the following tag: cats with images Step three: Add your rule to the stream This endpoint requires you to pass an application/JSON body along with this request that contains your rule and its tag. You will also notice that we have included the rule value and tag within an add object since we are trying to add this rule to the stream. This JSON body will look like this:-X GET \
Step six: Identify and specify which fields you would like to retrieve If you connect to the stream after step five, you will receive the default Post object fields in your response: id , text, and edit_history_tweet_ids. If you would like to receive additional fields beyond these, you will have to specify those fields in your request with the field and/or expansion parameters. For this exercise, we will request a three different sets of fields from different objects:
- The additional tweet.created_at field in the primary Post objects.
- The associated authors’ user object’s default fields for the returned Posts: id, name, and username
- The additional user.created_at field in the associated user objects.
Now that you know this, you can piece together your request URL to connect to the stream, which will look like this:
https://api.x.com/2/tweets/search/stream?tweet.fields=created_at&expansions=author_id&user.fields=created_at
Step seven: Connect to the stream and review your response Now that your rule is in place and you’ve specified which fields you want returned, you’re ready to connect to the stream, which will deliver Post objects that match the rule you’ve submitted. This is what the cURL command looks like once you’ve added the URL from step six into your request:
curl -X GET -H "Authorization: Bearer $APP_ACCESS_TOKEN" "https://api.x.com/2/tweets/search/stream?tweet.fields=created_at&expansions=author_id&user.fields=created_at"
Once again, this request must be authenticated using OAuth 2.0 App-Only, so make sure to replace $APP_ACCESS_TOKEN with your credentials before copying and pasting it into your command line tool.
Once you are connected to the filtered stream you will start to receive Posts that match your rules in the following JSON format: