Introduction
Consuming APIs is one of the most useful skills for programmers. It's how applications retrieve external data, integrate with other systems, and automate real-world tasks.
In Python, this can be done very simply with the requests library. With just a few lines of code, you can make a request, verify that it worked, and start working with the returned data.
In this post, you will learn the basics of consuming an API in Python, understand the response, and avoid some common errors.
What is an API?
Simply put, an API is a way for systems to communicate.
Think of it this way:
Your code makes a request for something (usually some kind of information).
A server responds with data containing information about that "thing" you requested.
This is typically in JSON format (in some cases it can also be SOAP).
Example:
Weather app → calls an API
Backend → calls another API
System → integrates with another system
In other words: practically all the tools we use today consume an API.
Installing the library
To consume an API in Python, we'll use the requests library.
In your terminal, type this:
pip install requestsMaking your first request
Now let's make a simple request.
import requests
response = requests.get("https://jsonplaceholder.typicode.com/posts")
print(response.status_code)
print(response.json())Okay... but what just happened?
requests.get(...) → makes the HTTP request
response.status_code → shows whether it was successful or not
200 = success (there are several HTTP codes here in this documentation, take a look)
response.json() → transforms the response into a Python object
Working with the data
Now that we have the data, we can use it for anything:
data = response.json()
for post in data[:5]:
print(post["title"])Here you are already:
accessing data returned from the API and storing it in the data variable
manipulating the data
iterating and displaying the post titles
Common Mistakes
Many people assume it will always work.
Not checking the response status:
if response.status_code != 200:
print("Erro na requisição")Assuming that valid JSON is always returned is a mistake; not every API responds correctly, and if you don't handle this, the code may break.
Not using a timeout: Without a timeout, your application may freeze while waiting for a response.
response = requests.get("https://api.site.com", timeout=5)Use .text instead of .json()
# errado
print(response.text)
# correto
print(response.json())Consuming APIs is an essential skill for any developer.
With just a few lines of code, you can already retrieve external data, integrate systems, and build real solutions.
From there, you can evolve to authentication, custom APIs, and more complex integrations.

