Twitter API Guide

Twitter has an interesting API which contains multiple options for developers to research. Here we review several topics of how to work with Twitter API v2 using API tester app:

  • Actions with likes,

  • Retweets information,

  • Timeline’s use.

Getting started with Twitter API

To be able to send requests to this API you will need to get an access token. To get that you should create a developer’s account on Twitter. The full instruction you can get via this link:

https://developer.twitter.com/en/docs/twitter-api/getting-started/getting-access-to-the-twitter-api 

To get a developer’s account your personal Twitter account must be fully verified. This includes your current email address and a phone number. Without this information you will not be able to get an access to the Twitter API.

As far as Twitter has a very strict developer’s policy make sure that information that you filled in your application do not violates this policy! Sometimes it takes time before your application to the developer’s account will be approved by moderators.

Once you got all necessary access keys you can proceed to Twitter API.

All requests have the common endpoint:

https://api.twitter.com

The methods of twitter API accepts various parameters which are used to cusotmize the requests according to needs. The list of Twitter API v2 endpoints with detailed request description can be found via this link:

https://developer.twitter.com/en/docs/api-reference-index#twitter-api-v2

Getting a list of liked Tweets

With Twitter API v2 you can get an access to the information about user’s likes. Let’s try to make a GET request to review a list of liked Tweets by a concrete user. For example, we’ll take an official account of Elon Musk. To get started with request we need:

  1. To tap «Create new request» button and select GET method.

  2. To place this endpoint into the address line:

    https://api.twitter.com/2/users/:id/liked_tweets  

You can import this request into API Tester app via these links:

Note that you need to place your Bearer Token into the OAuth section of EACH REQUEST in order to get a response!

Now we will need to add a user ID. To find a user ID you need to make an additional request to retrieve a singe user with a username:

You can import this request into API Tester app via these links:

After importing this request you need to place a username into the Path Variables section. In this example we’ll take Elon Mask account, it’s username «elonmusk».

When all parameters are set we can run request. In the response we can find a user’s ID, name and username.

Now we can copy the ID and get back to the initial request. Place the user ID from previous step into the Path Variables section and run the request.

Done! Now you can find a list of 100 tweets liked by the user.

There are some additional parameters available for this request. You can find their description and purpose here:

 https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/get-users-id-liked_tweets

Let’s add parameters "expansions" (to get a users IDs) and "max_results" (to get 10 Tweets instead of 100). To do that you need to place a meaning "author_id" for parameter «expansions» and number 10 for parameter "max_results".

This response contains not only Tweets information but also at the end you can find all user IDs mentioned in the response with their usernames.

Tweets liking users

Now we will review the request which allows you to get the information about Tweet’s liking users. The endpoint is following:

https://api.twitter.com/2/tweets/:id/liking_users

You can import this request into API Tester app via these links:

To run this request you will need to:

  1. Import request via links above or create a request by yourself from scratch (to do so you can use the instructions from the previous request).

  2. You must specify a Tweet about which you would like to receive the information. You can find the Tweet ID by navigating to twitter.com, clicking on a Tweet, and then looking in the URL. For example, the following URL's Tweet ID is 1519495072802390016:

https://twitter.com/elonmusk/status/1519495072802390016?s=20&t=yZ8URkjvSsuG6p-HZ6-EWA

3. Place the Tweet Id from the previous step as a meaning for Path Variable «id» in the request settings screen.

4. Run the request and study the response.

Twitter API offers many options for users to expand the requests with additional parameters. In this request let’s try to add an information about comments for this Tweet from users who liked it ("description") and get an URL of the users profile images ("profile_image_url") using the "user.fields" parameter. You will need to place these values into the Query Params section in a comma-separated list without spaces between commas and fields. Also, we will shorten the response list to 10 users.

Here is the resulted response.

You can search for detailed information about this request and its additional parameters via link:

https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/get-tweets-id-liking_users

Retweets

This request is extremely convenient in cases when you need to get the information about who has Retweeted a Tweet. To make this request use the following endpoint:

https://api.twitter.com/2/tweets/:id/retweeted_by

You can import this request into API Tester app via these links:

To run this request we also need an ID of a tweet (you can follow the same steps which we described in the previous request). Let’s get the same Tweet and place its ID into the Path Variables section:

As you can see, this request is quite similar to one where we wanted to get information about Tweet’s likes. Now we will add some parameters to get more information. For example, you want to know the location of users who retweeted the Tweet and exact time when retweet was made. To do so we use Query Params section and add values "location" and "created_at" for param "user.fields".

After adding the parameters response has changed. If you take a closer look, you can notice that not all users have a location data. This is the case, due to the fact that some users prefer to hide their private data (such as location) and it’s not available publicly. But in general you can still get a lot of information.

If you need to study information about request and its additional parameters, it’s available via link:

https://developer.twitter.com/en/docs/twitter-api/tweets/retweets/api-reference/get-tweets-id-retweeted_by

Timelines

This function of Twitter API is extremely useful for those who needs to collect the data about someone’s Twitter profile. This request returns Tweets composed by a single user, specified by the requested user ID. By default, the most recent ten Tweets are returned per request. But you can collect up to the 3500 Tweets from one user. Next, we’ll describe how to do that using API Tester.

  1. Add the endpoint for request:

    https://api.twitter.com/2/users/:id/tweets

You can import this request into API Tester app via these links:

2. Pick a user whose Twitter profile you would like to explore and find user’s ID with request which was described in previous section:

 curl -X GET ‘https://api.twitter.com/2/users/by/username/:username'

In this example we’ll study Twitter account of Bill Gates (@BillGates), user ID 50393960.

3. Place user ID as a value in Path Variables section and run the request.

Total count of Tweets in response is 10. Twitter API is constructed in a way of book, in order to not make responses huge by size and an amount of information in the end of many responses you can find the following line:

"next_token": "7140dibdnow9c7btw421dyxdoegupc2r4js276uwgzgvk".

This token is used to get user an access to the next response page. To do so, you need to copy the value of this token and place it in Query Params sections as a value for "pagination_token" parameter. Now we can run request again.

We can see that response has changed and shows new Tweets, also in the end of the response you can find two tokens: «next_token» and «previous_token». These tokens provide an access to Tweets according to their name, you just need to place them as a meaning to «pagination_token» parameter.

In the next section we’ll review different parameters which can be applied for this request.

Adding timestamp for getting Tweets for a certain time period

For example, you would like to know what Tweets were published by Bill Gates from March 2020 until April 2020. To get these Tweets we need to add two parameters to the request "start_time" and "end_time". It is important to place a time period in a right format, otherwise this parameter will not be applied to the request. For Twitter API we use this format: YYYY-MM-DDTHH:mm:ssZ, for our example:

  1. the start time is 2020-03-01T00:00:01Z

  2. the end time is 2020-04-01T00:00:01Z

Note that Tweets are delivered in reverse-chronological order, starting with the most recent. So if you will specify only one timestamp, you will get a response which will contain the most recent Tweets. That’s why in cases when you need to get a concrete Tweets from concrete period of time for your convenience specify both - the start and the end time.

Now we need to place values into the Query Params section of the Request. To make sure that we really got necessary Tweets, let’s also add parameter which indicates when Tweet was created (param "tweet.fields", value "created_at"). After all parameters are set we can run request.

As you can see, we got Tweets which were created in a given period. You can also use a pagination token (see description above) to study all Tweets for chosen time period.

Explore the preview image URL and attachments for Tweets in timeline

When you need to expand the scope of information about Tweets from certain timestamp you may use additional parameters. Let’s say we want to add an information about media attachments of Tweets. Then we need to add the following parameters:

  1. expansions: attachments.media_keys;

  2. tweet.fields: created_at,attachments;

  3. media.fields: preview_image_url;

  4. max_results: 20.

After setting all necessary parameters we can run request. In the end of the response you can find all attachments which were available from the uploaded Tweets and their media keys (special identifiers for each concrete media file), in this example only one Tweet contain preview image URL.

In this guide we reviewed some basic requests from Twitter API v2 and ran them in API Tester app. Twitter API also has an integration with Postman app, where full collection of all requests is constructed. You can download this collection from Postman via this link and import if to API Tester:

https://www.postman.com/twitter/workspace/twitter-s-public-workspace/collection/9956214-784efcda-ed4c-4491-a4c0-a26470a67400?ctx=documentation

Last updated