How to set up Twitter API

and send requests using Postman

Ankita Prakash
5 min readJul 27, 2022

The Twitter API allows one to find and retrieve different forms of data from it’s social media platform. The basic version is free to use, and returns data sufficient for carrying out analyses and building side-projects. This article uses the Twitter API v2, which is the latest version of the API.

In this blog, you will learn how to set up a developer account on Twitter and send requests using the API through Postman.

You can use programming languages to send the API requests. However, since the motive here is to check the different types of data returned by Twitter API, we will stick to Postman. Postman is an API platform that helps developers create, share, and test APIs.

Setting up a Developer Account

  1. Visit https://developer.twitter.com/en and sign in with your Twitter account.
  2. After logging in, you have to input some basic details like the country you are based in, your use case, etc.

3. When you click on Let’s do this, you will find a Developer agreement & policy. Accept the Terms & Conditions and click on Submit.

4. Then verify your email ID. This is how the Twitter Developer Platform looks like.

5. Type a suitable name and click on Get Keys.

6. You will see an API key, an API key secret and a Bearer token on your screen. Copy the three values and save it somewhere safe. You will need these keys when you send requests with Postman.

7. Now you can go to your dashboard. In the left side-panel, you can find the app you just created under Projects & Apps.

You have successfully set up your Twitter Developer Account.

Setting up Postman

  1. Download and install Postman on your local system.
  2. Create a new workspace (preferably Personal).
  3. The next step is to import the Twitter API v2 collection in Postman. Click on Import in your workspace, then click on Link, and add the following link:

https://raw.githubusercontent.com/twitterdev/postman-twitter-api/master/Twitter%20API%20v2.postman_collection.json

This is the raw JSON file of the Twitter API v2 collection and is available at the the postman-twitter-api repository on GitHub.

4. If the collection is imported successfully, this is how Postman would look like.

This completes the set up of the Twitter API v2 collection in Postman. We move on to setting up the environment.

5. Similar to the process by which we imported the collection, we will now import the environment using the following link:

https://raw.githubusercontent.com/twitterdev/postman-twitter-api/master/Twitter%20API%20v2.postman_environment.json

6. By clicking on Variables, you will find a table that looks similar to this.

This is where you will add the API key (in consumer_key), API key secret (in consumer_secret) and the bearer token that you saved while setting up the developer account.

But we still need the access_token and token_secret.

7. Visit your Twitter Developer Portal and select your app. Under your project, you will find the option of Keys and tokens. Under Authentication tokens, you have the option of Access Token and Secret. Click on Generate and save the keys.

8. Add all the keys and tokens in their respective positions as both the initial value and the current value. Don’t forget to save the changes.

Finally, Postman has also been set up and we are ready to go!

Sending requests

If the setup is done correctly, this is how the Postman interface will look like.

Make sure that the environment (highlighted in red in the above image) has been selected as Twitter API v2. Now we can send requests and test the API.

Let’s start by searching for a profile with the username.

  1. Under User Lookup in the left side-panel, select GET User by Username.
  2. In the Query Params of the Params tab, select user.fields and add public_metrics as the value.
  3. Under the Path Variables, add any valid Twitter username as the value to the key username. For demo, I have used my Twitter handle, ankitaprakash_.
  4. Click on Send.

If everything works fine, you will get a json file that contains the basic public information of the Twitter user.

Here’s how your screen should look like now.

You can extract data on various other variables as well. When you hover over a specific API on the left panel, you can find the option to view documentation.

The documentation will provide you with all the information — the fields you can pass, the outputs you can expect, etc. — for each of the APIs. Feel free to try them out and get data on different variables.

Let’s try out one more request. This time, we will extract the public metrics of the following tweet by the Indian entrepreneur, Kunal Shah.

For this request, we need the tweet id, which is the set of numbers that uniquely identifies each tweet and is available in its URL.

Here, the tweet id is 1551967569925419009.

  1. Under Tweet Lookup in the left side-panel, select GET Single Tweet.
  2. In the Query Params of the Params tab, select tweet.fields and add public_metrics as the value.
  3. Under the Path Variables, add the tweet id as the value to the key id.
  4. Click on Send.

The output is a json file, as given below. It includes the counts of retweets, replies, likes and quote tweets on Kunal Shah’s original tweet.

In this manner, you can play around with any number of variables and tweets.

Why did I write this blog?

I always find documentations pretty stringent. It is a pain to follow them and I struggled immensely when I first used Twitter APIs. That’s why it seemed like the right thing to write down the steps in a manner that is easy to follow. Here’s the output — a ~1000 words technical article that explains how one can set up their Twitter developer account and test the API with Postman.

Please reach out to me with feedback and follow me on Medium.

--

--

Ankita Prakash

Ankita Prakash, an analyst at a startup, writes about business, analytics, writing, etc. and her personal journey as an early-career professional.