Creating an API with Flask

Flask is a micro web framework for creating APIs in Python. It’s very simple, but very powerful which is designed to get an application started quick and easy. Flask is very powerful, becuase it has the ability to scale up to complex applications.

Just because it is a micro web framework does not mean that the entire application has to fit into a single file, nor Flask is lacking in any aspects of functionality. Micro simply means Flask aims to keep the core simple but extensible, says the Flask documentation.

Installing Flask is very easy, I recommend using pip to install Flask. First, check and see what version of python and pip you have installed with

python --version
pip --version

If the commands output version numbers, Python and pip are installed. You can easily install Flask with one command:

pip install Flask

This will install Flask with the pip package manager that is designed for Python. You can also install Flask with the Python3 version of Anaconda which comes with Flask installed.

Python has a built in database, SQLite, which makes it very easy to store and manipulate data. You don’t have to install any other software or keep a 3rd party service, as long as you have Python imported than you have relational database management system. I would also look into other libraries that you can import and explore some of the things the Python community has created such as Marshmellow, SQLAlchemy, and others.

A minimal Flask application takes less than 5 minutes to create. Create a file named or any other name

from flask import Flaskapp = Flask(__name__)
app.config["DEBUG"] = True
@app.route("/", methods=["GET", "POST"])
def home():
return "Hello World!"

Save the file and go to your terminal and type to start your application and head over to which is the default port for Python applications to see what you have created!

Each line in the application obviously does something different, lets go line by line and explain what each line does.

imports the Flask class from the installation of flask

creates an instance of the flask class and saves it as the variable app.

starts the debugger. If your code is wrong or malformed, you’ll see an error when you visit your app. Otherwise you will only see a generic message in the browser.

creates a route to tell Flask what URL should trigger the function. declares what HTTP methods are allowed, if none are declared such as just the default method will be

runs the flask application.

For this show application I am going to be using hard coded test information and not connecting to a database. I recommend checking out Pretty Printed Flask Movie API Example video that goes in depth on connecting and creating the database.

I’m gonna create some test data for the catalog as a list of dictionaries

from flask import Flask
from flask import request, jsonify
players = [
{'id': 0,
'name': 'Tom Brady',
'team': 'Buccaneers'},
{'id': 1,
'name': 'Aaron Rodgers',
'team': 'Packers'},
'name':'Patrick Mahomes',
'team': 'Chiefs'}
app = Flask(__name__)
app.config["DEBUG"] = True
@app.route("/", methods=["GET", "POST"])
def home():
return jsonify(players)

is just a variable that is a list of dictionaries that is hardcoded for test purposes.

imports request and jsonify functions that are given to us by Flask.

Jsonify is a function that allows us to turn lists and dictionaries into JSON format, this allows us to take that information and use it in whatever front end you decide.

At this point you have created a working API. You can expand on this API by created more routes with the same method above and declaring more routes. You can filter information with routes, add data, or find information with specific data.


Creating Web APIs with Python and Flask —

Creating RESTful Web APIs using Flask and Pythong—

Do You Know Python Has A Built-In Database —

Configuring Your Flask App —

Flask Movie API Example —

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