Create a rest API with Flask in less than 10 minutes

Let’s start

Installing Flask and a little bit more

Assuming that you have python installed in your machine. If not, follow this tutorial before continuing:

Continuing

Create a folder for your project, and inside of the folder, execute the following code.

Creating environment and installing flask lib

Let’s create your primary file type the imports and initial code as demonstrated below.

Create some methods

First, the class Employee is created, extending Resource to act as a front for the rest API. The Employee’ methods become immediately available for calls when the route is defined. Check the complete documentation here: https://flask-restful.readthedocs.io/en/latest/intermediate-usage.html

Class and methods with route defined

As you can see, the method has a path, and you can test this path with Postman. Before executing your code, don’t forget the add the port.

In our example, it will listen to port 5002.

Run your rest API

Send a request using Postman.

Postman first test

That is it

Now, with only this code, you already created your rest API.
Tutorial finished.

Do you want to increment it a little more by using a database?

Adding database

SQLAlchemy is a toolkit for relational databases that will help you connect, create, and execute queries in a database. See more here: https://www.sqlalchemy.org/

You can also use another database, depending on what your project requires.

Create your database or get one already done and populated

https://www.sqlitetutorial.net/sqlite-sample-database/

For our purpose, we will use the sample from the above link, saving the database file in the root directory like this:

Folder database and db file

Now we add the code for connecting to the database.

Connecting with database file

Let’s create a query to select all the employees’ data and return the ID list.

Running the same test with Postman.

Listing IDs from employees

We now can add two methods for creating and deleting employees in the database using POST.

Create employee method
Delete employee method

Testing both endpoints in Postman:

Successful message creating employee
Successful message deleting employee

Next steps

The implementation of the methods is straightforward. Thus anything can be passed as a parameter, even null values. We can create the query dynamically and setting the values as they get set and, for specific fields, validate its value type .e.g:

Checking delete ID parameter:

Number validation

Conclusion

Now you have up and running a simple rest API. Flask is very practical, as you could see, and you can improve your API gradually depending on your requirements. Although you can do everything as a developer, Flask is a micro-framework and building a heavy load web application would not be recommended in this case.

GitHub

Get the complete code here: https://github.com/amanajas/flask

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store