These recommendations describe how all Code Camp APIs should be implemented.
- Validation errors should return 422 response code.
- Authentication errors should return 401 response code.
- All request to save data should implement the POST http request method.
- Successfull POST requests to create new records should return 201 response code.
- Successfull GET, DELETE requests should return 200 response code.
- If something goes wrong, return 400 response code.
Provide the following data set in your response:
- message: This should have an understandable reason for why you returned a specific response code or just OK for get requests with a data entity.
- status: This should hold either of error, info, warning, or error depending on the response code or message.
- code: This should be the same as the returned response code.
- data: This should either be an an empty object or an object holding all the required data set.
{
"message": "OK",
"status": "success",
"code": 200,
"data": {
"user": {
"id": 1,
"username": "realralphg",
"email": "[email protected]",
"name": "Raphael Issah",
"photo": "https://demosite.com/photos/realralphg/18236391_12312321_01278219.jpg"
},
"posts": []
}
}
Registration and Authentication requests should return the same EXTRA DATA set as other requests with the addition of an authentication token.
{
"message": "OK",
"message": "Registration was successful",
"status": "success",
"code": 201,
"token": "1|0912u312m312nn83z321noxsermxew34",
"data": {
"user": {
"id": 1,
"username": "realralphg",
"email": "[email protected]",
"name": "Raphael Issah",
"photo": "https://demosite.com/photos/default.jpg"
},
}
}
Authentication should be implemented by passing the authentication token as a Bearer token to the Authorization header property.
{
"headers": {
"Authorization": "Bearer 1|0912u312m312nn83z321noxsermxew34"
}
}
All data properties should be named in such manner that describes the data held by the property.
- Plural Nouns should be used to identify collections.
- Singular Nouns should identify single collection items.
{
"data": {
"user": {
"username": "realralphg",
...
},
"posts": [
{
"id": 1,
...
},
{
"id": 2,
...
},
...
]
}
}
Use plural nouns for collections, this plural naming convention becomes a global code. This also helps normal people to understand that these groups of APIs form a collection.
Collection | Role |
---|---|
/users | list all users |
/users/123 | specific user |
/users/123/posts | list of posts that belong to a specific user |
/users/123/posts/321 | specific post of a specific users post list |
This document is only a recommendation and is subject to change without prior notice.
Author: 3m1n3nc3 for Greysoft Code Camp