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

Be sure to read the next Social article: How to Build a Facebook "Hello World" Web App in Python