Creating A Simple API Using Flask In Python

Adegboro Gbenga
3 min readSep 26, 2021

--

This is a simple guide for building an API with Flask

Image by Joel Filipe from Unsplash

Flask is a broadly used micro web framework for creating APIs in Python. It is a simple yet powerful web framework that is designed to get started quick and easy, with the ability to scale up to complex applications.

“Micro” does not mean that your whole web application has to fit into a single Python file (although it certainly can), nor does it mean that Flask is lacking in functionality. The “micro” in microframework means Flask aims to keep the core simple but extensible.

Installation

pip install Flask

Simple Flask App

from flask import Flaskapp = Flask(__name__)
@app.route('/‘)def welcome(): return "Hello World!"
if __name__ == '__main__': app.run()

Now, let’s understand the working of the code line-by-line before running it:

from flask import Flask → Imports the Flask class

app = Flask(__name__) → Creates an instance of the class

@app.route(‘/‘) → We use the route() decorator to tell Flask what URL should trigger the function and methods specify which HTTP methods are allowed. Though I will be keeping it simple in this hands-on and putting just “/”.

if __name__ == ‘__main__’ → __name__ is a special variable in Python which takes the value of the script name. This line ensures that our Flask app runs only when it is executed in the main file and not when it is imported in some other file

app.run() → Run the Flask application

It is here we specify the host. The default value for host and port value is localhost(127.0.0.1) and 5000 respectively and you can set the parameter port to use the port number of your choice.

This code can be better written as:

If __name__ != ‘__main__’:    pass
else:
app.run()

Save this file as app.py (or any other filename you want) and run it.

You should see something like this:

* Running on http://127.0.0.1:5000/

Launch any web browser and go to the URL to see the app in action.

(Press CTRL+C to quit)

Variable Rules

You can add variable sections to a URL by using <variable_name>. The function receives the variable as a keyword argument.

from flask import Flaskapp = Flask(__name__)
@app.route('/<string:name>/')def hello(name):return "Hello " + name
app.run()

Run the above code to start the Flask application.

Open the browser and go to http://127.0.0.1:5000/<string:name>/, you will see the output as Hello plus the name you attached.

Now let’s go ahead and push this code to our online repo (GitHub). If you have no previous experience doing this, I explained it in a previous article.

Open the terminal and enter the following commands:

git init

git add app.py

git commit -m “Simple API using flask v1”

git branch -M master

git push <insert remote repository url> maste

Now the code has been successfully pushed to my online repo, to access it click here

Thanks for reading 💜

Please leave me a comment if you find any typos or technical glitches! if you also have any thoughts on the topic, leave a comment — I am open to learning and knowledge explorations.

You can also reach out to me on Twitter.

Remember to drop a 👏

--

--