A simple guide to creating REST API’s with flask

Photo by Rachael Gorjestani on Unsplash

About a week back I had asked some of my juniors to build a flask app. The result was a mixture of classes and  Resource implementation of flask_restful library. When asked why they made it so complex, he showed me the google search results for – how to build REST API’s using flask. Almost all the tutorials out there are either too in-depth or too difficult for beginners.This post is a beginner friendly tutorial on how to build flask API’s.

Before we start – a few jargon.
Flask: is a Python-based microframework that enables you to build web applications. The “micro” in microframework means Flask aims to keep the core simple but extensible.

REST: a RESTful API uses HTTP requests to GET, PUT, POST and DELETE data.

RESTful API designing: guidelines is a must read before you continue. It talks about terminologies, endpoints, versioning status codes and so much more.


Let’s take this program snippet for example –

Using @app.route you define which function will run when a particular endpoint(URL) is called. Inside the route decorator you can define the API endpoint, the methods that will activate the function below it.

URL patterns

In the above example, the <username> can be anything. A variable, that is used as a parameter for the function below the decorator. This is one of the ways using which you can pass variables from your front-end to your backend.

GET request

The following snippet creates an API to recieve 2 numbers and return their sum.

Using URL parameters: eg –

The same task using form data

And the same using json data

You can use the above examples with different types of request types by changing the method’s parameter.

Handling GET and POST request in the same function

Leave a reply:

Your email address will not be published.

Site Footer

Sliding Sidebar

About Me

About Me

Hey, I am Thomas Ashish Cherian. What I cannot create, I do not understand.

Social Profiles