API Standards

URL patterns to follow

For example, we build APIs for users
Add User - /user/add
Update User - /user/update/:id
Delete User - /user/delete/:id
View User - /user/view/:id
List Users - /users

Response Status Codes

200 (Request processed successfully.)
201 (Record created successfully.)
400 (Bad Request. Required parameters missing.)
401 (Unauthorized)
403 (Forbidden - User not having required permissions.)
404 (Not found)
500 (Internal Server Error)

Request-Response Structure

1) Add User
URL: /user/add
Method: POST
Request
Header
Authorization:token
Content-type: application/json
Body
  • e.g.
  • {
                                                                            "first_name":"First Name",
                                                                            "last_name":"Last Name",
                                                                            "email":"test@test.com",
                                                                            "contact":"789789789789"
                                                                        } 
  • Response
    Success
    Status Code - 201
    Error
    Validation Error
    Status Code - 400
  • e.g.
  • {"errors":{"first_name":{"message":"First name is required."}}}
  • Internal Server Error
    Status Code - 500
    2) Update User
    URL: /user/update/:id
    Method: POST
    Request
    Header
    Authorization:token
    Content-type: application/json
    Body
  • e.g.
  • {
                                                                            "first_name":"First Name",
                                                                            "last_name":"Last Name",
                                                                            "email":"test@test.com",
                                                                            "contact":"789789789789"
                                                                        } 
  • Response
    Success
    Status Code - 200
    Error
    Validation Error
    Status Code - 400
  • e.g.
  • {"errors":{"first_name":{"message":"First name is required."}}}
  • User Not Found
    Status Code - 404
    Internal Server Error
    Status Code - 500
    3) View User
    URL: /user/view/:id
    Method: GET
    Request
    Header
    Authorization:token
    Response
    Success
    Status Code - 200
  • e.g.
  • {
                                                                                "id":"1",	
                                                                                    "first_name":"First Name",
                                                                                "last_name":"Last Name",
                                                                                "email":"test@test.com",
                                                                                "contact":"789789789789"
                                                                            }
                                                                            
  • Error
    User Not Found
    Status Code - 404
    Internal Server Error
    Status Code - 500
    4) Delete User
    URL: /user/delete/:id
    Method: DELETE
    Request
    Header
    Authorization:token
    Response
    Success
    Status Code - 200
    Error
    User Not Found
    Status Code - 404
    Internal Server Error
    Status Code - 500
    5) List Users
    URL: /users
    Method: GET
    Request
    Header
    Authorization:token
    Query Parameters
    page:1
    filter:{role:"admin"}
    Response
    Success
    Status Code - 200
  • {
                                                                                "users": [
                                                                                    {
                                                                                        "id":"1",
                                                                                        "first_name":"First Name",
                                                                                        "last_name":"Last Name",
                                                                                        "email":"test@test.com",
                                                                                        "contact":"789789789789"
                                                                                    }
                                                                                ],
                                                                                "total_users": 1,
                                                                                "records_per_page": 1
                                                                            }
                                                                            
  • Error
    Internal Server Error
    Status Code - 500