Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to write a simple Flask API for hello world?

  • Last Updated : 17 Oct, 2021

Prerequisites: Introduction to REST API 

REST stands for Representational State Transfer and is an architectural style used in modern web development. It defines a set of rules/constraints for a web application to send and receive data. In this article, we are going to learn how to create a simple REST API that returns ‘Hello World’, with the help of a flask.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

In this article we are going to write a simple flask API for hello world using two methods:



  • Using Flask jsonify object.
  • Using the flask_restful library with Flask.

Installation

1. Install the python Flask library using the following command.

pip install Flask

2. Install the flask-restful library using the following command.

pip install Flask-RESTful

Method 1: Using Flask ‘jsonify’ object

  • Create a new python file named ‘main.py’.
  • import Flask, jsonify and request from the flask framework.
  • Register the web app into an app variable using the following syntax.

Python3




app =   Flask(__name__)
  • Create a new function named ‘helloworld’. This function is going to return the ‘Hello World’ text in JSON format.
  • Route the ‘helloworld’ function to your desired URL using the following syntax.

Python3




@app.route('/path_of_the_response', methods=['GET'])
def helloworld():
    pass
  • Inside the ‘helloworld’ function if request method is ‘GET’ then create a python dictionary with the ‘Hello World’ message.
  • Jsonify the python dictionary and return it.
  • Build the flask application using the following command.

Python3




if __name__ == '__main__':
    app.run(debug=True)
  • Run the ‘main.py’ file in the terminal or the IDE and type the following URL in the browser.
http://127.0.0.1:5000/hello

Code :

Python3




from flask import Flask, jsonify, request
  
app = Flask(__name__)
  
  
@app.route('/hello', methods=['GET'])
def helloworld():
    if(request.method == 'GET'):
        data = {"data": "Hello World"}
        return jsonify(data)
  
  
if __name__ == '__main__':
    app.run(debug=True)

Output :



Method 2: Using the flask_restful library 

  • Create a new python file named ‘main.py’.
  • Import Flask from the flask framework.
  • Import API and Resource from the ‘flask_restful’ library.
  • Register the web app into a app variable using the following syntax.

Python3




app =   Flask(__name__)
  • Register the app variable as an API object using the Api method of the ‘flask_restful’  library.

Python3




api =   Api(app)
  • Create a resource class named ‘HelloWorld’.
  • Inside the resource class, create a ‘get’ method.
  • Return a dictionary with the ‘Hello World’ message from the ‘get’ method.
  • Add the resource class to the API using the add_resource method.
  • Build the flask application using the following command.

Python3




if __name__ == '__main__':
    app.run(debug=True)
  • Run the ‘main.py’ file in the terminal or the IDE and type the following URL in the browser.
http://127.0.0.1:5000/hello

Code :

Python3




from flask import Flask
from flask_restful import Api, Resource
  
app =   Flask(__name__)
  
api =   Api(app)
  
class HelloWorld(Resource):
    def get(self):
        data={"data":"Hello World"}
        return data
  
api.add_resource(HelloWorld,'/hello')
  
  
if __name__=='__main__':
    app.run(debug=True)

Output :




My Personal Notes arrow_drop_up
Recommended Articles
Page :