Yes if you read the title Make APIs within 10 minutes, it's actually true and if you are someone who is really looking to build something cool with ease, you are at the right place.
Tech Stack to be used:-
- Python
- Flask
For this article, we will only cover simple API building and move on to some complex API in the next article.
Project that we will be building:- A small ToDo App without any front end and we will test it with POSTMAN, the postman is basically an API testing tool. You can download it from here
Also, we will not be using any DB to store the ToDo's, and to keep it very simple will be using some Data Structure for the temporary storage of the Todos.
Before starting let's first design the API of how it would look and what all functionalities it should have
API Design
- Add a Todo
- Update any Todo
- Delete any Todo
- Get any Todo
We will use a dictionary for keeping a todo and will keep the key as the id and value as the todo.
Step 1
Import the libraries and initialize the app:
import flask
app = flask.Flask(__name__)
Step 2
Define a simple route for the app and run the app:
import flask
app = flask.Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == '__main__':
app.run()
For running the above code just use python app.py
or whatever your app name is. For verification go to 127.0.0.1:5000 or the localhost with a port of 5000.
Now let's say for any reason you don't have your port 5000 free what will we do?
Simply change the last line of app.run to app.run(host = '127.0.0.1', port = 800)
for example if you want to run on port 800.
You will be able to see Hello World! written in the web browser.
Step 3
Now let's start building the API and first build the template for the whole app and then add in the code stub that we want. The template sort of would look like this:-
import flask
app = flask.Flask(__name__)
tasks = {}
id = 0
@app.route("/add",methods=['GET', 'POST'])
def add_task():
return "Hello World!"
@app.route("/update",methods=['GET', 'POST'])
def update_task():
return "Hello World"
@app.route("/delete",methods=['GET', 'POST'])
def delete_task():
return "Hello World"
@app.route("/get",methods=['GET', 'POST'])
def get_task():
return "Hello World"
if __name__ == '__main__':
app.run(host = '127.0.0.1', port = 800)
Now, let's start adding in the code part for each route.
- Add Task:-
For adding tasks we will need a JSON input which we will receive from the POST part.
import flask
app = flask.Flask(__name__)
tasks = {}
@app.route("/add",methods=['GET', 'POST'])
def add_task():
task = flask.request.get_json(force=True)
tasks[task["id"]] = task["taskid"]
return "Recieved Request"
@app.route("/update",methods=['GET', 'POST'])
def update_task():
return "Hello World"
@app.route("/delete",methods=['GET', 'POST'])
def delete_task():
return "Hello World"
@app.route("/get",methods=['GET', 'POST'])
def get_task():
return tasks
if __name__ == '__main__':
app.run(host = '127.0.0.1', port = 800)
We take in a JSON input and set the ids and tasks for that particular task See the below image for testing the API for adding the task.
Now let's move on to the rest of the routes.
- Update:-
For this particular, we require the id and the updated task.
import flask
app = flask.Flask(__name__)
tasks = {}
@app.route("/add",methods=['GET', 'POST'])
def add_task():
task = flask.request.get_json(force=True)
tasks[task["id"]] = task["taskid"]
return "Recieved Request"
@app.route("/update",methods=['GET', 'POST'])
def update_task():
updated_task = flask.request.get_json(force=True)
tasks[updated_task["id"]] = updated_task["taskid"]
return "Updated Request"
@app.route("/delete",methods=['GET', 'POST'])
def delete_task():
return "Hello World"
@app.route("/get",methods=['GET', 'POST'])
def get_task():
return tasks
if __name__ == '__main__':
app.run(host = '127.0.0.1', port = 800)
For checking out the update route pass the same form of data in JSON format with a POST request.
- Delete:-
For this, we only need the id for which the task is to be deleted. So from the rest, we only need the id as the JSON input.
import flask
app = flask.Flask(__name__)
tasks = {}
@app.route("/add",methods=['GET', 'POST'])
def add_task():
task = flask.request.get_json(force=True)
tasks[task["id"]] = task["taskid"]
return "Recieved Request"
@app.route("/update",methods=['GET', 'POST'])
def update_task():
updated_task = flask.request.get_json(force=True)
tasks[updated_task["id"]] = updated_task["taskid"]
return "Updated Request"
@app.route("/delete",methods=['GET', 'POST'])
def delete_task():
delete_id = flask.request.get_json(force=True)
del tasks[delete_id["id"]]
return "Deleted the task"
@app.route("/get",methods=['GET', 'POST'])
def get_task():
return tasks
if __name__ == '__main__':
app.run(host = '127.0.0.1', port = 800)
- Get all the Tasks:-
This will be a simple GET request with no body input so just have to return tasks.
import flask
app = flask.Flask(__name__)
tasks = {}
@app.route("/add",methods=['GET', 'POST'])
def add_task():
task = flask.request.get_json(force=True)
tasks[task["id"]] = task["taskid"]
return "Recieved Request"
@app.route("/update",methods=['GET', 'POST'])
def update_task():
updated_task = flask.request.get_json(force=True)
tasks[updated_task["id"]] = updated_task["taskid"]
return "Updated Request"
@app.route("/delete",methods=['GET', 'POST'])
def delete_task():
delete_id = flask.request.get_json(force=True)
del tasks[delete_id["id"]]
return "Deleted the task"
@app.route("/get",methods=['GET', 'POST'])
def get_task():
return tasks
if __name__ == '__main__':
app.run(host = '127.0.0.1', port = 800)
Now your app is a complete Todo app with all the functionalities without any DB required for only one-time use till the server runs. For now, quit the existing server and run the server again and check all the functionalities using POSTMAN.
In the next article, we will connect a DB and do the same task. Till then keep coding.
Happy Coding
Sarvesh Dubey