Step-by-step guide to making your first request to the new Twitter API v2
Tags: twitter, showdev, beginners
Published: 2020-12-02 17:45:54

Earlier this year, Twitter announced [early access to the new Twitter API v2](https://twittercommunity.com/t/announcing-early-access-to-the-next-generation-of-the-twitter-api/139612). The new API provides exciting new features such as:
- Improvements to the response Tweet payload
- Support for getting Twitter polls data in the API
- Tweet annotations (that provide contextual information about a Tweet and named entities in a Tweet)
- Support for retrieving entire conversation thread using conversation_id parameter in search

In this article, I will show you how you can get started quickly with the new Twitter API v2.

## Step 1: Sign up for a developer account

In order to get started with the new Twitter API, you need an a developer account. If you do not have one yet, you can [sign up for one](https://developer.twitter.com/en/portal/petition/essential/basic-info). Once you have an approved developer account, proceed to the next step.

## Step 2: Enroll in the new developer portal experience

Next, set up your [project](https://developer.twitter.com/en/docs/projects/overview). Projects allow you to organize your work and access with the new Twitter API.

## Step 3: Create a project and connect an app

Next, in the [developer portal](https://developer.twitter.com/en/portal/dashboard), create a new project. 

![Create a project](https://dev-to-uploads.s3.amazonaws.com/i/j33nl7qvumn3e74bd3f7.png)

Give it a name, select the appropriate use-case, provide a project description. Next, you can either create a new [app](https://developer.twitter.com/en/docs/apps/overview) or connect an existing app (an app is a container for your API keys that you need in order to make an HTTP request to the Twitter API).

![Add an app](https://dev-to-uploads.s3.amazonaws.com/i/3cmxpqq99nrx8hrcd00g.png)

Click ‘create a new App instead’ and give your app a name in order to create a new app.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ln5geka3wedsibgjwi30.png)

Once you click complete, you will get your API keys and the bearer token that you can then use to connect to the new endpoints in the Twitter API v2.

![App Keys](https://dev-to-uploads.s3.amazonaws.com/i/nbzu5rn2zadn03262amo.png)

Click the **(+)** next to API key, API secret key and Bearer token and copy it in a safe place on your local machine, you will need these to make the API calls in the next step. 

<em>**Note**: The keys in the screenshot above are hidden, but in your own developer portal, you will be able to see the actual values for the API key, API secret key and Bearer token.</em>

## Step 4: Make an HTTP request to one of the new endpoints

Now that you have your API keys and bearer token, you are ready to make your first API call to the new Twitter API v2. In this example, we will call the [recent search endpoint](https://developer.twitter.com/en/docs/twitter-api/tweets/search/introduction) in the new Twitter API v2. If you are familiar with the Twitter API v1.1, you may have used the [search/tweets](https://developer.twitter.com/en/docs/twitter-api/v1/tweets/search/api-reference/get-search-tweets) endpoint which let you search for Tweets from the last 7 days. The recent search endpoint is the replacement in v2 of this search/tweets endpoint from v1.1.

We can call the recent search endpoint using one of the following approaches:

### 1. Using curl
The first approach is to make the request in your terminal using curl. The example below shows how you can get Tweets in the last 7 days from the [TwitterDev account](https://twitter.com/TwitterDev) using the recent search endpoint. Just **replace the XXXXXX** in the request below with your own bearer token (obtained from step 3 above), and paste the snippet in your terminal. You will get the json response for this request. 

```bash
curl --request GET 'https://api.twitter.com/2/tweets/search/recent?query=from:twitterdev' --header 'Authorization: Bearer XXXXXX'
```

Once you are comfortable with making this API call, you can learn more advanced concepts such as modifying the query (in the request above) to get data using other keywords and operators, along with how to get additional information in your response (such as user object, place information, polls data etc.) by exploring [the documentation for the recent search endpoint](https://developer.twitter.com/en/docs/twitter-api/tweets/search/introduction).

### 2. Using sample code available on Github 
Alternatively, you can use one of the [code samples available on our Github](https://github.com/twitterdev/Twitter-API-v2-sample-code) to make your first request to the Twitter API v2. Currently, we have code samples available in Python, Javascript (Node), Java, Ruby. In this article, I will describe how to use the [sample code for the recent search endpoint](https://github.com/twitterdev/Twitter-API-v2-sample-code/tree/master/Recent-Search) in Python.

1. First, you will have to [download the code samples](https://github.com/twitterdev/Twitter-API-v2-sample-code/archive/master.zip) from Github. Alternatively, you can also clone this repository in Github Desktop if you have it setup. 
2. Navigate to the [recent_search.py](https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/master/Recent-Search/recent_search.py). Make sure you have the requests library installed. If not, install it by running: `pip install requests` in your terminal.
3. Copy your bearer token (obtained in step 3) and set in your environment variable by running: `export 'BEARER_TOKEN'='<your_bearer_token>'` in your terminal. 
4. Run the [recent_search.py](https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/master/Recent-Search/recent_search.py) file by running `python3 recent_search.py`

This will run the script and give you Tweets in the last 7 days from the [TwitterDev account](https://twitter.com/TwitterDev). You can modify the query in the [recent_search.py](https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/master/Recent-Search/recent_search.py) file and also specify additional fields you want returned in your JSON response.

### 3. Using Postman
Yet another way of making your first request to the new Twitter API is using [Postman](https://www.postman.com/downloads/). Postman is a tool that lets you make HTTP requests using a graphical user interface, by easily specifying the request URL, parameters, headers etc. 

For this example, I will show you how you can use Postman to call the recent search endpoint.

 - [Download the Postman client](https://www.postman.com/downloads/) for your machine.
 - Open Postman. Click on the **+ New** button and select request.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/c0way7kqb43l13eq3u0o.png)

 - Give the request a name. Click create collection and give it a name. Then click **Save**.
 - Enter the following in the 'Enter a request URL' section: `https://api.twitter.com/2/tweets/search/recent?query=from:twitterdev`

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/c0i8h15r6al77bu8usej.png)

 - Navigate to Headers and add the key as **Authorization** and value as **Bearer XXXXX** where XXXXX is the value of your bearer token from step 3.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/81h2ncne64r4wqljed0c.png)

 - Click Send and you will see the response for your request.

Check out the [Postman collection for the Twitter API v2](https://documenter.getpostman.com/view/9956214/T1LMiT5U) that you can run directly in Postman for other endpoints available in the Twitter API v2 as well. You will need the keys obtained in step 3 in order to configure the Postman collection.

### 4. Using select libraries
You can also use some of the libraries that support the new endpoints that are part of the new Twitter API. Check out our [tools and libraries page](https://developer.twitter.com/en/docs/twitter-api/tools-and-libraries) and look for libraries in your favorite programming language. Look for the libraries that support the **v2** of our API.

## Wrap-up
You can use one of these approaches mentioned above to quickly make your first request to the new Twitter API v2. This will enable you to try out the new endpoints and features that are part of the new Twitter API. 

**If you build anything with the new Twitter API, please share it with us on [this forum](https://twittercommunity.com/t/share-what-you-built-with-the-new-twitter-api-v2/146415), and if you are one of the first 10 developers to share what you’ve built with the new Twitter API, we may send some cool swag your way!**

This is just the beginning. We will continue to add new endpoints to the Twitter API v2. To see what's coming, checkout our [product roadmap](https://trello.com/b/myf7rKwV/twitter-developer-platform-roadmap).

Below are some resources that will help you get started with the new Twitter API. Reach out to me on Twitter [@suhemparack](https://twitter.com/suhemparack) with feedback or questions.

## Resources
- [Video technical overview of the new Twitter API v2](https://twitter.com/TwitterDev/status/1296493468345413637)
- [Twitter API v2: Early Access Guide](https://developer.twitter.com/en/docs/twitter-api/early-access)
- [The Academic Research take on Early Access to the new Twitter API v2](https://twittercommunity.com/t/the-academic-research-take-on-early-access-to-the-new-twitter-api/139697)
- [Migrate to the new Twitter API](https://developer.twitter.com/en/docs/twitter-api/migrate)
- [Twitter API v2: Early Access Tutorials](https://developer.twitter.com/en/docs/twitter-api/tutorials)