REST APIs are crucial in a client-server architecture as they provide a communication channel between the two. The client puts a message on the API; the API takes the message to the server; the server performs some action and responds to the client via the same API. If the API is eliminated, the whole full-stack architecture will fail.
This piece will discuss how you can write a simple REST API using Flask. We have also written a guide about how you can build a REST API using Node.js and Express.js.
Before we jump to building a REST API, let’s quickly look at Flask. It is a python micro-framework that is often used to write backend architecture. It is lightweight and lets you create an application using a single file. It does not require the developer to follow a particular directory structure and provides features like URL routing, template engine, etc.
Let’s go ahead and start building a REST API using Flask. We will do it in steps to make it easier to follow.
You can skip this step if you have already installed Python on your computer. If you haven’t, you can download the latest version from here. Afterward, install it on your computer.
Now create a directory on your computer and open it in your preferred code editor. Now open the terminal in the same directory.
You should create a virtual environment when working with Python, so your packages are not installed globally. The virtual environment can be created using Python’s
You can install this by opening your terminal and entering the following command there:
pip install virtualenv
This command will globally install virtualenv package on your computer. After its done installing, run the following there:
It will create an env directory with your virtual environment files. There is no need to change and open any files in the virtual environment.
There are different commands to activate the virtual environment on Linux and Windows. Choose the command from below that fits the bill for you:
# for linuxsource env/Scripts/activate# for windowssource env/bin/activate
Ensure you activate the virtual environment by running the command according to your operating system. The virtual environment needs to be activated for the entire span of your development process.
Once the virtual environment is activated, install the
flask_cors package. For this, run the following command in the terminal:
pip install Flask flask_cors
Now create an
app.py file in your project directory and import the
flask_cors packages at the top. Here is how you do it:
# importing packagesfrom flask import Flaskfrom flask_cors import CORS
Now set up a Flask app and enable CORS by adding the following piece of code in your
# APP SETUPapp = Flask(__name__)# enable resource sharing between frontend and serverCORS(app)
Last but not least, you need to create routes. Here is how you do it:
# ROUTES@app.route('/hello', methods=['GET'])def getHello():return 'This is a GET firstname.lastname@example.org('/hello', methods=['POST'])def postHello():return 'This is a POST email@example.com('/hello', methods=['PUT'])def putHello():return 'This is a PUT firstname.lastname@example.org('/hello', methods=['DELETE'])def deleteHello():return 'This is a DELETE request!'
In the end, your
app.py file will look something like this:
# IMPORTfrom flask import Flaskfrom flask_cors import CORS# APP SETUPapp = Flask(__name__)# enable resource sharing between frontend and serverCORS(app)# ROUTES@app.route('/hello', methods=['GET'])def getHello():return 'This is a GET email@example.com('/hello', methods=['POST'])def postHello():return 'This is a POST firstname.lastname@example.org('/hello', methods=['PUT'])def putHello():return 'This is a PUT email@example.com('/hello', methods=['DELETE'])def deleteHello():return 'This is a DELETE request!'if __name__ == "__main__":app.run(debug=True)
Now run the server by running the following command in the terminal:
Go to this URL. You will see a response like the following:
The GET request is executing because when you call an API via the browser’s address bar, it makes a GET request to the API.
That’s it. If you have followed along till now, you have successfully created a REST API using Flask and Python, and I hope this article improved your understanding of the REST APIs and Flask.