This tutorial will introduce you to web APIs and teach you how to use the requests Python library to fetch and update information in web APIs. You will also learn how to interact with the Twitter API as a working example.
Introduction to Web APIs
An API (Application Programming Interface) is a framework for building HTTP services that can be consumed by a wide variety of clients. Web APIs use HTTP protocol to handle requests between the client and the web server.
Some of the most common APIs that enable developers to integrate and use their infrastructure include:
- Google APIs
- Twitter API
- Amazon API
- Facebook API
One of the most important reasons to use an API as opposed to other static data sources is because it’s real time. For example, the Twitter API we are going to use will fetch real-time data from the social network.
Another advantage is that the data keeps changing, so if you were to download it at intervals, it would be time-consuming.
Using the Requests Library
In order to use an API, you will need to install the requests Python library. Requests is an HTTP library in Python that enables you to send HTTP requests in Python.
In your terminal, type:
To check if the installation has been successful, issue the following command in your Python interpreter or the terminal:
If there are no errors, the installation has been successful.
How to Get Information From a Web API
The GET method is used to get information from a web server. Let’s see how to make a GET request to get GitHub’s public timeline.
We use the variable
req to store the response from our request.
Read full tutorial at code.tutsplus