Python and REST APIs: Interacting With Web Services – Real Python
Python and REST APIs: Interacting With Web Services by Jason Van Schooneveld Updated Oct 22, 2026 Reading time estimate 51m intermediate api web-dev Table of Contents Remove ads Recommended Course Interacting With REST APIs and Python (1h 44m) There’s an amazing amount of data available on the Web. Many web services , like YouTube and GitHub, make their data accessible to third-party applications through an application programming interface (API) .
One of the most popular ways to build APIs is the REST architecture style. Python provides some great tools not only to get data from REST APIs but also to build your own Python REST APIs. In this tutorial, you’ll learn: What REST architecture is How REST APIs provide access to web data How to consume data from REST APIs using the requests library What steps to take to build a REST API What some popular Python tools are for building REST APIs By using Python and REST APIs, you can retrieve, parse, update, and manipulate the data provided by any web service you’re interested in.
If you’re new to APIs in general, then start with Real Python’s broader guide to working with APIs in Python , which covers consuming all kinds of web APIs. This tutorial zooms in on the REST architecture style specifically. Free Bonus: Click here to download a copy of the “REST API Examples” Guide and get a hands-on introduction to Python + REST API principles with actionable examples. Take the Quiz: Test your knowledge with our interactive “Python and REST APIs: Interacting With Web Services” quiz.
You’ll receive a score upon completion to help you track your learning progress: Interactive Quiz Python and REST APIs: Interacting With Web Services In this quiz, you'll test your understanding of REST APIs and how to interact with them using Python. With this knowledge, you'll be able to retrieve, parse, update, and manipulate data provided by any RESTful API that you're interested in. REST Architecture REST stands for re presentational s tate t ransfer and is a software architecture style that defines a pattern for client and server communications over a network.
REST provides a set of constraints for software architecture to promote performance, scalability, simplicity, and reliability in the system. REST defines the following architectural constraints: Stateless: The server won’t maintain any state between requests from the client. Client-server: The client and server must be decoupled from each other, allowing each to develop independently. Cacheable: The data retrieved from the server should be cacheable either by the client or by the server.
Uniform interface: The server will provide a uniform interface for accessing resources without defining their representation. Layered system: The client may access the resources on the server indirectly through other layers such as a proxy or load balancer . Code on demand (optional): The server may transfer code to the client that it can run, such as JavaScript for a single-page application. Note, REST is not a specification but a set of guidelines on how to architect a network-connected software system.
Remove ads REST APIs and Web Services A REST web service is any web service that adheres to REST architecture constraints. These web services expose their data to the outside world through an API. REST APIs provide access to web service data through public web URLs. For example, here’s one of the URLs for GitHub’s REST API : https://api.github.com/users/<username> This URL allows you to access information about a specific GitHub user.
You access data from a REST API by sending an HTTP request to a specific URL and processing the response. HTTP Methods REST APIs listen for HTTP methods like GET , POST , and DELETE to know which operations to perform on the web service’s resources. A resource is any data available in the web service that can be accessed and manipulated with HTTP requests to the REST API. The HTTP method tells the API which action to perform on the resource.
While there are many HTTP methods, the five methods listed below are the most commonly used with REST APIs: HTTP method Description GET Retrieve an existing resource. POST Create a new resource. PUT Update an existing resource. PATCH Partially update an existing resource. DELETE Delete a resource. A REST API client application can use these five HTTP methods to manage the state of resources in the web service. Status Codes Once a REST API receives and processes an HTTP request, it will return an HTTP response .
Included in this response is an HTTP status code . This code provides information about the results of the request. An application sending requests to the API can check the status code and perform actions based on the result. These actions could include handling errors or displaying a success message to a user. Below is a list of the most common status codes returned by REST APIs: Code Meaning Description 200 OK The requested action was successful.
201 Created A new resource was created. 202 Accepted The request was received, but no modification has been made yet. 204 No Content The request was successful, but the response has no content. 400 Bad Request The request was malformed. 401 Unauthorized The client is not authorized to perform the requested action. 404 Not Found The requested resource was not found. 415 Unsupported Media Type The request data format is not supported by the server.
422 Unprocessable Content The request data was properly formatted but contained invalid or missing data. 500 Internal Server Error The server threw an error when processing the request. These ten status codes represent only a small subset of the available HTTP status codes . Status codes are numbered based on the category of the result: Code range Category 2xx Successful operation 3xx Redirection 4xx Client error 5xx Server error HTTP status codes come in handy when working with REST APIs as you’ll often need to perform different logic based on the results of the request.
API Endpoints A REST API exposes a set of public URLs that client applications use to access the resources of a web service. These URLs, in the context of an API, are called endpoints . To help clarify this, take a look at the table below. In this table, you’ll see API endpoints for a hypothetical CRM system. These endpoints are for a customer resource that represents potential customers in the system: HTTP method API endpoint Description GET /customers Get a list of customers.
GET /customers/<customer_id> Get a single customer. POST /customers Create a new customer. PUT /customers/<customer_id> Update a customer. PATCH /customers/<customer_id> Partially update a customer. DELETE /customers/<customer_id> Delete a customer. Each of the endpoints above performs a different action based on the HTTP method. Note: The base URL for the endpoints has been omitted for brevity. In reality, you’ll need the full URL path to access an API endpoint: https://api.example.com/customers This is the full URL you’d use to access this endpoint.
The base URL is everything besides /customers . You’ll note that some endpoints have <customer_id> at the end. This notation means you need to append a numeric customer_id to the URL to tell the REST API which customer you’d like to work with. The endpoints listed above represent only one resource in the system. Production-ready REST APIs often have tens or even hundreds of different endpoints to manage the resources in the web service.
Remove ads REST and Python: Consuming APIs To write code that interacts with REST APIs, most Python developers turn to requests to send HTTP requests. This library abstracts away the complexities of making HTTP requests. It’s one of the few projects worth treating as if it’s part of the standard library . To start using requests , you need to install it first. You can use pip to install it: Language: Shell $ python -m pip install requests Now that you’ve got req
The rest of this page is on the live site.