EDIT (Feb 2023): As of 09/02/2023, Twitter is closing down public access to the API and replacing it with a paid service.
Many data science projects involve interacting with an API in order to scrape data which can be used for analysis. From an academic perspective, Twitter is a popular platform for understanding people’s options and sentiments about current affairs and various other topics. It’s also incredibly useful for mining data and gaining insights. All of this is possible via access to Twitter’s API service.
In this post, we outline how to gain access to the Twitter API by generating a set of API keys. Whether you’re a data scientist, developer or hobbyist, this quick guide can help you get started.
Generating API Keys
In order to get things going, you will need to have an existing Twitter account ready. For obvious reasons, I’m not going to cover that here 🙂
Using your account, head over to apps.twitter.com where you’ll be presented with a screen similar to the following. I’ve blanked out my personal details.
Once on this page, hit the “+ New Project” button to create the API keys needed for python to access the API and stream the tweets.
On the new page, go ahead and follow the steps by creating a project name, selecting a use case (depends on what you are using it for) and creating a description.
Once this is done you’ll be presented with a screen similar to the following. Just select “Create a new App”
On the next page, select “Development”.
When asked to create a project name, you’ll need to name it something that hasn’t been named before so it needs to be fairly unique. I suggest using your username and a project name like “[MY USERNAME]_[MY APP NAME]”.
On the next screen, you’ll be presented with your API Key and secret. Make a note of this as you’ll need it later. That’s it! The main part is done!
If you plan to use the Twitter API to scrape and post tweets and access other information (which I’m assuming you do), there are a few more things you need to do to get things going.
Once you have created your initial API keys, you need to create “access tokens”. These are necessary in order to fully interact with the Twitter API and scrape data from a third-party client (e.g. Python code).
To do this make sure you have your newly created app selected on the sidebar, and select the “Keys and tokens” tab.
On the new page, scroll down to where you can see the “Access Token and Secret” box and select “Generate”.
As always, make a note of the access token keys shown to you as you won’t be able to receive them again without having to regenerate them.
What to do now?
If you’ve followed these steps correctly then you should have a set of API keys to use at the ready. With all that done, you’re now in a position to start interacting with the API via your own code. What you do from here is completely up to you.
If you’re interested in more data science projects using Twitter data, don’t forget to subscribe to this blog for more!