Searching Tweets Using Twitter API
Tags: python, api, http, twitter
Published: 2021-09-03 15:22:08

Disclaimer: this is the English version of my work which is also available in Brazilian Portuguese, so, if you prefer, you can access that version by [clicking here](https://dev.to/lisandramelo/buscando-tweets-com-a-api-do-twitter-3g1b).

## Creating your Developer Account

The Twitter API is a tool provided by Twitter that allows real-time access to what is being tweeted by its users. To access it, you will first need to have a developer account; you will need to request one, [click here](https://developer.twitter.com/en/apply-for-access ) to go to the request page.

Once you gain access to the Developer Portal, you will notice a dashboard like the one below.

![Developer Portal Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z4t3zm71k6rnp2fixkha.PNG)

Now to connect to an API endpoint, you will need to create a project and an App. To do it, click on `Create New Project` and fill in the required information for the project and the App creation. After filling out the entire form, you will see a screen with the authentication keys of your application; they will be necessary when connecting to the API.

![App Authentication Keys](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lsovupi1fpysoc3wdnzr.PNG)

Now you have everything needed to connect to the API!

## API Connection

It is possible to use several different technologies to create the API connection, but we will use **Python** and some of its libraries.

First, about the libraries that will be used, there will be three: [Python Requests](https://docs.python-requests.org/en/master/), [json](https://docs.python.org/3/library/json.html#module-json) and [Python Decouple](https://github.com/henriquebastos/python-decouple). *Python Requests* will be responsible for carrying out the **HTTP** requests, *json* will help handle the responses obtained for the .json format, and the *Python Decouple* library will assist in the separation of the authentication from what is in the code.

So, first, we will import the required libraries.

```python
from decouple import config
import requests
import json
```

After that, we must import the authentication value into the code for that, we will create, in the same directory of our file, a file with the name `.env`, and inside it, we will add the following information.

```text
BEARERTOKEN=BearerTokenValue
```

After the file creation, it is now possible to use the Bearer Token authentication value without adding sensitive information to the code. To do this, we'll add the following line of code to our `.py` file.

```python
bearer_token = config('BEARERTOKEN')
```

The `bearer_token` variable will now hold the value of your Bearer Token.

Now, we will create a function that adds authentication in the header of the **HTTP** request.

```python
def set_auth(ob_auth):
    objeto_para_auth.headers["Authorization"] = f"Bearer {bearer_token}"
    return ob_auth
```

After that, we can carry out the request process for the API. We will define our search parameters and API endpoint.

```python
search_url = "https://api.twitter.com/2/tweets/search/recent"
str_busca = f'to:{profile} {keyword} lang:{language}'
query_params = {'query': str_aux,'tweet.fields': 'attachments,author_id,created_at,lang,public_metrics,source','max_results':limit_tweets}
```

In the code above, the `search_url` variable receives the endpoint that will be used (you can read a little more about the endpoints available by [clicking here](https://developer.twitter.com/en/docs/api-reference-index)). Furthermore, the str_busca variable receives the search string in the case of the example the search will be for tweets sent to a profile, containing a search key and written in a specific language, you can found other search filters [here](https://developer.twitter.com/en/docs/twitter-api/v1/rules-and-filtering/search-operators). Lastly, the query-params receives the search string, the fields that will be present in the responses (see more options by [clicking here](https://developer.twitter.com/en/docs/twitter-api/fields)) and the number of tweets returned in the search.

After the previous definitions, we can perform the request for that we will use the commands below.

```python
response = requests.get(search_url, auth=set_autenticacao, params=query_params)

if response.status_code != 200:
	raise Exception(response.status_code, response.text)

r = response.json()
print(json.dumps(r, indent=4, sort_keys=True))
```
Now you can see your tweets obtained based on your search!

The program developed in this tutorial is available in [my GitLab repository](https://gitlab.com/veronicamars73/twitter-api-search-tutorial).