Getting started with the follows lookup endpoints

This quick start guide will help you make your first request to the follows lookup endpoints with a set of specified fields using Postman.

If you would like to see sample code in different languages, please visit our Twitter API v2 sample code GitHub repository. 

Prerequisites

To 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 follower list request

Step one: Start with a tool or library

There are several different tools, code examples, and libraries that you can use to make a request to this endpoint, but we are going to use the Postman tool here to simplify the process.

To load the Twitter API v2 Postman collection into your environment, please click on the following button:

Once you have the Twitter API v2 collection loaded in Postman, navigate to the "Follows" folder, and select "Followers of user ID". Note that these instructions will also work well with the "Users a user ID is following" request, if you'd prefer to use that endpoint. 


Step two: Authenticate your request

To properly make a request to the Twitter API, you need to verify that you have permission. To do so, this endpoint requires you to authenticate your request with either OAuth 2.0 App-OnlyOAuth 2.0 Authorization Code with PKCE, or OAuth 1.0a User Context authentication methods.

For simplicity's sake, we will utilize OAuth 2.0 App-Only with this request, but you will need to use one of the other authentication methods if you'd like to request private metrics or users. 

To utilize OAuth 2.0 App-Only, you must add your keys and tokens, specifically the App Access Token (also known as the App-only Bearer Token) to Postman. You can do this by selecting the environment named “Twitter API v2” in the top-right corner of Postman and adding your keys and tokens to the "initial value" and "current value" fields (by clicking the eye icon next to the environment dropdown).

These variables will automatically be pulled into the request's authorization tab if you've done this correctly.
 

Step three: Specify a user

With this endpoint, you must specify the user ID whose followers you would like to receive in the response. For example, the user ID for @TwitterDev is 2244994945.

In Postman, navigate to the "Params" tab and enter this username into the "Value" column of the id path variable (at the bottom of the section), making sure to not include any spaces before or after usernames.

Key Value
id 224499494
max_results 5

 

Step four: Identify and specify which fields you would like to retrieve

If you click the "Send" button after step three, you will receive the default user object fields in your response: id, name, and username.

If you would like to receive additional fields beyond id, name, and username, 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 additional sets of fields from different objects:

  1. The additional user.created_at field in the primary user objects.
  2. The associated pinned Tweets’ object’s default fields for the returned users: id and text.
  3. The additional  tweet.created_at field in the associated Tweet objects.
     

In Postman, navigate to the "Params" tab and add the following key:value pair to the "Query Params" table:

Key Value Returned fields
user.fields created_at user.created_at
expansions author_id tweet.idtweet.text
tweet.fields created_at includes.users.created_at

You should now see the following URL next to the "Send" button:

      https://api.twitter.com/2/users/2244994945/followers?user.fields=created_at&expansions=pinned_tweet_id&tweet.fields=created_at

    


Step five: Make your request and review your response

Once you have everything set up, hit the "Send" button and you will receive the following response:

      {
    "data": [
        {
            "created_at": "2007-05-23T06:01:13.000Z",
            "pinned_tweet_id": "1293595870563381249",
            "name": "Twitter API",
            "id": "6253282",
            "username": "TwitterAPI"
        },
        {
            "created_at": "2007-02-20T14:35:54.000Z",
            "name": "Twitter",
            "id": "783214",
            "username": "Twitter"
        },
        {
            "created_at": "2011-09-14T16:50:47.000Z",
            "pinned_tweet_id": "1293216056274759680",
            "name": "Twitter Music",
            "id": "373471064",
            "username": "TwitterMusic"
        },
        {
            "created_at": "2011-05-17T17:54:29.000Z",
            "name": "Twitter Sports",
            "id": "300392950",
            "username": "TwitterSports"
        },
        {
            "created_at": "2009-11-04T21:06:16.000Z",
            "pinned_tweet_id": "1293238566567387136",
            "name": "Twitter Design",
            "id": "87532773",
            "username": "TwitterDesign"
        }
    ],
    "includes": {
        "tweets": [
            {
                "created_at": "2020-08-12T17:11:04.000Z",
                "id": "1293595870563381249",
                "text": "Twitter API v2: Early Access released\n\nToday we announced Early Access to the first endpoints of the new Twitter API!\n\n#TwitterAPI #EarlyAccess #VersionBump https://t.co/g7v3aeIbtQ"
            },
            {
                "created_at": "2020-08-11T16:01:49.000Z",
                "id": "1293216056274759680",
                "text": "say howdy to your new yeehaw king @orvillepeck—our #ArtistToFollow this month 🤠 https://t.co/3pk9fYcPHb"
            },
            {
                "created_at": "2020-08-11T17:31:16.000Z",
                "id": "1293238566567387136",
                "text": "Deaf or hard of hearing? Surrounded by too much noise or just prefer to read?\n\nWe're happy to present the #DisabilityInUX panel recording with English subtitles featuring @treemershon, @ace_ux, @Imani_Barbarin, @alexhaagaard, and @AshleeMBoyer.\n\nhttps://t.co/1xQTn1ZYMA"
            }
    ],
    "meta": {
        "result_count": 5,
        "next_token": "F40TEHJQTL0HEZZZ"
    }
}

    


Step six: Paginate through your results

You may notice that there is a meta object located at the bottom of the response. In this case, we received a next_token, signaling that there is another page of results that we can retrieve. To pull the next page of results, you will pull the value of the next_token field, and add it to the request as the value to an additional pagination_token parameter.

Key Value
pagination_token F40TEHJQTL0HEZZZ

If you send the request after you’ve added this additional parameter, the next five results will deliver with the subsequent payload since we specified max_results as 5 in step three. You can continue to repeat this process until all results have been returned, but can also use the max_results parameter to request up to 1000 users per request so you don’t have to paginate through results quite as much.