Create a RESTfull API using Python and Flask (Part 1)

Kasun Dissanayake
Mar 5 · 5 min read

RESTfull API ??

A RESTful API is an application program interface (API) that uses HTTP requests to GET, PUT, POST and DELETE data. REST we called as REST(REpresentational State Transfer). It is an architectural style and approach to communications in web services development. A RESTful API explicitly takes advantage of HTTP methodologies like ,

  • GET to retrieve a resource
  • PUT to change the state of or update a resource, which can be an object, file or block
  • POST to create that resource
  • DELETE to remove it.


Python is a full-fledged all-round language. It’s an interpreted, interactive, object-oriented, extensible programming language. For further reference check here.


Flask is a Python web framework which can be used to develop web applications. Flask is unique in that it’s self-labeled as a “microframework”, meaning that the philosophy behind it is to provide a functional and concise core for your application, and to leave the rest up to you, the developer. You can check more about this here.

Let’s Go

Here, we’re going to learn,

  • What an API is and when you should use one?.
  • How to build a web API that returns data to its users?.
  • Some principles of good API design, applying them to an API that draws book metadata from a database.


Python 3, the Flask web framework, and a web browser are required to build this API, and installation instructions for all platforms are described below. I have used Windows environment here.

First you need to install Python. To download Python, follow this link.

To confirm that Python has installed successfully, first open the command line. Once your command line is open, enter these commands:

python --version
pip --version

Next, you’ll need to install Flask. Open up a terminal and type,

pip install flask

This will install Flask using the pip package manager for Python.

Congrats!! Now you have setup the project configurations successfully.

Okay, Let’s get some knowledge about API’s.

Why do we need an API?

  1. Your data set is large, making download via FTP unwieldy or resource-intensive.
  2. Your users will need to access your data in real time, such as for display on another website or as part of an application.
  3. Your data change/update frequently.
  4. The users who need your data may need a part of data at once.
  5. The users will need to perform actions rather than retrieving data, such as updating, or deleting data.

When using or building APIs, you will encounter these terms frequently:

HTTP (Hypertext Transfer Protocol) — The underlying protocol used by the World Wide Web(WWW) and this protocol defines how messages are formatted and transmitted, and what actions Web servers and browsers should take in response to various commands.

URL (Uniform Resource Locator) — A unique identifier used to locate a resource on the internet. It is also referred to as a web address. URLs consist of multiple parts — including a protocol and domain name — that tell a web browser how and where to retrieve a resource.

JSON (JavaScript Object Notation) — A lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language

REST (REpresentational State Transfer) — A software architectural style that defines a set of constraints to be used for creating Web services. Web services that conform to the REST architectural style, termed RESTful Web services, provide interoperability between computer systems on the Internet.

Our Project

In this project we’re going to build a prototype API using Python and the Flask web framework. Our example API will take the form of a distant reading archive — get media details such as music tracks, playlists, artists, albums to those working on digital projects.

We’ll begin by using Flask to create a home page for our site. In this step, we’ll learn the basics of how Flask works and make sure our software is configured correctly. Once we have a small Flask application working in the form of a home page, we’ll iterate on this site, turning it into a functioning API.

Okay first we are going to name our project as myfirst_api. Make folder and name it as myfirst_api. Change current working directory to myfirst_api.

mkdir myfirst_api
cd myfirst_api

Then create directory and name it as api.

mkdir api

You can also create the myfirst_api and api folders using your operating system’s graphical user interface. Now, choose your favourite IDE to do the implementation. I have used Visual Studio Code here.

Save this code as in the api folder.

import flask

app = flask.Flask(__name__)
app.config["DEBUG"] = True

@app.route('/', methods=['GET'])
def home():
return '''<h1>Welcome to New Project</h1>
<p>A prototype API for distant reading of Music Albums and Playlists.</p>'''

In the command line, navigate to your api folder:

cd myfirst_api/api

Now, run the Flask application with the command:


You should see output similar to this:

Type this URL in your browser and press enter (Press CTRL+C to quit)

Congratulations, you’ve created a working web application!

In this tutorial, you saw how relatively easy it is to create a comprehensive REST API with Python. Now that we have a running Flask application and know a little about what Flask does.

In Part 2 of this series, you’ll learn how to add our data as a list of Python dictionaries, Designing Requests and How to connect our API to a Database.

Kasun Dissanayake

Written by

Software Engineer at hSenid Software International

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade