Simple CRUD API
March 15, 2026
Question
Build a simple Flask REST API for managing student records. You can consider this as the starter data :
students = [
{"id": 1, "name": "Pradyumna", "age": 24, "course": "Computer Science", "city": "Austin"},
{"id": 2, "name": "Rahul", "age": 22, "course": "Data Science", "city": "Dallas"},
{"id": 3, "name": "Ananya", "age": 21, "course": "Electronics", "city": "Houston"},
{"id": 4, "name": "Kiran", "age": 23, "course": "Computer Science", "city": "Austin"},
{"id": 5, "name": "Sachin", "age": 20, "course": "Mechanical", "city": "Seattle"}
]The tasks include :
- GET /students : Get all students
- GET /students/:id : Get a student by id
- POST /students : Create a new student
- PUT /students/:id : Update a student by id
- DELETE /students/:id : Delete a student by id
- Search /students?name=:name : Search students by course and city
Solution
So given the in memory data, we are building a simple CRUD API for managing student records.
The APIs would be GET, POST, PUT, DELETE. Lets start with the GET API.
# api endpoint : /students
@app.route('/students',methods = ['GET'])
def get_students() :
return jsonify(students)
Now lets GET a student by id.
# api endpoint : /students/:student_id
@app.route("/students/<int:student_id>", methods = ['GET'])
def get_students_by_id(student_id: int):
for student in students:
if student['id'] == student_id:
return jsonify(student)
return jsonify({"error": "Student not found"}), 404Now lets POST a new student.
# api endpoint : /students
@app.route("/students", methods = ['POST'])
def create_student():
data = request.json
new_Student = {
"id": len(students) + 1,
"name" : data['name'],
"age" : data["age"],
"course" : data["course"],
"city" : data["city"]
}
students.append(new_Student)
return jsonify(new_Student), 201
Now lets PUT an existing student.
# api endpoint : /students/:student_id
@app.route("/students/<int:student_id>", methods = ['PUT'])
def update_student(student_id: int):
data = request.json
for student in students:
if student['id'] == student_id:
student['name'] = data['name']
student['age'] = data['age']
student['course'] = data['course']
student['city'] = data['city']
return jsonify(student), 200
return jsonify({"error": "Student not found"}), 404Now lets DELETE a student.
# api endpoint : /students/:student_id
@app.route("/students/<int:student_id>", methods = ['DELETE'])
def delete_student(student_id: int):
for student in students:
if student['id'] == student_id:
students.remove(student)
return jsonify({"message": "Student deleted successfully"}), 200
return jsonify({"error": "Student not found"}), 404Lets search students by course and city.
# api endpoint : /students?course=:course&city=:city
@app.route("/students", methods = ['GET'])
def search_students() :
course = request.args.get('course')
city = request.args.get('city')
results = []
for student in students :
if student['course'] == course and student['city'] == city :
results.append(student)
return jsonify(results), 200Follow up questions
- Now introduce the concept of pagination to the API.
- Consider now the data grows, and we store it in a database.
THis would be the solution to the pagination problem.
# api endpoint : /students?page=:page&limit=:limit
@app.route("/students", methods = ['GET'])
def get_students_paginated() :
page = request.args.get('page', 1, type = int)
limit = request.args.get('limit', 10, type = int)
start = (page - 1) * limit
end = start + limit
return jsonify(students[start:end]), 200Now if i say that the data is stored in a database, and we need to fetch the data from the database, then i would do it this way..
@app.route("/students", methods = ['GET'])
def get_students():