REST API design principles
Information on using the REST API scheme and principles of its design.
REST API scheme
Common scheme for our REST API is <VERB> [namespace] <objects> <id> <action>.
VERBcan bePOST,GET,PATCH,PUT,DELETE.namespaceshould scope some specific functionality likeauth,lambda. It is optional in the scheme.- Typical
objectsaretasks,projects,jobs. - When you want to extract a specific object from a collection, just specify its
id. - An
actioncan be used to simplify REST API or provide an endpoint for entities withoutobjectsendpoint likeannotations,data,data/meta. Note: action should not duplicate other endpoints without a reason.
Design principles
- Use nouns instead of verbs in endpoint paths. For example,
POST /api/tasksinstead ofPOST /api/tasks/create. - Accept and respond with JSON whenever it is possible
- Name collections with plural nouns (e.g.
/tasks,/projects) - Try to keep the API structure flat. Prefer two separate endpoints
for
/projectsand/tasksinstead of/projects/:id1/tasks/:id2. Use filters to extract necessary information like/tasks/:id2?project=:id1. In some cases it is useful to get alltasks. If the structure is hierarchical, it cannot be done easily. Also you have to know both:id1and:id2to get information about the task. Note: for now we acceptGET /tasks/:id2/jobsbut it should be replaced by/jobs?task=:id2in the future. - Handle errors gracefully and return standard error codes (e.g.
201,400) - Allow filtering, sorting, and pagination
- Maintain good security practices
- Cache data to improve performance
- Versioning our APIs. It should be done when you delete an endpoint or modify
its behaviors. Versioning uses a schema with
Acceptheader with vendor media type.
Links
- Best practices for REST API design
- Flat vs. nested resources
- REST API Design Best Practices for Sub and Nested Resources
- A specification for building APIs in JSON
Last modified
March 25, 2022
: Add a link on JSON:API (2fb2a6a9)