This is a simple REST API built with Python and FastAPI and MongoDB for CRUD operations (Create, Read, Update, Delete) on Students. FastAPI is a powerful web framework for building APIs.
Install the project dependencies:
pip install -r requirements.txt
Run the application:
venv\Scripts\activate
uvicorn main:app --reload
The application will start and be available at http://localhost:8000.
GET /students
Returns a list of all students in the system:
curl http://localhost:8000/students/ -H "Accept: application/json"
Response:
[
{
"id": "7a2d72f9e92d4231ac6dd834aa7461d1",
"name": "Jai",
"age": 26,
"address":
{
"city": "Delhi",
"country": "India"
}
},
{
"id": "4a1dddb9d74e427d9f8e6fdbe2e112a2",
"name": "Adi",
"age": 20,
"address":
{
"city": "Jaipur",
"country": "India"
}
},
{
"id": "95f3bee04dad487eb54ae61a457201ea",
"name": "Anuj",
"age": 23,
"address":
{
"city": "Pune",
"country": "India"
}
}
]
GET /students/{user_id}
Returns details for a specific user with the given user_id:
curl http://localhost:8000/students/95f3bee04dad487eb54ae61a457201ea -H "Accept: application/json"
Response:
{
"id": "95f3bee04dad487eb54ae61a457201ea",
"name": "Anuj",
"age": 23,
"address":
{
"city": "Pune",
"country": "India"
}
}
POST /students
API to create a student in the system. All fields are mandatory and required while creating the student in the system.
name
(string, required): the name of the userage
(string, required): the email address of the useraddress
(string, required): the address for the user which includes city and country.
curl -X POST http://localhost:8000/students/
-H 'Content-Type: application/json'
-d '{
"age": 31,
"name": "Shyam",
"address":
{
"country": "Japan",
"city": "Tokyo"
}
}'
Response:
{
"id": "34c8b3111bf34e1f8fbff33cb6b0a593"
}
PATCH /students/{user_id}
Updates an existing user with the given user_id. The request body should include a JSON object with the following properties:
name
(string): the new name for the userage
(int): the new age for the usercity
(string): the new city for the usercountry
(string): the new country for the user
curl -X PUT http://localhost:8000/students/95f3bee04dad487eb54ae61a457201ea
-H "Accept: application/json"
-d '{"age": 24, "address":{"city": "Jaipur"}}'
Response:
{}
DELETE /students/{user_id}
Deletes the user with the given user_id:
curl -X DELETE http://localhost:8000/{student_id}
Response:
{}
Code credits for this code go to Aman Khandelwal