Activity 35: Documentation of your Basic CRUD API (SIA2)

Monette NicolasMonette Nicolas
2 min read

Basic CRUD API Documentation

Overview

This API is designed for managing a list of students, offering basic CRUD operations to add, view, update, and delete student information. Each student record includes attributes like name, section, ID, email, and age.

Base URL

If the API is running on a local server, the base URL will be:

http://localhost:5000

Endpoints

1. GET /students

  • Description: Fetches the list of all students.

  • Method: GET

  • URL: /students

Response:

  • Status Code: 200 OK

  • Body:

      [
          {
              "name": "Juan Carlos",
              "section": "BSIT-4B",
              "id": 1,
              "email": "juan@gmail.com",
              "age": 22
          },
          {
              "name": "Jose Rizal",
              "section": "BSIT-2A",
              "id": 2,
              "email": "jose@gmail.com",
              "age": 21
          }
      ]
    

2. GET /students/<student_id>

  • Description: Fetches details of a specific student by ID.

  • Method: GET

  • URL: /students/<student_id>

  • Parameters:

    • Path: student_id (integer) - ID of the student to retrieve.

Response:

  • Status Code: 200 OK

  • Body (example for ID 1):

      {
          "name": "Juan Carlos",
          "section": "BSIT-4B",
          "id": 1,
          "email": "juan@gmail.com",
          "age": 22
      }
    

Error Response:

  • Status Code: 404 Not Found

  • Body:

      {
          "message": "Student not found"
      }
    

3. POST /students

  • Description: Adds a new student to the list.

  • Method: POST

  • URL: /students

  • Body (JSON format):

      {
          "name": "Maria Clara",
          "section": "BSIT-3C",
          "email": "maria@gmail.com",
          "age": 20
      }
    

Response:

  • Status Code: 201 Created

  • Body:

      {
          "name": "Maria Clara",
          "section": "BSIT-3C",
          "id": 3,
          "email": "maria@gmail.com",
          "age": 20
      }
    

4. PUT /students/<student_id>

  • Description: Updates details of an existing student by ID.

  • Method: PUT

  • URL: /students/<student_id>

  • Parameters:

    • Path: student_id (integer) - ID of the student to update.
  • Body (JSON format with fields to update):

      {
          "section": "BSIT-4A",
          "age": 23
      }
    

Response:

  • Status Code: 200 OK

  • Body:

      {
          "name": "Juan Carlos",
          "section": "BSIT-4A",
          "id": 1,
          "email": "juan@gmail.com",
          "age": 23
      }
    

Error Response:

  • Status Code: 404 Not Found

  • Body:

      {
          "message": "Student not found"
      }
    

5. DELETE /students/<student_id>

  • Description: Deletes a student by ID.

  • Method: DELETE

  • URL: /students/<student_id>

  • Parameters:

    • Path: student_id (integer) - ID of the student to delete.

Response:

  • Status Code: 200 OK

  • Body:

      {
          "message": "Student deleted"
      }
    

Error Response:

  • Status Code: 404 Not Found

  • Body:

      {
          "message": "Student not found"
      }
    

Running the API

  1. Activate the Virtual Environment: (on Windows)

     .\myenv\Scripts\activate
    
  2. Run the API Server:

     python app.py
    

Testing the API

You can test the endpoints using Postman or by sending HTTP requests directly.

0
Subscribe to my newsletter

Read articles from Monette Nicolas directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Monette Nicolas
Monette Nicolas