Twitter API Setup#

To collect Twitter data, we’re going to work with the Twitter API and twarc2, a Python package for collecting Twitter data through the Twitter API.

To use the Twitter API and twarc, we first need to complete the following steps:

  1. Apply for a Twitter developer account, which also requires making or already having a regular Twitter account

  2. Get Twitter API keys

  3. Install twarc

  4. Configure/set up twarc

The following instructions will help guide you through this process.

Apply for a Twitter Developer Account#

If you are an academic researcher (e.g., “post-doc, professor, fellow, master’s student working on a thesis, PhD candidate, or affiliated with an academic institution AND have a clearly defined project”), you can apply for the Academic Research track of the Twitter API, which enables access to all Twitter data.

If you are an undergraduate student or not affiliated with an academic institution, you can apply for a standard developer account, which enables access to tweets from the last week as well as tweets in real time.

Get API Keys and Bearer Token#

Once you’ve applied for a developer account and had it approved by Twitter, you need to generate your API keys and bearer token — the special passwords that will allow you to work with the Twitter API. To generate your API keys and bearer token, you can follow these instructions from Suhem Parack, the Developer Advocate for Academic Research at Twitter.

After you generate your API keys and bearer token, you should record them in a secure location on your computer (do not share them on the internet because these are private like passwords!).

Install twarc2#

To install twarc2, you can run the following from command line:

!pip install twarc --upgrade

Configure/Set up Twarc#

Once twarc is installed, you need to configure it with your API keys and/or bearer token so that you can actually access the API.

To configure twarc, you can run the following from the command line:

!twarc2 configure
Please enter your Bearer Token (leave blank to skip to API key configuration): ^C

Twarc will ask for your bearer token, which you can copy and paste into the blank after the colon, and then press enter. You can optionally enter your API keys, as well.

If you’ve entered your information correctly, you should get a congratulatory message that looks something like this:

Your keys have been written to /Users/melwalsh/Library/Application Support/twarc/config


    Happy twarcing!   

Now you’re ready to collect and analyze tweets!