Building a simple REST API with Python and Flask by Onejohi

For this tutorial, you will need Python 3 and the Flask web framework. You’ll also require a web browser and a text editor (such as Notepad++ or BBEdit). Join the growing number of people supporting Programming Historian so we can continue to share knowledge free of charge.

build simple restful api with python and flask part 1

The clients of our web service will be asking the service to add, remove and modify tasks, so clearly we need to have a way to store tasks. The obvious way to do that is to build a small database, but because databases are not the topic of this article we are going to take a much simpler approach. To learn about proper use of databases with Flask once again I recommend that you read my Mega-Tutorial. Before Project manager Wikipedia we modify our code, first download the example database from this location and copy the file to your api folder using your graphical user interface. The final version of our API will query this database when returning results to users. At the end of this lesson, you’ll be exposed to a somewhat more complex API that uses a database, but most of the principles and patterns we’ve used so far will still apply.


This response also includes a copy of the new car with an id generated by the API. It’s important to send back an id in the response so that the user can modify the resource again. As you do this, make sure to consider any nested resources. For example, customers may have sales, or events may contain guests. Establishing these resource hierarchies will help when you define API endpoints. As with most things in technology, there’s a wide range of opinions on the best approach to building APIs.

  • Summary defines the UI interface display text for this endpoint.
  • History Data Service – A collection of data from a wide variety of historical sources.
  • Ticket booking apps use a desired single portal API to keep updated data at a familiar interface.
  • It doesn’t try to handle all aspects of web application development.

If the output for these commands includes a version number, Python is installed and available from the command line and you can proceed to the next step. You can use the Windows, macOS, or Linux operating systems to complete this tutorial, and those few instructions that are not the same across platforms will be explicitly noted. Python 3, the Flask web framework, and a web browser are required for this tutorial, and installation instructions for all platforms are outlined below. Learn some principles of good API design, applying them to an API that draws book metadata from a database.


The PUT and DELETE requests would look similar as well, with only the –request flag changing. The two basic tasks of a waiter are taking an order and delivering it to the cook and then taking the prepared food from the kitchen to the customer. In this light, a waiter’s job description fits perfectly into how an API works. The API takes in some data from the user as an input and delivers it to the system.

build simple restful api with python and flask part 1

These actions could include handling errors or displaying a success message to a user. Overall, I think you’ve done a nice job putting together this material. My one beef is that it perpetuates an unfortunately common Network Security Specialist Freelance Jobs & Employment pattern. You constantly refer to an ‘id’ when it would be much better to simply use a URI. The issue is that any client working with your API will have to know how to construct URLs for requests they want to make.

Step 1 – Setup the Python Project with Pipenv

The authentication extension gives us the freedom to choose which functions in the service are open and which are protected. We apply this technique to all the other functions and with this we ensure that the client always sees URIs instead of ids. The problem with the current design of the API is that clients are forced to construct URIs from the task identifiers that are returned. This is pretty easy in itself, but it indirectly forces clients to know how these URIs need to be built, and this will prevent us from making changes to URIs in the future.

  • All APIs have a base URL like this one that is the same across all requests to the API.
  • This function allows us to return 404 pages when something goes wrong in the application.
  • As you use other APIs in your research, you’ll develop a sense of what makes a good API from the perspective of a potential user.
  • At this point, the user can either update or delete the person.

As you can see in this snippet, there are three methods to fetch, write and delete records from the CSV file. The View provides the mechanism to update the web application DOM. Its own API is what the Controller calls to update the DOM when a user interaction event requires it. The above HTML code extends the home.html file to pull in the external normalize.min.css file, which is a CSS reset file to normalize the formatting of elements across browsers. You’ve got a working REST API with a great Swagger UI documentation/interaction system.

What is a RESTful web service?

At the end of the series you would understand how easy to build restful API with flask. In this article we’ll setting our environment and create endpoint that will show “Hello World”.

build simple restful api with python and flask part 1

In short, that means that they will work on Linux, Mac OS X and also on Windows if you use Cygwin. The commands are slightly different if you use the Windows native version of Python. History Data Service – A collection of data from a wide variety of historical sources. Chronicling America – A digitized collection of American newspaper articles from the 18th to the 20th century. As before, we’ll examine the code more closely once you have it running.

exertio theme

Leave Your Comment

Ready To Get Started

Freelance Soko connects buyers of freelance services to the best freelancing talent in Kenya. Enjoy convenience and security working online in Kenya.