Skip to main content

How to use fields and expansions

By default, the X API v2 data objects include a small number of default fields when making a request without the use of the fields or expansions parameters. This guide will show you how to use the fields and expansions query parameters in your request to receive additional objects and fields in your response. In this guide, we will be requesting several fields in the following Tweet screenshot.   This image includes a screenshot of a Tweet posted by @X. You can see the Tweet text, username, published date and time, source, and public metrics. It also includes a video. As you can see in the screenshot, there are several visible pieces of information related to the Tweet, including the Tweet author, Tweet metrics, created timestamp, video, and video view count. There are also several pieces of data that are not visible within the screenshot, but are still available to request.  When making a request to the API, the default response is simple, containing only the default Tweet fields (id and text). You will also only receive the primary object that returns with the given endpoint that you are using, and not any of the associated data objects that might relate to the primary object. This simplicity, along with the fields and expansions parameters, enable you to request only those fields you require, depending on your use case.   

Requesting additional fields and objects.

First off, we will be requesting a Tweet object using a Tweet ID and the GET /tweets endpoint. Request:
Response:
The following step-by-step guide will show you how to retrieve the additional data we can see in the screenshot.
  1. Identify the additional fields that you would like to request by using our object model, or by reviewing the list of fields in the endpoints’ API reference pages. In this case, we will be requesting the following additional fields: attachments, author_id, created_at, public_metrics.
  2. Build the tweet.fields query parameter with the above fields as its value using a comma-separated list: ?tweet.fields=attachments,author_id,created_at,public_metrics
  3. Add the query parameter to the GET /tweets request that you made earlier.
Request: curl --request GET --url 'https://api.x.com/2/tweets?ids=1260294888811347969&tweet.fields=attachments,author_id,created_at,public_metrics' \ --header 'Authorization: Bearer $BEARER_TOKEN' Response:  
4. Next, we are going to request fields related to the video that was included in the Tweet. To do so, we will use the expansions parameter with attachments.media_keys as the value, and add this to the request. ?expansions=attachments.media_keys Request:  
Response, with the media object represented in the includes object:  
5. And finally, we are going to request the view count and duration of the video. These aren’t default fields so we have to specifically request them. Use the media.fields parameter with the comma-separated values, public_metrics and duration_ms in your request. ?media.fields=public_metrics,duration_ms Request:   curl --request GET --url 'https://api.x.com/2/tweets?ids=1260294888811347969&tweet.fields=attachments,author_id,created_at,public_metrics&expansions=attachments.media_keys&media.fields=duration_ms,public_metrics' --header 'Authorization: Bearer $BEARER_TOKEN' Response, which now includes all the data that can be seen in the Tweet screenshot:  
In total, we included the following parameters in this example:
  • ids=1260294888811347969
  • tweet.fields=attachments,author_id,created_at,public_metrics
  • expansions=attachments.media_keys
  • media.fields=public_metrics,duration_ms  
When tied together, here is what the full query string looks like:

X API v2 Example payloads

Tweet

Tweet reply

Extended Tweet

Tweet with media

Retweeted quote Tweet