Python and APIs: how to use Python to connect and interact with APIs
Tags: python, api, tutorial, request
Published: 2023-11-08 12:49:44

---
title: Python and APIs: how to use Python to connect and interact with APIs
published: true
date: 2023-08-13 22:00:00 UTC
tags: python, api, tutorial, request
canonical_url: https://blog.apify.com/python-and-apis/
---

An [API](https://blog.apify.com/what-is-an-api/) (Application Programming Interface) is an interface that allows different systems and applications to communicate with each other. An API is like a messenger that takes your request and delivers it to the provider that youre requesting from, then returns the response to you.

For example, your web browser makes a request to the Apify web server to view this blog post, and the Apify web server, in turn, provides the blog post. An API works the same way: you request some data from an API server and receive a response.

![Python and APIs: snake coiled around circuits to illustrate using Python to interact with APIs](https://blog.apify.com/content/images/2023/09/python-and-apis-how-to-use.jpg)

Python is a great option to use to interact with APIs

You can save time and make tasks easier by using APIs, and if you already know Python, it's a great language to use for this. APIs are useful for integrating other software, such as payment processing systems, into your applications. APIs can be used to access data in real-time, such as the Weather API to access current weather conditions, the Google Maps API to integrate a Google map within your application, or the Stock Market API to access real-time stock quotes, data feeds and stock exchanges. There are many APIs available that can be used for lots of different purposes.

There are various HTTP methods and status codes for APIs. HTTP methods tell the API the type of action that needs to be performed on the data. Once the API processes the HTTP request that is made using HTTP methods, it returns a response with an HTTP status code. This status code tells us about the success or failure of the request.

## **API status codes and HTTP methods**

Let's take a look at some of the most common status codes and HTTP methods.

### **Status codes**

The status code is returned at every call made to the web server. Here are some of the most common status codes:

- **200 - OK** : The request was successful.

- **201 - Created** : The resource was created, such as a new user or a new file.

- **204 - No Content** : The request was successful, but there is no content to return.

- **301 - Moved Permanently** : The requested resource has been permanently moved to a new location.

- **400 - Bad Request** : The server cannot process the request because the request is either wrong or missing some information.

- **401 - Unauthorized** : The request requires authentication because you did not send the correct credentials to access an API.

- [**403 - Forbidden**](https://blog.apify.com/web-scraping-how-to-solve-403-errors/): You do not have the right permission to access the resource.

- **404 - Not Found** : The requested resource is not found on the server.

- **405 - Method Not Supported** : The method used in the request is not supported by the resource.

- **500 - Internal Server Error** : An unknown error has occurred on the server side.

- **503 - Service Unavailable** : The server is overloaded or undergoing maintenance.

### **HTTP methods**

There are many HTTP methods, but the five most commonly used are listed below:

- **GET** : This method is used to retrieve data (such as the content of a web page) from the server.

- **POST** : This method is used to send data to the server or add new data to the server.

- **PUT** : This method is used to update data on the server or change the existing information.

- **PATCH** : This method is used to make partial changes to an existing resource on the server.

- **DELETE** : This method is used to delete certain information from the server.

## **Getting started with the Python Requests library**

In Python 3, you need only one library for making HTTP requests and working with APIs: the _Requests_ library. _Requests_ is not a part of the standard Python library, so youll need to install it to get started.

![Python and APIs: snake coiled around keyboard](https://blog.apify.com/content/images/2023/09/python-and-apis-snake-coiled-around-keyboard.jpg)

Install Python Requests with `pip install requests` to get started

The requests library allows you to send HTTP requests. The HTTP request returns a response object with all the response data (content, status code, headers, etc.). The two most important parts of response data are the status code and the response body. It's a versatile library that can even be used for [web scraping with Python Requests](https://blog.apify.com/web-scraping-with-python-requests).

The status code indicates the success or failure of the request. The response body contains the data returned by the API. The data can be in the form of a dictionary, string, or any other object.

You can install the requests library by running the below command in your terminal:

```
pip install requests

```

After the installation finishes, import it using the code below.

```
import requests

```

All set, so its time to make some API requests with Python. Well be using [REQRES API](https://reqres.in/).

### **Using GET request**

One of the common HTTP methods is GET. The GET method allows you to retrieve data from the specified resource.

To make a get request, invoke the _requests.get()_ method.

```
import requests
response = requests.get('https://reqres.in/api/users')
print(response)

```

The code output is this:

```
<Response [200]>

```

The code is generating status code _200,_ which shows that the URL is reachable. Its time to retrieve some data from the URL.

```
import requests
response = requests.get('https://reqres.in/api/users')
print(response.json())

```

The code output is this:

```
{'id': 1, 'email': 'george.bluth@reqres.in', 'first_name': 'George', 'last_name': 'Bluth', 'avatar': 'https://reqres.in/img/faces/1-image.jpg'}, 

{'id': 2, 'email': 'janet.weaver@reqres.in', 'first_name': 'Janet', 'last_name': 'Weaver', 'avatar': 'https://reqres.in/img/faces/2-image.jpg'}, 

.
.
.

```

The GET request is made to the API endpoint _/users_, and the list of all the users will be returned. Then the `.json()` function is called to produce the output in JSON ([JavaScript Object Notation](https://en.wikipedia.org/wiki/JSON)) format, as shown above. The JSON output looks like a Python dictionary thats easy to read.

To read the data of a single user, you can make an API call with id = 1.

```
import requests
response = requests.get('https://reqres.in/api/users/1')
print(response.json())

```

Here's the code output:

```
{'data': {'id': 1, 'email': 'george.bluth@reqres.in', 'first_name': 'George', 'last_name': 'Bluth', 'avatar': 'https://reqres.in/img/faces/1-image.jpg'}, 'support': {'url': 'https://reqres.in/#support-heading', 'text': 'To keep ReqRes free, contributions towards server costs are appreciated!'}}

```

If you try to make an API call with an id that doesnt exist, youll get a 404 response, which means the user with this particular id is not found. Run the below code, for example, and youll get a 404 response.

```
import requests
response = requests.get('https://reqres.in/api/users/22')
print(response)

```

### **Using POST request**

POST request will add new data to the API. First, create the API data in the form of JSON and then add it using the POST method.

```
import requests
new_user = {
    "name": "morpheus",
    "job": "leader"
}
response = requests.post("https://reqres.in/api/users", json=new_user)

print(response)
print(response.json())

```

Heres the code output:

```
<Response [201]>
{'name': 'morpheus', 'job': 'leader', 'id': '781', 'createdAt': '2023-08-05T16:23:14.034Z'}

```

The new user has been created, and the above API data is returned.

You can register new users by providing your email and password.

```
import requests

new_user = {
    "email": "eve.holt@reqres.in",
    "password": "pistol"
}
response = requests.post("https://reqres.in/api/register", json=new_user)

print(response)
print(response.json()

```

The code output is this:

```
<Response [200]>
{'id': 4, 'token': 'QpwL5tke4Pnpja7X4'}

```

The new user has been registered, and the _id_ of a new user is 4.

If you try to register with only _email id_, then youll get a response of _400_ which means a bad request.

```
import requests

new_user = {
    "email": "eve.holt@reqres.in"
}
response = requests.post("https://reqres.in/api/register", json=new_user)

print(response)
print(response.json())

```

Heres the code output:

```
<Response [400]>
{'error': 'Missing password'}

```

### **Using PUT request**

PUT request is used to update the existing data, which means old data is replaced by new data. Below, were updating a new user whose id is 2.

```
import requests

update_user = {
    "name": "morpheus",
    "job": "zion resident"
}
response = requests.put("https://reqres.in/api/users/2", json=update_user)

print(response)
print(response.json())

```

The code output is the following:

```
<Response [200]>
{'name': 'morpheus', 'job': 'zion resident', 'updatedAt': '2023-08-05T17:03:18.934Z'}

```

The information has been updated as shown above, and you can also see the time and date of the update.

### **Using PATCH request**

To make partial updates, you can make a PATCH request to the API.

```
import requests

update_user = {
    "name": "morpheus",
    "job": "zion resident"
}
response = requests.patch("https://reqres.in/api/users/2", json=update_user)

print(response)
print(response.json())

```

The code output is this:

```
<Response [200]>
{'name': 'morpheus', 'job': 'zion resident', 'updatedAt': '2023-08-06T05:24:22.972Z'}

```

### **Using DELETE request**

To delete resources from the API, use the DELETE request.

```
import requests
response = requests.delete("https://reqres.in/api/users/2")
print(response)

```

This is the code output:

```
<Response [204]>

```

The user has been deleted, which is why the server is returning a 204 response. This means that the request was successful, but theres no content to return.

### **Query parameters**

When make API calls with Python Requests, you get a large amount of data in return. In the above code examples, youve seen that when you make a GET request, you get tons of user details. However, in most cases, you only want certain information. This is where query parameters come in.

For example, when you make a Google search, the URL looks something like this: [https://www.google.com/search?q=apify](https://www.google.com/search?q=apify). The _q=apify_ is a query parameter. To add a query parameter to the URL, you must use a question mark (?). For multiple query parameters, you can separate them with an ampersand (&).

Let's add some query parameters to the Python API call. Here, were adding two query parameters, which show that you want all the users of page 2 with a delay of 3 seconds. The final query will become this: [https://reqres.in/api/users?page=2&delay=3](https://reqres.in/api/users?page=2&delay=3).

```
import requests

query_para={
    'page': 2,
    'delay': 3
}

response = requests.get('https://reqres.in/api/users', params=query_para)
print(response.json())

```

Heres the code output:

```
{'page': 2, 'per_page': 6, 'total': 12, 'total_pages': 2, 'data': [

{'id': 7, 'email': 'michael.lawson@reqres.in', 'first_name': 'Michael', 'last_name': 'Lawson', 'avatar': 'https://reqres.in/img/faces/7-image.jpg'}, 

{'id': 8, 'email': 'lindsay.ferguson@reqres.in', 'first_name': 'Lindsay', 'last_name': 'Ferguson', 'avatar': 'https://reqres.in/img/faces/8-image.jpg'}, 

{'id': 9, 'email': 'tobias.funke@reqres.in', 'first_name': 'Tobias', 'last_name': 'Funke', 'avatar': 'https://reqres.in/img/faces/9-image.jpg'}, 

{'id': 10, 'email': 'byron.fields@reqres.in', 'first_name': 'Byron', 'last_name': 'Fields', 'avatar': 'https://reqres.in/img/faces/10-image.jpg'}, 

{'id': 11, 'email': 'george.edwards@reqres.in', 'first_name': 'George', 'last_name': 'Edwards', 'avatar': 'https://reqres.in/img/faces/11-image.jpg'}, 

{'id': 12, 'email': 'rachel.howell@reqres.in', 'first_name': 'Rachel', 'last_name': 'Howell', 'avatar': 'https://reqres.in/img/faces/12-image.jpg'}], 

'support': {'url': 'https://reqres.in/#support-heading', 'text': 'To keep ReqRes free, contributions towards server costs are appreciated!'}

```

All users on page 2 will be shown after a 3-second delay.

![Python and APIs: illustration of high-tech snakes and ladders game](https://blog.apify.com/content/images/2023/09/python-and-apis-snakes-and-ladders.jpg)

Troubleshooting with Python doesn't have to be like a high-tech game of snakes and ladders

## **Troubleshooting Python and APIs**

Troubleshooting is important when working with APIs. Lots of things can go wrong (e.g. the API server may be down, or you may have sent the wrong parameters). Here are some of the most important advice that can help you troubleshoot errors:

1. **Identify the issue** : determine if the issue is related to your code or an external factor. For example, if youre using the Google Maps API and you haven't filled out billing information, then youll get an error when you make a request using your the API key.

2. **Use print statements** : use print statements at various stages of your code to debug the code and understand whats happening. You can print the responses of the request, URLs, response data, etc.

3. **Check your input** : double-check your input, such as the API endpoint URL, credentials, headers, query parameters, and so on.

4. **Handle exceptions** : you can use exception handling (try/except blocks) to catch exceptions.

5. **Rate limits and authentication** : be aware of the rate limits imposed by the API and ensure that your API keys and tokens are correctly configured and you have the necessary permissions to access the API endpoints.

Let's deal with a real-world issue to understand this more clearly. Here, well use the GitHub API to retrieve repos using a GET request.

You might like to first read [how to use Python with the GitHub API](https://blog.apify.com/python-github-api/) and then come back.

While retrieving the repos from the _apify_ github, theres a chance that some issues may occur, such as HTTP errors, network exceptions, etc. That's why using the try/except blocks can help us to catch errors.

First, well use the `requests.get()` method to read all the repos of the given username. Then, well check the status of the response using the `raise_for_status()` method. This method will raise an exception for _non-200 status codes_, and then this exception will be handled in the _except_ block of the _HTTPError_.

After that, well use one _except_ block for the network error because sometimes you may lose your internet connection. And at last, theres an except block for any unknown errors that may occur.

 

>import requests

 

If you change the repo name to "_apifyy_", a 404 error will occur. If you lose your network connection, a network error will occur, and you won't be able to fetch the repos.

## **Advanced tips**

If you've read this far, and even more so if you've looked at our guides to using Python with the [GitHub API](https://blog.apify.com/python-github-api/), the [Discord API](https://blog.apify.com/python-discord-api/), and the [Google Maps API](https://blog.apify.com/google-maps-api-python/), you should have a solid understanding of Python and APIs.

If you want to take it up a level, here are some advanced Python API tips that you could learn and try: authentication, pagination, and rate limiting.

### **Authentication**

Many APIs require some form of authentication in order to access their resources. Some APIs use basic authentication, while others require more complex techniques like [OAuth](https://en.wikipedia.org/wiki/OAuth).

Before making a request, you need to obtain an access token from the API provider. This is a unique identifier that grants you access to specific resources or actions.

>headers = {  
"Authorization": _f_"token {YOUR ACCESS TOKEN}  
}


### **Pagination**

Pagination is the process of dividing large amounts of data into multiple smaller pages. This is done to prevent the API from returning too much data at once, which can overload the API server. For example, if you go to the Apify repos, you will see something like this at the bottom:

![Python and APIs: screenshot of pagination](https://lh5.googleusercontent.com/VkGnXjx_8T9VtGaQePfGFLyxVwC1oDhNzXgbbBlVkJyzNqympPs0TLevY9GjeHK_ln9zYkX2O0R8usL0dIe4ylzSnHBJBz4II_gWRWuP8goVN6_c_pwsJUHcsacrbLWSs4wNpiTMEFevz6a56DP2jA)

Pagination is normally handled with the help of query parameters: _page_ and _size_. The page parameter defines which page you are requesting, and the size parameter defines the size of each page, like how many repos you want on one page.

### **Rate limiting**

Anyone can use public APIs, and sometimes people try to abuse them by making unnecessary requests or using bots. To prevent such attacks, the rate-limiting technique is used, which restricts you from making a certain number of requests per unit of time. Make sure you do not exceed the limit, as your requests will be blocked for some time.

For example, if you use the [GitHub API](https://blog.apify.com/python-github-api/) and youre making unnecessary requests, youll get blocked after 60 requests and receive a 403 status code.

![Python and APIs: screenshot of API rate limit exceeded error](https://lh4.googleusercontent.com/1f2b6nRoeSqMWSaqbd8ymvGyoDUmiV_OKIGMGVCe12EXm7U4v-w6osgC2-RT4Wdsb-0PV1SSZ67UQDmU6mN4OZMpGUsFW0Jh6ciN3lc2mcbmL8sqlbvyBDzN0xw1PrjNBVnFvAJgCxSpYjEws4ZuYA)

## **Using APIs and Python**

We've introduced you to the basics of using the Python Requests library to send API requests, and to some advanced API concepts and troubleshooting tips. You should now be aware of the most common HTTP methods and status codes and how they're used to retrieve or edit data through an API. From understanding status codes to manipulating data and even controlling the specifics of your request with query parameters, we've laid the foundation for more advanced use of APIs.

If you're interested in using Python for other purposes, check out [web scraping with Python](https://blog.apify.com/web-scraping-python/), or look at our other [Python articles](https://blog.apify.com/tag/python/).