How to import data from Twitter into Google Sheets using the Twitter API

There’s a lot of different ways to export data from Twitter to Google Sheets. The best way is to use the Twitter API.

In this tutorial, you will learn how to connect Twitter to Google Sheets in 7 steps:

  1. Install the Apipheny Add-on
  2. Create a Twitter App
  3. Create a project and add your app to this project
  4. Generate a Bearer Token
  5. Choose your Twitter Endpoint URL
  6. Enter Twitter API Request into Apipheny
  7. Run the Twitter API Request

Step 1.) Install and open the Apipheny add-on for Google Sheets

Apipheny is a free API connector for Google Sheets. You can use Apipheny to connect your Google Sheets to API data sources, easily.

1.) Install Apipheny by opening the following link on desktop and then clicking the Install button in the Google Marketplace: https://apipheny.io/install

2.) After you’ve installed Apipheny, open a Google Sheet and then click on the Extensions dropdown in the menu at the top.

In the dropdown list you should see Apipheny. Click Apipheny then click Import API to open the Apipheny sidebar in your Google Sheet.

open apipheny

Tip: you can open a new Google Sheet by entering this URL in your browser: sheet.new

Step 2.) Create a Twitter App

First, make sure you’re logged in to your Twitter account.

Then, in your Twitter developer account’s dashboard, click on the Projects and Apps -> Overview menu item on the left side of the page. Then, on the bottom of the page, click the +Create App button:

Projects & Apps section
Projects & Apps section

Choose a name for your app, then click the Next button:

Name your App section
Name your App section

In the Keys & Tokens tab of your app, you will find the API Key and API Key Secret. Copy it to a safe location, because you’ll need it later:

Keys and Tokens section
Keys and Tokens section

Step 3.) Create a project and add your app to this project

In your Developer Portal click Projects & Apps -> Overview, then click the +Add Project button:

Add a project
Add a project

Choose a name for your project, then click the Next button:

Name Your Project section
Name Your Project section

Choose from the drop-down box how you intend to use the Twitter developer platform, then click the Next button:

Usage of Twitter developer platform
Usage of Twitter developer platform

Write a description of your project, then click the Next button:

Project description
Project description

Click the Add an existing App button, to add your app to this newly created project:

Add an existing app to your project
Add an existing app to your project

Choose the app you created at Step 2.) from the drop-down box, then click the Next button:

Choose an existing app
Choose an existing app

Choose the environment for your app. We choose Development. Then, click the Next button:

Choosing app environment
Choosing app environment

Your app is now added to your project:

App added to the project
App added to the project

Step 4.) Generate a Bearer Token

In a command prompt window for Windows OS or a terminal for Mac OS, enter the following command and press the Enter key to run the command:

curl -u "your_API_key:your_API_secret" --data "grant_type=client_credentials" "https://api.twitter.com/oauth2/token"

The API Key and API Key Secret are the values you saved on Step 2.).

Note: For Mac OS you may need to use single quotes ( ' ) instead of double quotes ( " ).

Don’t forget to replace your_API_key and your_API_secret with real values that you obtained above.

Running command for Bearer token
Running command for Bearer token

Your Bearer token will then be displayed in the response.

Copy and paste your Bearer token somewhere safe because you will need it in the next steps.

Note: If the token/key that you generate has an expiration time, you will need to complete this same process again to get a new token when the old one expires.

Step 5.) Choose your Twitter API endpoint

In this section, we’ll show you how to browse the Twitter API documentation to find an API URL endpoint that retrieves the data you need from Twitter, if the data you want is available from the Twitter API. If the data you need is not available via the API, then you’ll need to use another method such as scraping.

You can check if the data you need is available from the Twitter API by checking the API documentation to see what endpoints are available. An endpoint is the last part of the API URL that tells the Twitter API what specific data to return back to you.

If you already know the Twitter API base URL and endpoint that you need, or you want to use the same example URL as us, just skip to Step 4.

To browse the available Twitter endpoints, first open the Twitter API documentation page: https://developer.twitter.com/en/docs/api-reference-index

The main section of this page contains a list of categories for the available APIs, each with their endpoints:

Twitter API documentation

Clicking on an endpoint link opens the dedicated page that shows the endpoint’s details.

For example, if you click the endpoint GET /2/lists/:id/tweets, the dedicated page will show the specific documentation that contains the HTTP method used (eg. GET), a short description of the endpoint’s purpose, the endpoint’s URL path, Authentication and rate limits section, OAuth 2.0 scopes required by this endpoint, a description of the available parameters and an example request and response:

Twitter API endpoint documentation example

All API calls to Twitter should be made to the https://api.twitter.com/2/ base URL, and all endpoints and parameters should be appended to this base URL:

https://api.twitter.com/2/

In the next step, we show an example API request to Twitter from Google Sheets with a completed API URL.

Step 6.) Enter your Twitter API request into the Apipheny add-on

Now here’s the last step. Go back to your Google Sheet and make sure that the Apipheny add-on is open on the “Import” tab. With the Import Tab open, enter these details into the add-on:

Method: At the top of the Apipheny sidebar, select the HTTP method (GET or POST) required by your API endpoint. For this example, we are using the GET method.

API URL: In Step 3, we explained how you can find the Twitter REST API endpoint that you need. Now copy the complete URL into the Apipheny add-on, where it says API URL Path, followed by any GET parameters required for your query.

For this example, we are using the GET /2/users endpoint, that returns a variety of information about one or more users specified by the requested IDs.

The complete API URL with parameters for this endpoint is:

https://api.twitter.com/2/users?ids=ID1,ID2

You can copy and paste the above example API URL into Apipheny and try it out. Just replace ID1, ID2 and with the IDs of any valid Twitter users.

Headers: In the Headers section of the Apipheny add-on, add one row with the following key and value:

Headers
Key:
Authorization
Value:
Bearer your_access_token

If you’re following this example, just copy “Authorization” into the Key field and “Bearer your-access-token” into the Value field.

your_access_token is the value you generated previously, in Step 3. There should be a literal space between “Bearer” and “your_access_token”.

Here’s our Twitter API request entered into Apipheny, with the API URL and headers:

Twitter API request in Apipheny add-on

Step 7.) Run the Twitter API Request in your Google Sheet

Finally, click the Run button at the bottom of the Apipheny add-on and then wait for the Twitter API data to be imported into your Google Sheet, like this:

Twitter API data in Google Sheets

After making a successful request to the Twitter API, try querying a different endpoint, or try changing the parameters in the API URL. You could also try using one of the more advanced features in the Apipheny add-on, such as:

  • Save and schedule your Twitter API request
  • Make a POST request to the Twitter API (if available)
  • Use the custom =APIPHENY() function to call the Twitter API request inside your spreadsheet, from a specific cell
  • Create a Twitter API request based on the value of a cell by referencing the value of the cell in the API URL with three curly braces eg. {{{Sheet1!A1}}}


Popular API Tutorials


API Knowledge

What is an API?

What is an API URL?

What are parameters?

What is an endpoint?

What is an API key/token?

What is basic authentication?

What are headers?

What is a GET request?

What is a POST request?

Import JSON to Google Sheets


SEO by SearchCyrus