munkeops
munkeops Hi I am just another guy trying to learn new things everyday and share my experiences. Hope you gain something in your read !

Setup a Flask API in 5 steps!

Setup a Flask API in 5 steps!

Hi guys, this post is going to be a quick starter’s guide to setting up your very own flask server to make API requests. Before we get started, ill try a give a bit of an overview on the aspects of this topic and where and how they are used.

APIs

APIs stand for application programming interface. They are essentially a means for communication between different aspects of an application(e.g., frontend and backend). It also serves another purpose of keeping the code abstracted and hidden, essentially not revealing the source code.

APIs for web dev?

In WebDev it is commonly seen (as in the microservice architecture) that the front end and the backend are essentially separated and hosted individually. Even in the case that they were hosted together, the front end page needs a mechanism to communicate with the backend and perform certain actions. In example, suppose you had a button on the front end that would upload an image, compress it and send it back for download. Now upon clicking the button we need to initiate this process in the backend, this requires the front end to communicate with the backend and hence an API.

Flask

Flask is a python framework built over raw concepts and low-level APIs such as sockets. With flask, we can set up a backend for a front end or standalone API for an application in no time. It’s an extremely light weight and modular ready to run HTTP based API server. While you can deploy the frontend separately (react/html etc. pages) and have the API separately, flask also supports the option of rendering pages in a lightweight manner as compared to some other heavy weights such as Django.

Steps to setup flask as an API

Step 1

install flask with python’s pip package manager.

1
pip install flask

Step 2

setup the falskapp package (flaskapp is the name of my app, you can name it anything):

1
2
3
root>mkdir flaskapp
root>cd flaskapp
flaskapp>touch __init__.py

Step 3 setup the flask object

Import the flask module and create the flask object (app) in init.py. Also import the views module here

1
2
3
4
from flask import Flask
app = Flask(__name__)

from .views import *

Step 4

create the views module (deriving from the mvc model)

1
flaskapp>touch views.py

inside views put the following template code:

1
2
3
4
from . import app
@app.route("/home")
def home():
    return "Hello, Flask!"

Step 5

create a server instance (server.py) which will be entry code to this application.

1
root> touch server.py

import the flaskapp package and run it

1
2
3
from flaskapp import *

app.run(host='127.0.0.1',port=5000,debug=True)

set host to locahost (127.0.0.1) for development and choose a port (5000 is generally used) and can set the debug mode to True so that the server updates and restarts when changes made durnig development.

Result

Voilà, you have your flask server up and running, to check it out you can open a browser and enter the IP and port as the server indicates in the output. The logs will show a GET request hit.

flaskoutput

the final file structure is as follows

flaskfilestruct

What next ?

Going further, you can create your own endpoints and send data along with the API as well. Also, you can check out postman as a handy tool to test out APIs.