WebAPI Using Flask


Flask is micro framework, which is used write APIs in a very simple way. Flask is very good example of SRP (Single Responsibility Principle). It holds the only responsibility of exposing API methods.

Why Flask?

It's a micro framework It's esy to use and simple to implement, and has good flexibility. It’s already used in many high traffic websites. LinkedIn is one of the best examples.

Usage of Flask

First install Flask using the command: pip install flask
Below is the sample api code using flask 
  1. from flask import Flask  
  3. app = Flask(__name__)  
  6. @app.route('/')  
  7. def welcomeMessage():  
  8.     return 'Welcome to flask sample api'  
  11. @app.route('/getTestMessage')  
  12. def getTestMessage():  
  13.     return 'Hi this test message from flask sample api'  
  16. app.run()  
Below is the code for sample client to consume sampleApi 
  1. import requests  
  3. url = ''  
  5. resp = requests.get(url)  
  6. print("First response: " + resp.text) 
  8. resp = requests.get(url + 'getTestMessage')  
  9. print("Second response: " + resp.text) 
Below is the output snap of sampleClient,
WebAPI Using Flask
Above we have seen how simple it is use and consume API which is developed using Flask.
Actually when we talk about web frameworks, we will come across one more popular framework, Django. Django is a full-fledged web framework used for complete web application development. it has inbuilt Database migration, Admin usage, and Security protection. It help developers to complete complex web applications without any external plugins, whereas Flask is light weight, and used for simple web application development or static data application. For Django I will write one more article with its usage. Both Django and Flask are good in their own way. Travelling for 100kms we don’t need a flight, in same way travelling for several thousand KM a car/bike doesn’t suite. It’s up to the developer  to choose which framework suits better for our requirements.


In this article we have seen  the usage of Flask and when we can use it. I hope it will help you.