How To Build a Twitter "Hello World" Web App in Python

As the documenters of the API economy, ProgrammableWeb is producing a series of tutorials – the “Hello World of APIs” – that demonstrates how to integrate an application with the most significant API providers in the industry, using a variety of the programming languages to achieve it. Each tutorial will walk through the steps a developer will undertake in understanding the API in question and ultimately an example of an application to call the API. 

This tutorial will walk through the steps required to implement a Web application in Python that makes calls to the Twitter REST API; we are using Twitter as the first example given its huge popularity as a social networking service and the fact that many apps and websites integrate with the Twitter API to add social or near real-time functionality. During the course of the tutorial the following topics will be addressed:

  • Application Infrastructure Choices – Why Python?
  • Understanding the API – API types, security, etc.;
  • Sending the request – A simple example of making an API call;
  • Delivering the requests – A simple web application.

By the end of the tutorial a developer familiar with Python should have sufficient knowledge to be able to build a application of his or her own that integrates with the Twitter REST API.

Application Infrastructure Choices – Why Python?

For this tutorial we will be using Python to build our web application (hosted for reference on Github). In another article we discuss the justifications and implications for using Python so please refer to that tutorial for the full discussion.

To support this tutorial we built a working web application that consumes the Twitter API using Flask, a Python-based micro framework. We also used four packages to help write our sample application efficiently:

  • Flask Bootstrap, a Flask-based implementation of Bootstrap that helped to minimize the amount of HTML and CSS we needed to construct to give us a working Web application.
  • Jinja, a HTML-templating utility that allow us create simple templates where we did need to write HTML (for the web screens discussed below).
  • WTForms, a package for easily creating and validating web forms in Flask.
  • oauthlib, which simplifies implementing the OAuth dance by wrapping the HTTP calls made to an OAuth provider; we followed some (but not all of) the sample flow detailed the main page of their GitHub pages
Chris Wood Chris is a freelance API specialist. His main interests are API management, Python and doing interesting stuff. Connect with Chris on Twitter




Hi, I have the consumer key and secret and would like to get access token and secret without manual intervention of user. In your code the user is directed to url and he has to manually grant access. Is there any way to code just to get token without the 3-way manual dance. For example i provide the consumer key and secret and i get back access token.


Morning. Unfortunately if your application needs to perform an action on behalf of a user you are obliged to use the 3-legged OAuth dance unless you are accesses your data from your account (see here for single user authorization: This link shows the type of authorization required for given scenarios.

You could always fudge this of course, but that kind of depends on having access to the user name and password of the user you are doing something on behalf and using something like Selenium to respond to the Twitter prompts like you are in a browser. Not hard but also not for the faint hearted.

Good luck!


Hi, this is very useful, however when I implement it twitter responds with a key that I have to provide to the App. I guess the problem is that I am not providing a callback URL, how do I say so? You say that twitter should redirect to that callback URL, but I don't see anywhere in the code where that URL is provided. Thanks in advance


Hi!, very useful post, thanks you very much. I am having a problem following it, once I authorize twitter I am getting a PIN that should be provided to the App. I guess this is because I did not provide a callback URL, however I don't see anywhere in your code where this callback is provided. Thank you very much


Hello and thanks for your question.

You've pulled the GitHub repo I provided in the comments above?

This is a working solution that you 'should' be able to run (once you've got the required packages install). The callback URL is dealt with by the following Flask route:


def handle_callback():

    global ACCESS_TOKEN


For this Flask app to work correctly your application config on Twitter will need to know about the callback URL to redirect the user.


Hi thanks, yeah I got the files from the GitHub repo, but twitter just displays a PIN which I am supposed to use to authorize my App. Anyway I got it to work using that PIN to authorize the App and then I receive a token. The problem is that the flow is kind of awkward for the user, because he needs to go back to the App and type that PIN. I still don't see how is twitter supposed to know what the URL for the callback URL is. I thought I was supposed to send that information to the twitter API when I call I actually tried to add a parameter oauth_callback as explained in, but that didn't work. Anyway, again, this post has been really useful for me.

PD: ok sorry, I just read the last sentence "For this Flask app to work correctly your application config on Twitter will need to know about the callback URL to redirect the user". I guess I will try that, thanks.




Ok, it did work, thank you very much. I just configured my callback URL in my twitter account, I was trying to specify the callback URL in the call to the API. It's weird that on twitter it says: "oauth 1.0a applications should explicitly specify their oauth_callback URL on the request token step, regardless of the value given here", but it works although I am not providing it. Thanks again


Yes, I'd say that's what you'd call a 'feature' of the Twitter REST API. Still, glad it's now working for you. :0)