Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.
Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.
For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.
It makes sense to start learning by reading and watching videos about fundamentals and how things work.
Data Science and Machine Learning - 16 wks
Full-Stack Software Developer - 16w
Search from all Lessons
Curated list of small interactive and incremental exercises you can take to get better at any coding skill.
Curated section of projects to build while learning with simple instructions, videos, solutions, and more.
Guides on different topics related to the technologies that we teach in our courses
In the world of modern development, it's very common to use an API (Application Programming Interface) to connect to third-party services. For example, if you're creating an application where you need to track users' locations, instead of writing all the necessary code for that, you can simply use the Google Maps API.
In this article, we will see how to consume an API in Python with the help of the Requests library. In the following example, we will look at a simple case of a GET request to the free jsonplaceholder API
To use the Requests library, you first need to install it on your computer. You can do this with the following command:
Once installed, you can use it in your code to make HTTP requests, in this case, a GET request for an example user.
In this example, we use the
get(api_url) method of the requests library to retrieve information from a fake user provided by the jsonplaceholder API. This method returns the information received from the API and stores it in the
response variable. If the request was successful, the API returns a
status_code in the range of 2XX (between 200 and 299) and the user information. If there was an error in the process, it returns a
status_code in the 4XX range (between 400 and 499) and a message explaining the error.
APIs are mechanisms that allow two software components to communicate with each other through a set of definitions and protocols. In the context of programming, an API is used to access functions and data from an external application. In Python, APIs are a common way to obtain and manipulate data from online services such as web services, databases, and other external resources.
A common example of an API is the weather API, which provides basic information about the weather in any city or country. If you need to create an application that requires weather information from anywhere in the world, you can use this API via HTTP protocols to retrieve and use that information directly in your application
Below are some examples of how to consume an API in Python using the Requests library and the free jsonplaceholder API that provides simulated data. To get started, you need to install the requests library on your computer with the following command:
This library allows you to access information obtained from an API in a very simple way. Here are some of the most common methods and properties for accessing the information returned by requests:
|Contains the status code of the request, e.g., 201|
|Contains the URL of the request.|
|Provides the headers of the request.|
|Provides the cookies of the request.|
|Contains the encoding of the request, e.g., utf-8.|
|Stores the information received from the API. For example, in the weather API, it might be a list of dictionaries with city information.|
Important note: Requests to the jsonplaceholder API only simulate the behavior of a real API. Not all requests are functional. For example, if you make a POST request to the jsonplaceholder API, the information will NOT be saved on the API's servers, but the API responds with a message that simulates success.
GET requests are used to retrieve information from a server.
In this example, we use the
get() method of the requests library to retrieve information about a simulated post from the jsonplaceholder API. This information is stored in the
response variable. Then, with an
if-else conditional, we check if the API request was successful. If it was, we print the information to the console; otherwise, we print an error message.
POST requests are used to send data to the server.
In this example, we use the
post() method from the requests library to create a new object on the server.
post() method takes two parameters: the first one is the API URL, and the second one is the information of the object we want to create within a dictionary.
PUT requests are used to update data on the server.
To make a PUT request, we need to use the
put() method from the requests library. This method also takes two parameters: the first one is the URL that tells the API which specific object you want to update, and the second parameter is the information with which you want to update the object.
DELETE requests are used to delete data on the server.
To make a DELETE request in Python, we need to use the
delete() method from the requests library. This method takes the URL as a parameter, indicating which specific object you want to delete. Normally, APIs return a message indicating whether the object was deleted successfully or not, but the jsonplaceholder API does not return a specific message; it only returns a 200
APIs play a fundamental role in application integration and data exchange in the world of software development. In Python, the Requests library allows us to interact with APIs in a simple and intuitive way. In this article, we learned how to use this library to make HTTP requests to obtain, create, update, or delete information in an API. Now, you are ready to consume an API correctly and use its functionalities in your own applications.