DCSA-API-Design-Principles 1.1 Final
DCSA-API-Design-Principles 1.1 Final
September 2021
Table of
contents
1 Introduction _________________________________________________ 5
2 Suitable ____________________________________________________ 6
3 Maintainable _________________________________________________ 6
4 Stable _____________________________________________________ 13
5 Secure _____________________________________________________ 14
6 Performant __________________________________________________ 15
7 Well-documented ______________________________________________ 15
Tables
Table 1: API quality characteristics __________________________________________ 5
Term Definition
CRUD Create, Read, Update and Delete (equivalent to POST, GET, PUT/PATCH
and DELETE rest operations)
FK Foreign Key
kebab-case kebab-case combines words by replacing each space with a dash (-).
All letters are lower case. For example, this-is-a-kebab-case-example.
PK Primary Key
The structure of this documents follows the order of the API quality characteristics mentioned in
the table above.
2 Suitable
Good APIs are suitable regarding their functionality and responsibilities. Suitable APIs provide a
well-defined service that optimizes value for consumers by having clearly defined responsibilities
and an appropriate level of abstraction.
- The API is constructed with the consumer in mind and SHOULD be based on User Stories.
- The API is constructed according to a minimalistic approach less is more.
3 Maintainable
APIs need to be maintainable. This means it should be easy to understand how to interact with
the APIs, and development of maintainable code should be facilitated. The requirements to
achieve good maintainability are described in this chapter.
3.1 JSON
Requests and responses MUST be in The application/json Media Type for JavaScript Object
Notation (JSON).
Requests and responses MAY be in other formats as well e.g. XML Media Types.
3.2 URLs
MUST point to a resource (entity, process).
MUST consist of nouns and MUST NOT be actions. HTTP verbs (GET, PUT, PATCH, POST, DELETE)
MUST be used for actions.
Kebab-case MUST be used in URLs. Path parameters MUST be consistent with property names.
Path parameters referring to property names MUST use camelCase.
3.3 Collections
Collection items MUST NOT consist of composite keys. A unique key MUST identify a collection
element. Collections SHOULD be pluralized.
3.4 Sorting
Sorting SHOULD be limited to specific fields. The parameter MUST be restricted to the values ASC
(ascending order) and DESC (descending order) describing the sort direction. The direction MAY
be omitted. If omitted ascending order MUST be used. A colon : is used to separate the field to
sort by and the direction. Multiple fields can be used, in which case each field is separated by a
comma,. The following table shows some examples:
The sort parameter is optional and if not specified, the order is set by the implementer.
It is up to the implementer to find a suitable Collator to use when sorting on String values.
3.5 Pagination
GET requests on collection results SHOULD implement pagination. Links to current, previous, next,
first and last page SHOULD be available in the response headers; more links MAY be present.
Table 3: Pagination
The default page size SHOULD be 100, if it is not specified on the endpoint. The default page size
MAY be changed per endpoint. If the response payload is large, the page size SHOULD be
changed to a smaller, more suitable number. The consumer SHOULD be able to override the
default page size via the limit-parameter.
Keyset-based pagination MUST be used. The server generates all links to pages relative to the
current page requested. It is not possible for the consumer to request a specific page the
consumer MUST select a page provided by the server. The server MUST provide links to the
previous and next page (if possible) and SHOULD provide links to first and last page.
If the filter and/or sorting used when getting a collection result is changed, the pagination MUST
be reset to first page.
It is important to note that the pagination represents a cut of the data at a specific moment in
time. If somebody else triggers the same search a minute later, they might get a different data
set for the same filtering/sorting criteria.
Property names MUST NOT include FK (Foreign Key) or PK (Primary Key), as this exposes database
design.
3.8 Arrays
Empty arrays MUST NOT be represented with null but MUST be empty lists [].
Time properties MUST be suffixed with A Time property only contains a specific time of a
day in 24-hour-format using the following format: hh:mm. Time format MAY include seconds in
which case the following is added :ss.
DateTime properties MUST be suffixed with A DateTime property contains both a date
and time.
Date, Time and DateTime MUST be presented using the ISO 8601 format.
Example Explanation
The following 5 timestamps represent the same time period in different Time Zones:
3.10 UTF-8
Encoding MUST be UTF-8.
API-Version In requests: used to specify the version of the contract (API version)
requested by the consumer.
When binary data is part of the payload (in a property), it MUST be Base64 encoded.
400 Bad Request invalidQuery The request query was invalid. Check the
documentation to ensure that the supplied
parameters are supported, and check if the
request contains an invalid combination of
parameters, or an invalid value.
400 Bad Request invalidUri The requested URI is not represented on the
server.
404 Not found notFound The requested resource could not be found.
405 Method not httpMethodNotAllowed The HTTP method for the request is not
allowed supported.
occurs.
429 Too many rateLimitExceeded Too many requests have been sent recently.
requests
A Retry-After header SHOULD be added to
notify consumer when to try again.
500 Internal server internalError The request failed due to an internal error.
error
404 Not Found MAY be returned instead of 400 Bad Request, 401 Unauthorized or 403 Forbidden
for sensitive information. This can be used when the implementer of the API does not want to
verify if restricted information is available.
Requests rejected before reaching the API (rejected at Gateway level) MAY omit the error object.
4 Stable
APIs need to be stable over time, which means the occurrence and impact of major changes are
minimized. The requirements to achieve stability are described in this chapter.
4.1 Versioning
Semantic Versioning 2.0 MUST be used to specify version information restricted to the format
<MAJOR>.<MINOR>.<PATCH>.
Pre-releases (rule 9 from Sematic Versioning) and build metadata (rule 10) MUST NOT be used
in API version information.
URI versioning MUST be used. Only <MAJOR> version is included example: /v2/events. First
version SHOULD include /v1/. An API-Version custom header MAY be added to the request; if
added it MUST only contain <MAJOR> version. API-Version header MUST be aligned with the URI
version.
A custom header called API-Version MUST be added in the response-specifying version. The API-
Version includes all three formats example: API-Version: 2.0.0.
Subscriber to the API SHOULD be robust when receiving a Message (Subscriber and Message as
defined in the Subscription model):
Implementors of the API MUST NOT be more than 1 major version behind the latest version. No
more than 3 parallel major versions SHOULD be available.
4.3 Deprecation
Deprecated endpoints MUST be documented in OpenAPI specification using the
property introduced in OpenAPI 3.0.
Deprecated endpoints SHOULD add Deprecation and Sunset headers to responses. See The
Deprecation Header Field and The Sunset http Response Header Field.
A Link header providing additional information SHOULD be added in combination with the
Deprecation header. If added, the link provided MUST point to the documentation for additional
information.
5 Secure
APIs need to be secure, which means measures and practices are implemented to prevent
abuse. The requirements to achieve this objective are described in this chapter.
Note that the security of the Receiv endpoint in the Subscription model is
exempt from this section as it uses a different security scheme, which is documented in the DCSA
Subscription Callback API publication.
6 Performant
The performance consequences of API design need to be considered, monitored, and optimized
for common interactions.
- Circuit Breaker Pattern SHOULD be used for fast fail. If the implementer of the API has internal
errors or a database that is not responding - it would be a good idea to add a Circuit Breaker
Pattern in order for the endpoint to respond quickly, instead of waiting for Timeout.
- Rate Limiting SHOULD be used to prevent too many requests.
7 Well-documented
Up-to-date, complete, and easy-to-read documentation needs to be available to facilitate
adoption. The requirements to achieve this objective are described in this chapter.
GitHub MUST contain the latest documents and links to the latest documents:
https://github.com/dcsaorg/DCSA-OpenAPI
7.2 HATEOAS
Where possible, header links SHOULD be added to discover relations to the resource requested.