API Design Guidelines #24
Replies: 2 comments 3 replies
-
|
Thanks for putting this proposal together Caleb. This is an excellent place to start. I've included my suggestions and questions inline below.
I think it'd be helpful to provide a quick example here similar to how you listed the CRUD operations above. Also linking out to the spec is a helpful addition: https://jsonapi.org/format/#document-resource-identifier-objects
Do we just want to say, use HTTPS all the time, and any URL that requests only HTTP should automatically be redirected to the HTTPS version?
I suggest including this link: https://jsonapi.org/format/#fetching-filtering
I suggest including this link: https://jsonapi.org/format/#fetching-pagination
I'm guessing that data would be the
I don't see
And Meta:
Errors:
I suggest including this link: https://jsonapi.org/format/#fetching-resources-responses
I suggest including this link for CREATE of a new resource: https://jsonapi.org/format/#crud-creating-responses
It'd be good to include a CREATE, UPDATE and DELETE example here too
|
Beta Was this translation helpful? Give feedback.
-
|
@jhodapp Thank you for excellent the feedback on this! I made some updates to the guidelines based on your suggestions. In particular, I modified the first guideline to:
as a sort of fallback disclaimer so that we do not need to reiterate the entire spec in our guidelines here. Let me know if that seems adequate to you. Trying to account the inevitable things that will fall through the cracks. This way we'll have something to refer to in those situations. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Proposal For Adopting a Set of Standardized API Guidelines
Goal:
To define a set of standard conventions that we agree to adhere to in order to build an ergonomic, functional, cohesive API.
Proposed Guidlines
General Design Principles
Utilize JSON API specification for consistent data formatting and structure in cases not covered by these guidelines.
Employ standard HTTP methods for CRUD operations (
GET,POST,PUT,DELETE).Name resources with singular nouns and collections with plural nouns (
/Userfor a single user,/Usersfor a collection ).Implement resource identification using unique identifiers (UUID).
Always use HTTPS. Any attempts to use HTTP should automatically be redirected to HTTPS counterpart.
Filtering
Allow filtering resources by specifying criteria in the query string.
Support common filter operators like
eq,ne,gt,lt,gte,lte,in, andnin.Enable filtering multiple attributes using a comma-separated list.
Use JSON API syntax for filter parameters (e.g.,
?filter[name]=John).Pagination
Implement pagination for large datasets to optimize performance.
Provide pagination parameters
page[number]andpage[size]in the query string.Include pagination information in the response header (e.g., Link) and meta field.
Implement cursor-based pagination for efficient handling of large datasets and updates.
JSON API Response Format
Utilize JSON API format for response structure, including:
data: An object representing the requested resource or collection.meta: Additional information about the response.errors: An array of error objects if the request fails.Utilize standard HTTP status codes to communicate the outcome of the request. Here are some of the most common codes and their appropriate usage:
200OK: The request was successful and the requested resource is available in the response.201Created: The request successfully created a new resource.202Accepted: The request has been accepted for processing, but the processing has not yet been completed.204No Content: The request was successful, but there is no content to return in the response.301Moved Permanently: The requested resource has been permanently moved to a new location.302Found: The requested resource has been temporarily moved to a new location.400Bad Request: The request is malformed or contains invalid syntax.401Unauthorized: The request requires authentication and the user is not authorized.403Forbidden: The user is authorized but does not have permission to access the requested resource.404Not Found: The requested resource could not be found.405Method Not Allowed: The requested method is not supported for the requested resource.422Unprocessable Entity: The request was well-formed but the server could not process it due to semantic errors.500Internal Server Error: An unexpected error occurred on the server.503Service Unavailable: The server is currently unavailable.Generic Examples:
Request (GET with filtering):
GET /api/v1/users?filter[name]=John&filter[age][gt]=18Response (successful):
{ "data": [ { "id": 1, "type": "users", "attributes": { "name": "John", "age": 25 }, } ], "meta": { "pagination": { "page": 1, "per_page": 10, "total_pages": 2 } } }Request (GET with pagination):
GET /api/v1/users?page[number]=2&page[size]=5Response (successful):
{ "data": [ { "id": 6, "type": "users", "attributes": { "name": "John", "age": 25 }, }, // ... other users ], "meta": { "pagination": { "page": 2, "per_page": 5, "total_pages": 3 } } }Request (GET with error):
GET /api/v1/users?filter[invalid_attribute]=invalid_valueResponse (error):
{ "errors": [ { "status": 400, "code": "INVALID_FILTER_ATTRIBUTE", "title": "Invalid Filter Attribute", "detail": "The attribute 'invalid_attribute' is not valid for filtering." } ] }Resources:
Beta Was this translation helpful? Give feedback.
All reactions