A guide for designing APIs.
- Use JSON for API request and response bodies.
- Use
snake_case
for JSON keys, notcamelCase
. - Use
ActiveModel::Serializers
for APIs served by Rails.
- Prefer RESTful "resources".
- Use the appropriate HTTP verb.
GET
,DELETE
, andPUT
requests should be idempotent.GET
requests should have no side effects.
- Use
:only
to whitelist the routes that are exposed for a resource.
resources :users, only: [:index, :show]
- Use namespaces to logically group API functions.
- Prefer shallow nesting of resources.
- Use the
Accept-Language
HTTP header. - Localize data values on the client, not the server.
- Return date and times in ISO-8601 format.
- Use the appropriate HTTP status code to indicate the error to the client.
- Include any applicable error messages in the response body:
{
errors: ["First name is required", "Date of birth is required"]
}
{
errors: ["This example just has one error, but we wrap it in 'errors' for consistency."]
}
- TBD when we need to version an API.
- TBD when we need to deprecate an API.