Build Simple Restful Api With Python and Flask Part 1

I’m going to divide this series into 3 or 4 articles. At the end of the series you would understand how easy to build restful API with flask. In this article we’ll setting our environment and create endpoint that will show “Hello World”.

I assumed that you have python 2.7 and pip installed on your computer. I have tested the code presented in this article on python 2.7, though it likely be okay on python 3.4 or newer.

a. Installing flask

Flask is microframework for python. The “micro” in microframework means Flask aims to keep the core simple but extensible ( You could installing flask with the following command:

$ pip install Flask

b. Prepare your IDE

Actually you could using all kind of text editor to build python app, but it would much easier if your are using IDE. Personally I prefer using Pycharm from jetbrains(

c. Create “Hello World” in flask

First you need to create your project folder, for this tutorial i will named it “flask_tutorial”. If you are using pycharm you could create project folder by choosing File and New Project from menus.

After that you could set your project location and interpreter. Your computer could have some python interpreter anyway.

After setting your project, on pycharm right click on your project folder then choose New -> Python File and named it “”.

Write down code below on

from flask import Flask

app = Flask(__name__)

def hello():
return "Hello World!"

__name__ == '__main__':

Run it from terminal. You could using command line or from pycharm you can click Terminal tab that located on bottom left and write code below.

$ python

Open your browser and access localhost:5000. And voila now you have your first flask application :)

OK now let’s take a look to the code.

from flask import Flask

This line ask the application to import Flask module from flask package. Flask used to create instances of web application.

app = Flask(__name__)

This line create an instance of your web application. __name__ is a special variable in python, it will equal to “__main__” if the module(python file) being executed as the main program.


This line define the routes. For example if we set route to “/” like above, the code will be executed if we access localhost:5000/. You could set the route to “/hello” and our “hello world” will be shown if we access localhost:5000/hello.

def hello():
return "Hello World!"

This line define function that will be executed if we access route.

if __name__ == '__main__':

This line mean that your flask app will being run if we run from Notice also that we are setting the debug parameter to true. That will print out possible Python errors on the web page helping us trace the errors.

Ok that’s all for part 1, next we will try CRUD operation on SQLLite with flask.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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