Testing the new Twitter API v2

Jonathan Pereira - Aug 21 '20 - - Dev Community

For developers building apps with social interactions, the Twitter API has been one of the most used API. While it was beneficial, it had its limitations. But not for long. Twitter just released the new version of the Twitter API, which has so much to offer.

What's new in Twitter API v2

Twitter seems to have finally answered the prayers of millions of developers using the Twitter API. The new API makes it simpler to listen and analyze the active conversations on the social platform. Twitter has introduced a couple of exciting things, including new endpoints, new tweet payload, conversation identifiers, and even annotations.

Having heard so much about it, we decided to take the Twitter API v2 for a spin. Luckily for us, Fiddler Everywhere can quickly test and debug APIs. SO we decided to test the Twitter API v2 on Fiddler Everywhere.

Fiddler Everywhere to the rescue

Alt Text
For those who haven't used Fiddler Everywhere before, it can capture and inspect network requests, including API calls. It also allows you to modify the request parameters to customize the response as required.

Testing the Twitter API v2

In this blog, we are going to test some of the endpoints of the new Twitter API v2 and see how it works.

Alt Text
Before getting started, make sure that you create a new app using the Twitter Developer Portal and generate the Bearer Token to authenticate the request in Fiddler Everywhere.

Users lookup

The Twitter API v2 makes it simple to get publicly available user information using the users endpoint.

Endpoint URL: https://api.twitter.com/2/users

You can do the following using this endpoint:

/users – Query multiple IDs to retrieve user information
/users/:id – Query the user ID to fetch information about a specific user
/users/by – Query the username to fetch user information

For this example, we'll use the /users/by endpoint.

We open the Composer in Fiddler Everywhere and enter the endpoint in the request bar and set the method to GET.

Alt Text
We need to provide authentication and query parameters. Under the Headers tab, add a new key-value pair called authorization and the Bearer Token as the value.

In the Params tab, add a key-value pair called usernames. You can enter the username or multiple usernames separated by commas in the value field.

Click the Execute button to see the API in action.

If everything were excellent, in the Response section below, you would see the entire response in the JSON tab.

Alt Text

You can modify the response as per your requirement by specifying the user.fields parameters.

I'm going to request the following fields:

Parameter Description
name Name of the user
username Username on Twitter
url Website URL mentioned in the profile
profile_image_url URL of the user's profile image
pinned_tweet_id Tweet ID of users pinned tweet, if any
verified Boolean response indicating whether or not Twitter has verified the user
created_at Date and time of account creation
public_metrics number of tweets, followers, following, and lists of the user
location Place mentioned in the user account

Once you hit execute, you'll notice the detailed information requested in the JSON tab of the response section.
Alt Text

Tweets lookup

Using the tweets endpoint, we can fetch tweets and information about them using the tweet ID.

Endpoint URL: https://api.twitter.com/2/tweets

We'll open a new request tab in Fiddler Everywhere, enter this API endpoint in the request bar, and set the GET method.

Again, we need to provide authentication and query parameters. You can use the same Bearer Token used in the previous request.

In the Params tab, add a key-value pair called ids. You can enter a tweet ID or multiple tweet IDs separated by commas in the value field.

Click the Execute button to make a call to the API.

You can view the tweets fetched in the JSON tab of the Response section.

Alt Text

We can add additional query parameters to get the required information. I'm going to add the following parameters:
expansions

Parameter Description
attachments.media_keys Identifies the media attached to the tweet
author_id Returns the user's unique identifier
entities.mentions.username Recognizes the Twitter users mentioned in the tweet
in_reply_to_user_id Provides the user ID of parent tweets author if the tweet is a reply
referenced_tweets.id Identifies the referenced tweet
referenced_tweets.type Identifies if the tweet is a retweet or a reply

text.fields

Parameter Description
attachments Indicates the attachment types, if any
conversation_id Returns the ID of the original tweet along which includes the thread of all replies
created_at Time of the tweet
geo Location from where the tweet originated, if available
lang Returns the language tag of the language detected
possibly_sensitive Returns a Boolean response indicating if the tweet was marked sensitive
public_metrics Provides the engagement metrics like retweet count and like count
Source Provides the app used for publishing the tweet

If you execute the request again, you will see the requested fields.
Alt Text

Recent Tweets search

The recent endpoint searches and fetches tweets that match the query.

Endpoint URL: https://api.twitter.com/2/tweets/search/recent

Again, We'll open a new request tab in Fiddler Everywhere and enter this API endpoint in the request bar and set the method to GET.

We need to provide authentication and query parameters. You can use the same Bearer Token used in the previous request.

In the Params tab, add a key-value pair called query. You can enter the query in the value field.

Click the Execute button to fetch the response.
Alt Text

You can view the tweets fetched in the JSON tab of the Response section. By default, the endpoint returns ten results from the last seven days. You can use the max_results parameter to indicate the required number of results.

You can use the same query parameters from above to modify the request as required.

This is the response that I received.
Alt Text

These were some of the basic operations that you can perform using the new Twitter API v2. Next time, we'll dive into how Twitter has improved the API using NLP to understand and analyze tweets and how you can implement it using the API.

Fiddler Everywhere makes debugging fun

Alt Text

If you are wondering what else you could do with Fiddler Everywhere, the tool comes in handy to inspect, debug, mock, and share network requests and responses. You can download and try it out yourself. Fiddler Everywhere is available on macOS, Linux, and Windows systems and supports all modern browsers and processes.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player