Spike — Twitter API

Ilyas O.
4 min readNov 21, 2022

--

A Spike about the Twitter API, with the recommended solution to choose.

The Twitter API can be used to retrieve and analyze Twitter data, it has different APIs and the limit is always an option.

Twitter API versions

There are multiple Twitter API’s such as Premium v1.1, but in this article we will focus more on the FREE ones that can be used by any Developer without paying extra money.

  • Twitter v2 : is the latest version, it contains more new and advanced features compared to v1.
  • Twitter v1 or Standard v1.1 : is the first Twitter API, and it’s offering limited features compared to v2.

→ Always try to use the latest and stable versions, as they have more new features and there might be always the possibility of the old ones becomes INACTIVE or DEPRECATED.

Authentication

There are two main methods to authenticate your Twitter App, by using:

  • OAuth 1.0 : is an Authorization flow that helps access to private account information on behalf of a Twitter account.
  • OAuth 2.0 : This Authorization flow is used for read-only access, like searching for public tweets, the endpoint will be accessible by a Bearer token.

If you want to access private data of a particular user, or getting tweets of the people you follow, you will need another authorization flow.

OAuth 2.0 Authorization Code Flow with PKCE
OAuth 2.0 Authorization Code Flow with PKCE

→ OAuth 2 is the best Authorization flow to use, as it’s recommended by Twitter and also it’s simple to use, especially if you want to get public twitter data.

Twitter API limits

Actually, there are so many types of rate limits, it depends on the Authorization Flow that we discussed before as well as the endpoints that we are going to use.

For example, if we are going to use OAuth 2 with the endpoint Recent search then, the limit will be 450 requests over any 15-minutes.

That’s pretty enough time for most developers, especially if you are building a Twitter Bot that will get Tweets each X minutes/seconds.

For more details — https://developer.twitter.com/en/docs/twitter-api/rate-limits#v2-limits

Setup Twitter Developer Account

In order to use the Twitter API, You need first to create a Twitter Developer account:

  • Firstly, create a Twitter account, if you don’t have one.
  • Create a Twitter Developer Account.
  • Go to your Developer Portal and Create a new App.
  • Once you create your App, you will be able to get Bearer Token that we are going to use in our endpoints.

After you finish the steps described above, you will have access to your Keys and Credentials, like shown in the picture below.

(The App name is ‘Crypto-Test-1’)

Developer Portal — Keys and tokens of the selected App
Developer Portal — Keys and tokens of the selected App

Demo

We are going to use the endpoint /v2/tweets/search/recent to get the public recent tweets by using the Twitter API v2.

You will need to provide the Bearer Authorization token in your Request Header, or directly from the Authorization tab if you are using Postman.

Example

Twitter v2 REST call
Get public recent tweets

If you prefer not wasting time over this, you can run and try the endpoint directly from the Twitter Developer Documentation. You can even try a live request.

Solution to choose

There are multiple Libraries that saves you time using the Twitter API in your project, such as libraries built with Python, R, …ect

In this section, I will focus on the Java side, as I’m a Java Developer. In fact, there is already an unofficial Java library for the Twitter API, called Twitter 4J.

It’s a good option to use Twitter 4J or any kind of Library, if you are going to use it’s full potential. If not, then use and call the Twitter API directly in your project.

When you use an external library, your project will be always depending on it. What will happen, if one day the X Library decided to stop supporting java 17, while your team is trying to migrate to this version?

So, like I mentioned above, if you believe that you only need to work with 2 or 3 endpoints without much complexities, there is no point of using the library. Just consume the Twitter API directly in your project. This applies for any REST API.

Always think of the future cases and choose the solution that will give you more CONTROL and effective MAINTAINABILITY for your project.

I hope you find this article helpful.

--

--