REST API with Python
Before we get into building a REST API using python, it will be a wise choice to give a brief overview of what actually an API is, and then what is REST API.
What is an API?
API as we all know is Application Programming Interface. It allows softwares to interact with each other. Say you are building an application that gives some analytics regarding videos on YouTube, you would obviously require data from YouTube. One way would be to go to all video pages and copy and paste that information but that would not only be time taking but disastrous for a software engineer to do, when you can simply use the API provided by YouTube to query it’s data.[https://developers.google.com/youtube/v3/docs/search/list]. An API exposes methods that the clients can interact with to get desired information with the benefits of abstraction, authentication.
What is REST API?
Now that we have a basic understanding of what an API is, let’s move to what is a REST API. An API that conforms to REST architecture is a REST API. What is this REST architecture? Well, good question. REST architecture defines a set of 6 constraints which makes any API a true Restful API. Please refer here for the detailed information regarding these constraints https://restfulapi.net/rest-architectural-constraints/.
REST API using Python
As we have moved past our checkpoints, it’s time to build our python REST API. Lets starting writing our first function.
1. from flask import Flask
2. app = Flask(__name__)
3. @app.route(‘/’)
def hello_world():
return “Hello, World!”
Breaking down the code till now, in statement 1. we are simply importing our desired module, no biggies there. In statement 2. we are creating an instance of the imported module, we are passing __name__ here because it will take care of substituting data according to whether we are working with an application or a module. The main highlight that completes our API is statement 3. where we decorate our ‘hello_world()’ function. This decorator defines for what path this function would be invoked. In our case this is simply the root URL. Now that we have successfully written a small API, it’s time to launch our server and bring this API to life.
windows>set FLASK_APP=pythonapi.py
windows> flask run
* Serving Flask app “pythonapi.py” *
Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: off * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit) 127.0.0.1 — — [04/May/2021 12:15:59] “←[37mGET / HTTP/1.1←[0m” 200–127.0.0.1 — — [04/May/2021 12:15:59] “←[33mGET /favicon.ico HTTP/1.1←[0m” 404 -
we have successfully started our server, let’s access our API, shall we?
PERFECT! Exactly, what we wanted. Now let’s add one more function to our API.
current_users=[‘Sam’,’Rajeev’,’Linda’]
from flask import jsonify
@app.route(‘/getUsers’)
def get_users():
return jsonify(current_users)
We’ve added another function that returns current users. Notice, the path to access this is ‘/getUsers’ and we’ve also imported another modify ‘jsonify’ because flask doesn’t support returning certain data types and list is one of them, using jsonify we would be returning our list in a Json format. Let’s restart our server with the changes and get that list.
There we have another method added to our API. That concludes a basic tutorial on setting up a REST API using python. We have added two basic get methods, we’ll address more complex methods such as POST, DELETE, PUT in later articles.