Explore a user’s Tweets and mentions with the Twitter API v2
Tags: beginners, twitter, showdev
Published: 2020-12-17 21:21:04

## Introduction

The user Tweet timeline and user mention timeline endpoints allow developers to retrieve the public Tweets composed by, or mentioning a user. While the [recent search endpoint](https://developer.twitter.com/en/docs/twitter-api/tweets/search/introduction) allows you to only get Tweets published in the last 7 days, the user Tweet timeline and user mention timeline endpoints allow you to retrieve Tweets and mentions that are older than the last 7 days, for an authorized user (using the user ID). Developers can use these endpoints to study topics, entities and sentiment of Tweets from a user’s timeline or mentions. In this tutorial, we will show you how to explore a user’s Tweets and mentions using the user Tweet timeline and user mention timeline endpoints.

In order to use the user Tweet timeline and user mention timeline endpoints, you will need to have a valid developer account. You will also need a [Project](https://developer.twitter.com/en/docs/twitter-api/tweets/search/introduction) created.

## A developer account

If you do not have one yet, you can [sign up for one](https://developer-staging.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api#v2-access-level).

## Create a Project and connect an App

In the [developer portal](https://developer.twitter.com/en/portal/dashboard), click create a new Project. 

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

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

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

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

![Name your app](https://dev-to-uploads.s3.amazonaws.com/i/r1brb45oqxuwkq0bq3u3.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.

![Keys and tokens](https://dev-to-uploads.s3.amazonaws.com/i/y9mhd3uspg0tzdme97sz.png)

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

**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.

## How to get the user ID for a user to use in the user Tweet timeline and user mention timeline endpoints

The user Tweet timeline and user mention timeline endpoints allow you to get Tweets using the user ID. In order to get the user ID from a username, you can use the new [user lookup endpoint v2](https://developer.twitter.com/en/docs/twitter-api/users/lookup/quick-start). Replace the USER_NAME with the username of your choice and XXXX with your own bearer token that you obtained above)
 
```bash
curl --request GET 'https://api.twitter.com/2/users/by/username/USER_NAME --header 'Authorization: Bearer XXXXXX'
```

You will see the user ID in response as shown below:

```json
{
   "data": {
       "id": "2244994945",
       "name": "Twitter Dev",
       "username": "TwitterDev"
   }
}
```

## Connecting to the user Tweet timeline and user mention timeline endpoints

In order to get the user Tweet timeline for a user ,run the following curl command in your terminal (make sure to replace the USER_ID with the user ID of your choice and XXXX with your own bearer token that you obtained above)

```bash
curl --request GET 'https://api.twitter.com/2/users/USER_ID/tweets' --header 'Authorization: Bearer XXXXXX'
```

Similarly, to get mentions for a user, run the following curl command in your terminal (make sure to replace the USER_ID with the user ID of your choice and XXXX with your own bearer token that you obtained above)

```bash
curl --request GET 'https://api.twitter.com/2/users/USER_ID/mentions --header 'Authorization: Bearer XXXXXX'
```

You will see that the JSON response for these requests contains the ID and text for the Tweets by default (example below)

```bash
{
   "id": "1334200897081987072",
   "text": "👀 If you are new to the Twitter API v2, check out this step-by-step guide to making your first request https://t.co/4rZqThpSbp"
}
```

If you want additional fields returned as part of the response (such as user information, additional Tweet fields such as context annotations etc.) then you will need to specify those fields explicitly in your response. Learn how to do this from the [guide on using fields and expansions](https://developer.twitter.com/en/docs/twitter-api/data-dictionary/using-fields-and-expansions).

You can also get these Tweets using programming languages of your choice. Check out our sample code in Python, Node (JavaScript), Java and Ruby for the user Tweet timeline and user mention timeline endpoints on our [Github repository](https://github.com/twitterdev/Twitter-API-v2-sample-code).

## Exploring the user’s Tweets

Once you know how to get Tweets using the user Tweet timeline and user mention timeline endpoints, you can start to explore ghdmtheir Tweets. For example, if you wanted to identify common named entities present in a user’s mentions, you can do the following:

In the API request, specify that you want the context_annotations object returned in the Tweet responses:
	
```bash
curl --request GET 'https://api.twitter.com/2/users/USER_ID/mentions?tweet.fields=context_annotations' --header 'Authorization: Bearer XXXXXX'
```

In the response, you will see if any named entities are present in the mentions. Here is an example:

```json
{
   "domain": {
       "id": "47",
       "name": "Brand",
       "description": "Brands and Companies"
   },
   "entity": {
       "id": "10045225402",
       "name": "Twitter"
   }
}
```

If you wanted to see which popular entities appear in your mentions, you could keep a count of popular entities by parsing each Tweet in the mentions.

If you wanted to explore the preview image URL for all Tweets in your timeline that contain media, you can do the following:

In the API request, specify that you want the preview_image_url in the tweet.media fields, and the attachments.media_keys expansions

```bash
curl --request GET 'https://api.twitter.com/2/users/2244994945/mentions?max_results=100&media.fields=preview_image_url&expansions=attachments.media_keys' --header 'Authorization: Bearer XXXXXX'
```

In the response, you will see the preview_image_url in the includes object as shown below:

```json
{
   "includes": {
       "media": [
           {
               "media_key": "16_1334657439640121344",
               "preview_image_url": "https://pbs.twimg.com/tweet_video_thumb/EoWn3rqU8AAtFWL.jpg",
               "type": "animated_gif"
           }
       ]
   }
}
```

Once you have an understanding of how to navigate a user’s Tweets, you can also use other APIs and services to do more with the Tweets. Check out this tutorial that shows you [how to analyze the sentiments of your Tweets](https://blog.twitter.com/developer/en_us/topics/tips/2020/how-to-analyze-the-sentiment-of-your-own-tweets.html). Below are some resources to keep handy when using the user Tweet timeline and user mention timeline endpoints.

## Resources
- Learn more about the [user Tweet timeline and the user mention timeline endpoints](https://developer.twitter.com/en/docs/twitter-api/tweets/timelines/introduction).
- Check out the [API reference for the user Tweet timeline endpoint](https://developer.twitter.com/en/docs/twitter-api/tweets/timelines/api-reference/get-users-id-tweets) to learn more about what’s available.
- Check out the [API reference for the user mention timeline endpoint](https://developer.twitter.com/en/docs/twitter-api/tweets/timelines/api-reference/get-users-id-mentions) to learn more about what’s available.
- Get inspired by reading our other [tutorials](https://developer.twitter.com/en/docs/twitter-api/tutorials).