A guide to teaching with the Twitter API v2
Tags: beginners, api, twitter
Published: 2022-04-04 17:54:24


Are you an educator looking to use Twitter API in your course? In this guide, you will learn:

* What is the Twitter API
* Why do educators use Twitter API in their course
* What type of data is available in the Twitter API
* What type of classes are being taught using the Twitter API
* Available access levels and which ones should your students choose
* How can students get keys and tokens to connect to the Twitter API in their code
* How to download Twitter data about a topic in CSV or JSON format without writing code
* What tools and resources are available for students to learn about the Twitter API

**Note:** In this guide, wherever I use the term *Twitter API*, I am  referring to the [Twitter API v2](https://twitter.com/TwitterDev/status/1460323737035677698) only which is the primary API. We will not be using the old Twitter API v1.1.

## Introduction

Twitter is a platform that is used by people across the world to exchange thoughts, ideas and information with one another, using Tweets. Each Tweet consists of up to 280 characters and may include media such as links, images and videos. The Twitter API provides a programmatic way to retrieve Twitter data.

Professors at various schools around the world use Twitter API in their class to teach students:

* data science, text mining, machine learning etc.
* how to write code
* how to work with APIs
* how to work with real-world social media data

The data from the Twitter API is available in [JSON format](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/JSON). An example of how a Tweet on Twitter is represented in JSON format is shown below:

![A Tweet on Twitter](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bjskvsavn4y5ufvvnbtb.png)

![JSON representation of the Tweet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hjnciw1kmd4supdl1ws0.png)

### Examples of courses that use the Twitter API

* [Introduction to Cultural Analytics & Python](https://melaniewalsh.github.io/Intro-Cultural-Analytics/welcome.html) by [Melanie Walsh](https://twitter.com/mellymeldubs)
* [Learn how BitProject uses the Twitter API to teach non-technical students how to code](https://developer.twitter.com/en/community/success-stories/bitproject).

## Applying for access to the Twitter API

In order to use the Twitter API, students need to get their keys and tokens. In order to do so, they need to first apply for the appropriate Twitter API access. Currently, the Twitter API v2 has 3 access levels:

### Essential access

This is the **fastest and recommended way for students in a classroom** to get started with the Twitter API. Students can simply sign up for a developer account [here](https://developer.twitter.com/en/portal/petition/essential/basic-info) and they will get instant access to the Twitter API v2 without waiting for a use-case review. This access level lets them retrieve up to **500,000 Tweets** per month from the Twitter API v2. When using search functionality in the Twitter API, they can get Tweets from the last 7 days in this access level.

### Elevated access

If a student in your class needs more than 500,000 Tweets per month, then they can apply for elevated access [here](https://developer.twitter.com/en/portal/products/elevated). This does require a use-case review, which is why it takes longer to get this access. This access level lets them retrieve up to **2 million Tweets** per month from the Twitter API v2. When using search functionality in the Twitter API, they can get Tweets from the last 7 days in this access level.

### Academic access

If your students are part of a Masters or PhD program and have a valid research use-case, they can apply for academic access [here](https://developer.twitter.com/en/portal/petition/academic/is-it-right-for-you). This does require a use-case review, which is why it takes longer to get this access and students have to provide additional documentation. This access level lets them retrieve up to **10 million Tweets** per month from the Twitter API v2. When using search functionality in the Twitter API v2, they can get any public Tweet from the full archive of public Tweets dating back to 2006. They can also get advanced search operators (such as geo) that let them filter the data they want returned, more precisely.

## How to get your API Keys and tokens

The steps for getting the API keys and tokens vary based on the access levels. Below are the instruction for each access-level:

### Getting keys and tokens for essential access

First, you need to apply for [essential access](https://developer.twitter.com/en/portal/petition/essential/basic-info). You will be asked to verify your email as part of this process. Additionally, you need to have phone number registered with your account in order to use the API. 

After you have created your account, you will be asked some basic questions:

![Questions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p84s7kx97yoiphvpe4ac.png)

Review and agree to the developer agreement and policy

![Developer Policy](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/doav0iupsyn10903czio.png)

Next, you will be asked to verify your email

![Verify Email](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zr9t80e7jy41e45wgiif.png)

You should received an email that looks like this:

![Confirm email](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0mnzon4isve8fid3mwv.png)

Once you click confirm, you will be directed to the developer portal. You can create an app by giving it a name

![Create App](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l70cdza42hf2s6vvbzdp.png)

Once you click Get Keys, you will be able to see your keys and bearer token. Please keep those in a safe place, as you will be using those to connect to the Twitter API v2.

![Keys & Tokens](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jytebrz5ay8smpwaicex.png)

### Getting keys and tokens for elevated access

Complete steps for getting keys and tokens for academic access can be found [here](https://github.com/twitterdev/getting-started-with-the-twitter-api-v2-for-academic-research/blob/main/modules/4-getting-your-keys-and-token.md#getting-keys-for-the-standard-product-track)

### Getting keys and tokens for academic access

Complete steps for getting keys and tokens for academic access can be found [here](https://github.com/twitterdev/getting-started-with-the-twitter-api-v2-for-academic-research/blob/main/modules/4-getting-your-keys-and-token.md#getting-keys-for-the-academic-research-product-track)

## What if my students just want Twitter data without writing any code?

Sometimes professors don't want their students spending too much time writing code to get Twitter data using the API. Twitter recently launched the [Downloader tool](https://developer.twitter.com/apitools/downloader) that lets researchers specify the search query for which they want the data, the time period and the format in which they would like the data i.e. JSON or CSV. Once specified, they can download the data and use for their assignments and projects. Learn more about how to write search queries [here](https://github.com/twitterdev/getting-started-with-the-twitter-api-v2-for-academic-research/blob/main/modules/5-how-to-write-search-queries.md)

**Step one:** Enter your academic bearer token

![Enter bearer token](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6k6hsgfowfmc83nimygh.png)

**Step two:** Create a new download

![Create Download](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3tktraqm7xiuivbepxn2.png)

**Step three:** Specify your query, time range and download format

![Query](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yisb5oml4hxb0qfog7ka.png)

**Note: The downloader tool currently works for academic access only.**

## Tools and resources for students to get started with the Twitter API v2

There are a lot of resources available to students looking to get started with the Twitter API v2. Some of those are listed below:

### Libraries and packages

If you are looking to use the Twitter API v2 in Python, two popular libraries that you can use:

* [Tweepy](https://docs.tweepy.org/en/latest/client.html)
* [Twarc](https://twarc-project.readthedocs.io/en/latest/api/library/)

If you are looking to use the Twitter API v2 in R, you can use the [academictwitteR](https://cran.r-project.org/web/packages/academictwitteR/vignettes/academictwitteR-intro.html) package.

Twitter also has its own first-party libraries in [Java](https://github.com/twitterdev/twitter-api-java-sdk) and [Typescript](https://github.com/twitterdev/twitter-api-typescript-sdk)

### Getting started with academic research with the Twitter API v2 (course)

Twitter has a dedicated course on [getting started with academic research using the Twitter API v2](https://github.com/twitterdev/getting-started-with-the-twitter-api-v2-for-academic-research). This course includes plug-n-play code samples and cheatsheets on writing search queries etc.

### Videos

* [A detailed video on working with the Twitter API v2in Python using Tweepy](https://www.youtube.com/watch?v=0EekpQBEP_8)
* [Technical overview of the Twitter API v2](https://www.youtube.com/watch?v=VOWgSRfToAI)
* [Understanding the new Tweet payload structure](https://www.youtube.com/watch?v=QYVip-Qf_2I)

### Blogs and articles

* [Step-by-step guide to making your first request to the new Twitter API v2](https://developer.twitter.com/en/docs/tutorials/step-by-step-guide-to-making-your-first-request-to-the-twitter-api-v2)
* [A comprehensive guide for using the Twitter API v2 with Tweepy in Python](https://dev.to/twitterdev/a-comprehensive-guide-for-using-the-twitter-api-v2-using-tweepy-in-python-15d9)
* [Year in review 2021: Academic Research with the Twitter API v2](https://dev.to/suhemparack/year-in-review-academic-research-with-the-twitter-api-v2-52mm)
* [Building visualizations with Tweet counts from the Twitter API v2 in Python](https://dev.to/suhemparack/building-visualizations-with-tweet-counts-from-the-twitter-api-v2-in-python-1nb0)
* [Getting started with R and v2 of the Twitter API](https://developer.twitter.com/en/docs/tutorials/getting-started-with-r-and-v2-of-the-twitter-api)
* [Introduction to Twitter data processing and storage on AWS](https://dev.to/twitterdev/introduction-to-twitter-data-processing-and-storage-on-aws-1og)
* [Translating Tweets from the Twitter API v2 using AWS Amazon Translate in Python](https://dev.to/suhemparack/translating-tweets-from-the-twitter-api-v2-using-aws-amazon-translate-in-python-1nj0)

### Twitter Community Forums

If students have any questions about the Twitter API, they can ask on the [Twitter Community Forums](https://twittercommunity.com/).

### Community datasets

Many researchers share their research datasets (Tweet IDs) on Github etc. An example of such a dataset is the [COVID-19-Dataset](https://github.com/echen102/COVID-19-TweetIDs) from [Emily Chen](https://twitter.com/thatmchen). Some professors have their students hydrate such datasets using a tool like [DocNow's Hydrator](https://github.com/DocNow/hydrator) and use those in their classroom.

## Final thoughts

I hope this guide is helpful in understanding how you can teach with the Twitter API in your classroom. If you teach a course that uses the Twitter API and have any questions, feel free to reach to me on Twitter [@suhemparack](https://twitter.com/suhemparack) and please share what course you are teaching!