Getting started with the user lookup endpoints

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

If you would like to see some code snippets in different languages, please visit these endpoints API Reference pages

 

Prerequisites

For you to be able to complete this guide, you will have need to have a set of keys and tokens, which you can generate by following these steps:

  1. Apply for a developer account and receive approval.
  2. If you already have a developer account, activate the new developer portal.
  3. Create a Project and an associated developer App in the developer portal.
  4. Navigate to your app's “Keys and tokens” page, and save your API Keys, Access Tokens, and Bearer Token to your password manager.

 

Steps to build a user lookup 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 GET /users/by 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. To do this with the GET /users/by endpoint, you must pass your developer App's Bearer Token along with your request.

First, from within the GET /users/by request in Postman, navigate to the “Authentication” tab. In the "Type" dropdown, select "Bearer Token", and then copy and paste your Bearer Token from your password manager into the "Token" field.
 

Step three: Identify and specify which user(s) you would like to retrieve

You must specify a user or a set of users that you would like to receive within the request. Depending on which user endpoint you use, you can pass either a user ID or a username. In this situation, we are going to use the GET /users/by endpoint which allows you to pass multiple usernames in a single request (rather than the single-ID, multi-ID, and single-username endpoints) and pass a set of usernames using the usernames query parameter.

Usernames are simply the account handle that you can find within an account's profile URL. For example, the following account’s username is twitterdev.

https://twitter.com/TwitterDev

In Postman, navigate to the "Params" tab and enter this username, or a string of usernames separated by a comma, into the "Value" column of the username parameter, making sure to not include any spaces between usernames and commas. 

Key Value
username twitterdev,twitterapi,adsapi

 

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/by?usernames=twitterdev,twitterapi,adsapi&user.fields=created_at&expansions=pinned_tweet_id&tweet.fields=author_id,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": "2013-12-14T04:35:55.000Z",
      "id": "2244994945",
      "name": "Twitter Dev",
      "pinned_tweet_id": "1255542774432063488",
      "username": "TwitterDev"
    },
    {
      "created_at": "2007-05-23T06:01:13.000Z",
      "id": "6253282",
      "name": "Twitter API",
      "username": "TwitterAPI"
    },
    {
      "created_at": "2013-02-27T20:01:12.000Z",
      "id": "1225933934",
      "name": "Twitter Ads API",
      "username": "AdsAPI"
    }
  ],
  "includes": {
    "tweets": [
      {
        "author_id": "2244994945",
        "created_at": "2020-04-29T17:01:38.000Z",
        "id": "1255542774432063488",
        "text": "During these unprecedented times, what’s happening on Twitter can help the world better understand & respond to the pandemic. \n\nWe're launching a free COVID-19 stream endpoint so qualified devs & researchers can study the public conversation in real-time. https://t.co/BPqMcQzhId"
      }
    ]
  }
}

Was this document helpful?

Thank you for the feedback. We’re really glad we could help!

Thank you for the feedback. How could we improve this document?

Thank you for the feedback. Your comments will help us improve our documents in the future.