Vienna Insurance Group API Guidelines

- Vienna Insurance Group API Guidelines
- 1. Introduction
- 2. Principles
- 3. General guidelines
- 4. Meta information
- 5. Security
- 6. Compatibility
- MUST not break backward compatibility
- SHOULD prefer compatible extensions
- MUST prepare clients accept compatible API extensions
- SHOULD design APIs conservatively
- MUST always return JSON objects as top-level data structures
- MUST treat Open API specification as open for extension by default
- SHOULD use open-ended list of values (
x-extensible-enum) for enumerations - SHOULD avoid versioning
- MUST use media type versioning
- MUST not use URI versioning
- 7. Deprecation
- MUST obtain approval of clients before API shutdown
- MUST collect external partner consent on deprecation time span
- MUST reflect deprecation in API specifications
- MUST monitor usage of deprecated API scheduled for sunset
- SHOULD add
DeprecationandSunsetheader to responses - SHOULD add monitoring for
DeprecationandSunsetheader - MUST not start using deprecated APIs
- 8. JSON guidelines
- MUST property names must be ASCII snake_case (and never camelCase):
^[a-z_][a-z_0-9]*$ - MUST declare enum values using UPPER_SNAKE_CASE format
- SHOULD define maps using
additionalProperties - SHOULD pluralize array names
- MUST not use
nullfor boolean properties - MUST use same semantics for
nulland absent properties - SHOULD declare optional fields as
nullable: true - MUST not treat empty string
""as null or absent property - SHOULD not use
nullfor empty arrays - SHOULD represent enumerations as strings
- SHOULD name date/time properties with
_atsuffix (exception: birthdate) - SHOULD define date properties compliant with RFC 3339
- SHOULD define time durations and intervals properties conform to ISO 8601
- MUST property names must be ASCII snake_case (and never camelCase):
- 9. Data formats
- MUST use JSON to encode structured data
- MAY use non-JSON media types for binary data or alternative content representations
- SHOULD encode embedded binary data in
base64url - SHOULD prefer standard media type name
application/json - SHOULD use standardized property formats
- MUST use standard date and time formats
- SHOULD use standards for country, language and currency codes
- MUST define format for number and integer types
- 10. Common data types
- 11. API naming
- MUST use lowercase separate words with hyphens for path segments
- MUST use snake_case (never camelCase) for query parameters
- SHOULD prefer hyphenated-pascal-case for HTTP header fields
- MUST pluralize resource names
- SHOULD not use /api as base path
- MUST avoid trailing slashes
- MUST stick to conventional query parameters
- 12. Resources
- MUST avoid actions — think about resources
- SHOULD model complete business processes
- SHOULD define useful resources
- MUST keep URLs verb-free
- MUST use domain-specific resource names
- MUST use URL-friendly resource identifiers: [a-zA-Z0-9:._\-/]*
- MUST identify resources and sub-resources via path segments
- MAY expose compound keys as resource identifiers
- MAY consider using (non-)nested URLs
- SHOULD only use UUIDs if necessary
- SHOULD limit number of resource types
- SHOULD limit number of sub-resource levels
- 13. HTTP requests
- MUST use HTTP methods correctly
- MUST fulfill common method properties
- SHOULD consider to design
POSTandPATCHidempotent - SHOULD use secondary key for idempotent
POSTdesign - MUST define collection format of header and query parameters
- SHOULD design simple query languages using query parameters
- SHOULD design complex query languages using JSON
- MUST document implicit filtering
- 14. HTTP status codes and errors
- 15. Performance
- 16. Pagination
- 17. Hypermedia
- 18. Common headers
- 19. Proprietary headers
- 20. API Operation
- 21. SOAP Guidelines
- Appendix A: References
- Appendix B: Tooling
- Appendix C: Best practices
- Appendix D: Changelog and References
1. Introduction
VIG’s software architecture centers around decoupled microservices that provide functionality via RESTful APIs with a JSON payload. Small engineering teams own, deploy and operate these microservices in their AWS (team) accounts. Our APIs most purely express what our systems do, and are therefore highly valuable business assets.
Those guidelines must be used for any newly developed services or any existing services to be adopted. With this in mind, we’ve adopted 'API First' as one of our key engineering principles. Microservices development begins with API definition outside the code and ideally involves ample peer review feedback to achieve high quality APIs. 'API First' encompasses a set of quality-related standards and fosters a peer review culture including a lightweight review procedure. We encourage our teams to follow them to ensure that our APIs
-
are easy to understand and learn
-
are general and abstracted from specific implementation and use cases
-
are robust and easy to use
-
have a common look and feel
-
follow a consistent RESTful style and syntax
-
are consistent with other teams’ APIs and our global architecture
Ideally, all VIG APIs will look like the same author has created them.
API Governance
In cases when breaking API changes are deemed unavoidable, the API evangelist and API champion have to be involved in the change. Without their explicit permission breaking changes must not be published.
TODO Establish API evangelist and API champion roles and make teams aware of the roles and how to approach breaking API changes.
Conventions used in these guidelines
The requirement level keywords "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" used in this document (case insensitive) are to be interpreted as described in RFC 2119.
2. Principles
API design principles
Comparing SOA web service interfacing style of SOAP vs. REST, the former tends to be centered around operations that are usually use case-specific and specialized. In contrast, REST is centered around business (data) entities exposed as resources that are identified via URIs and can be manipulated via standardized CRUD-like methods using different representations, and hypermedia. RESTful APIs tend to be less use case-specific and come with less rigid client / server coupling and are more suitable for an ecosystem of (core) services We apply the RESTful web service principles to all kinds of application (micro-) service components, independently from whether they provide functionality via the internet or intranet.
-
We prefer REST-based APIs with JSON payloads
-
We prefer systems to be truly RESTful [1]
An important principle for API design and usage is Postel’s Law, aka The Robustness Principle (see also RFC 1122):
-
Be liberal in what you accept, be conservative in what you send
Readings: Some interesting reads on the RESTful API design style and service architecture:
-
Book: Irresistable APIs: Designing web APIs that developers will love
-
InfoQ eBook: Web APIs: From Start to Finish
-
Lessons-learned blog: Thoughts on RESTful API Design
-
Fielding Dissertation: Architectural Styles and the Design of Network-Based Software Architectures
API as a product
A public API doesn’t mean that it is openly accessible by anybody. Depending on the service and the data, the consumer must follow a certain registration and validation process. Furthermore, there will be restricted APIs which will be only available for regulated companies - this will be handled via API audiences (see rule 219)
APIs should be based on the API as a product principle:
-
Treat your API as product and act like a product team
-
Put yourself into the place of your customers; be an advocate for their needs
-
Emphasize simplicity, comprehensibility, and usability of APIs to make them irresistible for client engineers
-
Actively improve and maintain API consistency over the long term
-
Make use of customer feedback and provide service level support
Embracing 'API as a Product' facilitates a service ecosystem which can be evolved more easily and used to experiment quickly with new business ideas by recombining core capabilities.
Understand the specifc use cases of your customers and carefully check the trade-offs of your API design variants with a product mindset. Avoid short-term implementation optimizations at the expense of unnecessary client side obligations, and have a high attention on API quality and client developer experience.
API as a Product is closely related to our API First principle (see next chapter) which is more focused on how we engineer high quality APIs.
API first
For newly developed APIs or extensions of an existing API you must follow an API first approach, which bascially requires two aspects:
-
define APIs first, before coding its implementation, using a standard specification language
-
get early review feedback from peers and client developers
By defining APIs outside the code, we want to facilitate early review feedback and also a development discipline that focus on service interface design …
-
profound understanding of the domain and required functionality
-
generalized business entities / resources, i.e. avoidance of use case specific APIs
-
clear separation of WHAT vs. HOW concerns, i.e. abstraction from implementation aspects — APIs should be stable even if we replace the entire service implementation including its underlying technology stack
Moreover, API definitions with standardized specification format also facilitate…
-
single source of truth for the API specification; it is a crucial part of a contract between service provider and client users
-
infrastructure tooling for API discovery, API GUIs, API documents, automated quality checks
Elements of 'API First' are also these API Guidelines and a standardized API review process as to get early review feedback from peers and client developers. Peer review is important for us to get high quality APIs, to enable architectural and design alignment and to supported the development of client applications decoupled from the service provider engineering life cycle.
It is important to learn that 'API First' is not in conflict with the agile development principles that we love. Service applications should evolve incrementally — and so its APIs. Of course, our API specification will and should evolve iteratively in different cycles; however, each starting with draft status and early team and peer review feedback. API may change and profit from implementation concerns and automated testing feedback. API evolution during development life cycle may include breaking changes for not yet productive features as long as we have aligned the changes with the clients. Hence, 'API First' does not mean that you must have 100% domain and requirement understanding and can never produce code before you have defined the entire API and get it confirmed by peer review. On the other hand, 'API First' obviously is in conflict with the bad practice of publishing API definition and asking for peer review after the service integration or even the service productive operation has started. It is crucial to request and get early feedback — as early as possible, but not before the API changes are comprehensive with focus to the next evolution step and have a certain quality (including API Guideline compliance), already confirmed via team internal reviews.
3. General guidelines
The titles are marked with the corresponding labels: MUST, SHOULD, MAY.
MUST follow 'API First' Principle
You must follow the 'API First' Principle, more specifically:
-
You must define APIs first, before coding its implementation, using Open API as specification language
-
You must call for early review feedback from peers and client developers
-
TODO Establish a lean API review process
-
TODO API linter service for automatic rule checks
-
TODO Define API audiences and tooling for realization in VIG
MUST provide API specification using Open API
We use the Open API specification as standard to define API specification files. API designers are required to provide the API specification using a single self-contained YAML file to improve readability. For any newly developed services you must use Open API 3.0, existing services that already use Open API 2.0 (a.k.a. Swagger 2) are for now still supported.
The API specification files should be subject to version control using a source code management system - best together with the implementing sources.
-
TODO How to publish APIs and make them discoverable
Hint: A good way to explore Open API 3.0/2.0 is to navigate through the Open API specification mind map and use the Swagger Plugin for IntelliJ IDEA to create your first API. To explore and validate/evaluate existing APIs the Swagger Editor.
MUST only use durable and immutable remote references
Normally, API specification files must be self-contained, i.e. files
should not contain references to local or remote content, e.g. ../fragment.yaml#/element or
$ref: 'https://github.com/zalando/zally/blob/master/server/src/main/resources/api/zally-api.yaml#/schemas/LintingRequest'.
The reason is, that the content referred to is in general not durable and
not immutable. As a consequence, the semantic of an API may change in
unexpected ways.
SHOULD provide API user manual
In addition to the API Specification, it is good practice to provide an API user manual to improve client developer experience, especially of engineers that are less experienced in using this API. A helpful API user manual typically describes the following API aspects:
-
API scope, purpose, and use cases
-
specific examples of API usage
-
edge cases, error situation details, and repair hints
-
architecture context and major dependencies - including figures and sequence flows
The user manual must be published online, e.g. via our documentation hosting
platform service, GHE pages, or specific team web servers. Please do not forget
to include a link to the API user manual into the API specification using the
#/externalDocs/url property.
4. Meta information
MUST contain API meta information
API specifications must contain the following Open API meta information to allow for API management:
-
#/info/titleas (unique) identifying, functional descriptive name of the API -
#/info/versionto distinguish API specifications versions following semantic rules -
#/info/descriptioncontaining a proper description of the API -
#/info/contact/{name,url,email}containing the responsible team
Following Open API extension properties must be provided in addition:
-
#/info/x-api-idunique identifier of the API (see rule 215) -
#/info/x-audienceintended target audience of the API (see rule 219)
MUST use semantic versioning
Open API allows to specify the API specification version in
#/info/version. To share a common semantic of version information we
expect API designers to comply to
Semantic Versioning 2.0 rules 1 to 8 and 11 restricted to the format
<MAJOR>.<MINOR>.<PATCH> for versions as follows:
-
Increment the MAJOR version when you make incompatible API changes after having aligned this changes with consumers,
-
Increment the MINOR version when you add new functionality in a backwards-compatible manner, and
-
Optionally increment the PATCH version when you make backwards-compatible bug fixes or editorial changes not affecting the functionality.
Additional Notes:
-
Pre-release versions (rule 9) and build metadata (rule 10) must not be used in API version information.
-
While patch versions are useful for fixing typos etc, API designers are free to decide whether they increment it or not.
-
API designers should consider to use API version
0.y.z(rule 4) for initial API design.
Example:
openapi: 3.0.1
info:
title: Parcel Service API
description: API for <...>
version: 1.3.7
<...>
MUST provide API identifiers
Each API specification must be provisioned with a globally unique and
immutable API identifier. The API identifier is defined in the info-block
of the Open API specification and must conform to the following definition:
/info/x-api-id:
type: string
format: urn
pattern: ^[a-z0-9][a-z0-9-:.]{6,62}[a-z0-9]$
description: |
Mandatory globally unique and immutable API identifier. The API
id allows to track the evolution and history of an API specification
as a sequence of versions.
API specifications will evolve and any aspect of an Open API specification may change. We require API identifiers because we want to support API clients and providers with API lifecycle management features, like change trackability and history or automated backward compatibility checks. The immutable API identifier allows the identification of all API specification versions of an API evolution. By using API semantic version information or API publishing date as order criteria you get the version or publication history as a sequence of API specifications.
Note: While it is nice to use human readable API identifiers based on self-managed URNs, it is recommend to stick to UUIDs to relief API designers from any urge of changing the API identifier while evolving the API. Example:
openapi: 3.0.1
info:
x-api-id: d0184f38-b98d-11e7-9c56-68f728c1ba70
title: Parcel Service API
description: API for <...>
version: 1.5.8
<...>
MUST provide API audience
TODO Establish API audiences suitable for VIG, viesure and twinformatics
Each API must be classified with respect to the intended target audience supposed to consume the API, to facilitate differentiated standards on APIs for discoverability, changeability, quality of design and documentation, as well as permission granting. We differentiate the following API audience groups with clear organisational and legal boundaries:
Example audiences as used by Zalando
- component-internal
-
This is often referred to as a team internal API or a product internal API. The API consumers with this audience are restricted to applications of the same functional component which typically represents a specific product with clear functional scope and ownership. All services of a functional component / product are owned by a specific dedicated owner and engineering team(s). Typical examples of component-internal APIs are APIs being used by internal helper and worker services or that support service operation.
- business-unit-internal
-
The API consumers with this audience are restricted to applications of a specific product portfolio owned by the same business unit.
- company-internal
-
The API consumers with this audience are restricted to applications owned by the business units of the same the company (e.g. Zalando company with Zalando SE, Zalando Payments SE & Co. KG. etc.)
- external-partner
-
The API consumers with this audience are restricted to applications of business partners of the company owning the API and the company itself.
- external-public
-
APIs with this audience can be accessed by anyone with Internet access.
Note: a smaller audience group is intentionally included in the wider group and thus does not need to be declared additionally.
The API audience is provided as API meta information in the info-block of
the Open API specification and must conform to the following specification:
/info/x-audience:
type: string
x-extensible-enum:
- component-internal
- business-unit-internal
- company-internal
- external-partner
- external-public
description: |
Intended target audience of the API. Relevant for standards around
quality of design and documentation, reviews, discoverability,
changeability, and permission granting.
Note: Exactly one audience per API specification is allowed. For this reason a smaller audience group is intentionally included in the wider group and thus does not need to be declared additionally. If parts of your API have a different target audience, we recommend to split API specifications along the target audience — even if this creates redundancies (rationale (internal link)).
Example:
openapi: 3.0.1
info:
x-audience: company-internal
title: Parcel Helper Service API
description: API for <...>
version: 1.2.4
<...>
For details and more information on audience groups see the API Audience narrative (internal link).
5. Security
MUST secure endpoints with OAuth 2.0 and an API key
-
TODO This will need work with regards to the existing IDP solution
Every API endpoint needs to be secured using OAuth 2.0 and an API key. Please refer to the Authentication section of the official Open API specification on how to specify security definitions in your API.
The following code snippet shows how to define the authorization OAuth2 and API Key
components:
securitySchemes:
apiKey:
type: apiKey
in: header
name: X-API-KEY
description: >
To retrieve the API key, go to the **My Apps** section in the
**Developer Portal**,
click on the desired app name and copy the `KEY` from the **API Keys**
section
clientCredentials:
type: oauth2
flows:
clientCredentials:
tokenUrl: https://api-dev.viesure.io/oauth2/token
scopes:
travel-insurance.documents.read: Read the documents for offers and quotes you created
travel-insurance.sessions.create: Create a new session
travel-insurance.sessions.read: Read the sessions you created
travel-insurance.quotes.create: Create the quotes for travel data
travel-insurance.offers.create: Create the offers for quotes you created
travel-insurance.offers.read: Read the offers you created
travel-insurance.payments.update: Update the payments for offers you created
travel-insurance.payments.read: Read the payments you created
travel-insurance.policies.read: Read the policies you created and the corresponding documents
The next code snippet applies this security scheme to all API endpoints. The bearer token of the client must have additionally the scopes scope_1 and scope_2.
security:
- BearerAuth: [ scope_1, scope_2 ]
MUST define and assign permissions (scopes)
| Access Type | Description |
|---|---|
|
|
|
|
|
|
|
|
APIs must define permissions to protect their resources. Thus, at least one permission must be assigned to each endpoint. Permissions are defined as shown in the previous section.
The naming schema for permissions corresponds to the naming schema for event type names. Please refer to MUST follow naming convention for permissions (scopes) for designing permission names.
APIs should stick to component-specific permissions without resource extension to avoid governance complexity of too many fine grained permissions. For the majority of use cases, restricting access to specific API endpoints using read and write is sufficient for controlling access for client types like merchant or retailer business partners, customers or operational staff. However, in some situations, where the API serves different types of resources for different owners, resource-specific scopes may make sense.
Some examples for standard and resource-specific permissions:
| Application ID | Resource ID | Access Type | Example |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
After permission names are defined and the permission is declared in the security definition at the top of an API specification, it must be assigned to each API operation by specifying a security requirement like this:
paths:
/business-partners/{partner-id}:
get:
summary: Retrieves information about a business partner
security:
- BearerAuth: [ business-partner-service.get ]
MUST follow naming convention for permissions (scopes)
As long as the functional naming is not supported for permissions, permission names in APIs must conform to the following naming pattern:
<permission> ::= <standard-permission> | -- should be sufficient for majority of use cases
<resource-permission> | -- for special security access differentiation use cases
<standard-permission> ::= <application-id>.<access-mode>
<resource-permission> ::= <application-id>.<resource-name>.<access-mode>
<application-id> ::= [a-z][a-z0-9-]* -- application identifier
<resource-name> ::= [a-z][a-z0-9-]* -- free resource identifier
<access-mode> ::= get | put | delete -- might be extended in future
This pattern is compatible with the previous definition.
6. Compatibility
MUST not break backward compatibility
Change APIs, but keep all consumers running. Consumers usually have independent release lifecycles, focus on stability, and avoid changes that do not provide additional value. APIs are contracts between service providers and service consumers that cannot be broken via unilateral decisions.
There are two techniques to change APIs without breaking them:
-
follow rules for compatible extensions
-
introduce new API versions and still support older versions
We strongly encourage using compatible API extensions and discourage versioning (see SHOULD avoid versioning and MUST use media type versioning below). The following guidelines for service providers (SHOULD prefer compatible extensions) and consumers (MUST prepare clients accept compatible API extensions) enable us (having Postel’s Law in mind) to make compatible changes without versioning.
Note: There is a difference between incompatible and breaking changes. Incompatible changes are changes that are not covered by the compatibility rules below. Breaking changes are incompatible changes deployed into operation, and thereby breaking running API consumers. Usually, incompatible changes are breaking changes when deployed into operation. However, in specific controlled situations it is possible to deploy incompatible changes in a non-breaking way, if no API consumer is using the affected API aspects (see also Deprecation guidelines).
Hint: Please note that the compatibility guarantees are for the "on the wire" format. Binary or source compatibility of code generated from an API definition is not covered by these rules. If client implementations update their generation process to a newer version of the API definition, it has to be expected that code changes are necessary.
SHOULD prefer compatible extensions
API designers should apply the following rules to evolve RESTful APIs for services in a backward-compatible way:
-
Add only optional, never mandatory fields.
-
Never change the semantic of fields (e.g. changing the semantic from customer-number to customer-id, as both are different unique customer keys)
-
Input fields may have (complex) constraints being validated via server-side business logic. Never change the validation logic to be more restrictive and make sure that all constraints are clearly defined in description.
-
Enum ranges can be reduced when used as input parameters, only if the server is ready to accept and handle old range values too. Enum range can be reduced when used as output parameters.
-
Enum ranges cannot be extended when used for output parameters — clients may not be prepared to handle it. However, enum ranges can be extended when used for input parameters.
-
Use
x-extensible-enum, if range is used for output parameters and likely to be extended with growing functionality. It defines an open list of explicit values and clients must be agnostic to new values. (See SHOULD use open-ended list of values (x-extensible-enum) for enumerations) -
Support redirection in case an URL has to change 301 (Moved Permanently).
MUST prepare clients accept compatible API extensions
Service clients should apply the robustness principle:
-
Be conservative with API requests and data passed as input, e.g. avoid to exploit definition deficits like passing megabytes of strings with unspecified maximum length.
-
Be tolerant in processing and reading data of API responses, more specifically…
Service clients must be prepared for compatible API extensions of service providers:
-
Be tolerant with unknown fields in the payload (see also Fowler’s "TolerantReader" post), i.e. ignore new fields but do not eliminate them from payload if needed for subsequent
PUTrequests. -
Be prepared that
x-extensible-enumreturn parameter may deliver new values; either be agnostic or provide default behavior for unknown values. -
Be prepared to handle HTTP status codes not explicitly specified in endpoint definitions. Note also, that status codes are extensible. Default handling is how you would treat the corresponding 2xx code (see RFC 7231 Section 6).
-
Follow the redirect when the server returns HTTP status code 301 (Moved Permanently).
SHOULD design APIs conservatively
Designers of service provider APIs should be conservative and accurate in what they accept from clients:
-
Unknown input fields in payload or URL should not be ignored; servers should provide error feedback to clients via an HTTP 400 response code.
-
Be accurate in defining input data constraints (like formats, ranges, lengths etc.) — and check constraints and return dedicated error information in case of violations.
-
Prefer being more specific and restrictive (if compliant to functional requirements), e.g. by defining length range of strings. It may simplify implementation while providing freedom for further evolution as compatible extensions.
Not ignoring unknown input fields is a specific deviation from Postel’s Law
(e.g. see also
The
Robustness Principle Reconsidered) and a strong recommendation. Servers might
want to take a different approach but should be aware of the following problems
and be explicit in what is supported:
-
Ignoring unknown input fields is actually not an option for
PUT, since it becomes asymmetric with subsequentGETresponse and HTTP is clear about thePUTreplace semantics and default roundtrip expectations (see RFC 7231 Section 4.3.4). Note, accepting (i.e. not ignoring) unknown input fields and returning it in subsequentGETresponses is a different situation and compliant toPUTsemantics. -
Certain client errors cannot be recognized by servers, e.g. attribute name typing errors will be ignored without server error feedback. The server cannot differentiate between the client intentionally providing an additional field versus the client sending a mistakenly named field, when the client’s actual intent was to provide an optional input field.
-
Future extensions of the input data structure might be in conflict with already ignored fields and, hence, will not be compatible, i.e. break clients that already use this field but with different type.
In specific situations, where a (known) input field is not needed anymore, it either can stay in the API definition with "not used anymore" description or can be removed from the API definition as long as the server ignores this specific parameter.
MUST always return JSON objects as top-level data structures
In a response body, you must always return a JSON object (and not e.g. an array) as a top level data structure to support future extensibility. JSON objects support compatible extension by additional attributes. This allows you to easily extend your response and e.g. add pagination later, without breaking backwards compatibility. See SHOULD use pagination links where applicable for an example.
Maps (see SHOULD define maps using additionalProperties), even though technically objects, are also forbidden as top
level data structures, since they don’t support compatible, future extensions.
MUST treat Open API specification as open for extension by default
The Open API 2.0 specification is not very specific on default extensibility
of objects, and redefines JSON-Schema keywords related to extensibility, like
additionalProperties. Following our overall compatibility guidelines, Open
API object definitions are considered open for extension by default as per
Section
5.18 "additionalProperties" of JSON-Schema.
When it comes to Open API 2.0, this means an additionalProperties declaration
is not required to make an object definition extensible:
-
API clients consuming data must not assume that objects are closed for extension in the absence of an
additionalPropertiesdeclaration and must ignore fields sent by the server they cannot process. This allows API servers to evolve their data formats. -
For API servers receiving unexpected data, the situation is slightly different. Instead of ignoring fields, servers may reject requests whose entities contain undefined fields in order to signal to clients that those fields would not be stored on behalf of the client. API designers must document clearly how unexpected fields are handled for
PUT,POST, andPATCHrequests. If no documentation is given, the request must be denied with a 400 Bad Request. See SHOULD design APIs conservatively.
API formats must not declare additionalProperties to be false, as this
prevents objects being extended in the future.
Note that this guideline concentrates on default extensibility and does not
exclude the use of additionalProperties with a schema as a value, which might
be appropriate in some circumstances, e.g. see SHOULD define maps using additionalProperties.
SHOULD use open-ended list of values (x-extensible-enum) for enumerations
Enumerations are per definition closed sets of values, that are assumed to be complete and not intended for extension. This closed principle of enumerations imposes compatibility issues when an enumeration must be extended. To avoid these issues, we strongly recommend to use an open-ended list of values instead of an enumeration unless:
-
the API has full control of the enumeration values, i.e. the list of values does not depend on any external tool or interface, and
-
the list of value is complete with respect to any thinkable and unthinkable future feature.
To specify an open-ended list of values use the marker x-extensible-enum as
follows:
deliver_methods:
type: string
x-extensible-enum:
- PARCEL
- LETTER
- EMAIL
Note: x-extensible-enum is not JSON Schema conform but will be ignored by
most tools.
SHOULD avoid versioning
When changing your RESTful APIs, do so in a compatible way and avoid generating additional API versions. Multiple versions can significantly complicate understanding, testing, maintaining, evolving, operating and releasing our systems (supplementary reading).
If changing an API can’t be done in a compatible way, then proceed in one of these three ways:
-
create a new resource (variant) in addition to the old resource variant
-
create a new service endpoint — i.e. a new application with a new API (with a new domain name)
-
create a new API version supported in parallel with the old API by the same microservice
As we discourage versioning by all means because of the manifold disadvantages, we strongly recommend to only use the first two approaches.
MUST use media type versioning
However, when API versioning is unavoidable, you have to design your multi-version RESTful APIs using media type versioning (instead of URI versioning, see below). Media type versioning is less tightly coupled since it supports content negotiation and hence reduces complexity of release management.
Media type versioning: Here, version information and media type are provided
together via the HTTP Content-Type header — e.g.
application/x.vig.contract+json;version=2. For incompatible changes, a new
media type version for the resource is created. To generate the new
representation version, consumer and producer can do content negotiation using
the HTTP Content-Type and Accept headers. Note: This versioning only applies to
the request and response content schema, not to URI or method semantics.
In this example, a client wants only the new version of the response:
Accept: application/x.vig.contract+json;version=2
A server responding to this, as well as a client sending a request with content should use the Content-Type header, declaring that one is sending the new version:
Content-Type: application/x.vig.contract+json;version=2
Using header versioning should:
-
include versions in request and response headers to increase visibility
-
include Content-Type in the Vary header to enable proxy caches to differ between versions
Hint: Until an incompatible change is necessary, it is recommended to stay
with the standard application/json media type.
Further reading: API Versioning Has No "Right Way" provides an overview on different versioning approaches to handle breaking changes without being opinionated.
MUST not use URI versioning
With URI versioning a (major) version number is included in the path, e.g.
/v1/customers. The consumer has to wait until the provider has been released
and deployed. If the consumer also supports hypermedia links — even in their
APIs — to drive workflows (HATEOAS), this quickly becomes complex. So does
coordinating version upgrades — especially with hyperlinked service
dependencies — when using URL versioning. To avoid this tighter coupling and
complexer release management we do not use URI versioning, and go instead with
media type versioning and content negotiation (see above).
7. Deprecation
Sometimes it is necessary to phase out an API endpoint, an API version, or an API feature, e.g. if a field or parameter is no longer supported or a whole business functionality behind an endpoint is supposed to be shut down. As long as the API endpoints and features are still used by consumers these shut downs are breaking changes and not allowed. To progress, the following deprecation rules have to be applied to make sure that the necessary consumer changes and actions are well communicated and aligned using deprecation and sunset dates.
MUST obtain approval of clients before API shutdown
Before shutting down an API, version of an API, or API feature, the producer
must make sure that all clients have given their consent on a sunset date.
Producers should help consumers to migrate to a potential new API or API
feature by providing a migration manual and clearly state the timeline for
replacement availability and sunset (see also SHOULD add Deprecation and Sunset header to responses). Once all clients of
a sunset API feature are migrated, the producer may shut down the deprecated
API feature.
The minimum lifetime of an API is set to 1 year and the initial shutdown must be communicated at least 6 month in advance. Any deviations from those timeframes have to be cleared by the API Champion.
MUST collect external partner consent on deprecation time span
If the API is consumed by any external partner, the API owner must define a reasonable time span that the API will be maintained after the producer has announced deprecation. All external partners must state consent with this after-deprecation-life-span, i.e. the minimum time span between official deprecation and first possible sunset, before they are allowed to use the API.
MUST reflect deprecation in API specifications
The API deprecation must be part of the API specification.
If an API endpoint (operation object), an input argument (parameter object),
an in/out data object (schema object), or on a more fine grained level, a
schema attribute or property should be deprecated, the producers must set
deprecated: true for the affected element and add further explanation to the
description section of the API specification. If a future shutdown is
planned, the producer must provide a sunset date and document in detail
what consumers should use instead and how to migrate.
MUST monitor usage of deprecated API scheduled for sunset
Owners of an API, API version, or API feature used in production that is scheduled for sunset must monitor the usage of the sunset API, API version, or API feature in order to observe migration progress and avoid uncontrolled breaking effects on ongoing consumers. See also MUST monitor API usage.
SHOULD add Deprecation and Sunset header to responses
During the deprecation phase, the producer should add a Deprecation: <date-time>
(see draft: RFC
Deprecation HTTP Header) and - if also planned - a Sunset: <date-time> (see
RFC 8594) header on each response affected by a
deprecated element (see MUST reflect deprecation in API specifications).
The Deprecation header can either be set to true - if a feature is retired
-, or carry a deprecation time stamp, at which a replacement will become/became
available and consumers must not on-board any longer (see MUST not start using deprecated APIs). The optional
Sunset time stamp carries the information when consumers have to stop
using a feature the latest. The sunset date should always offer an eligible time interval
for switching to a replacement feature.
Deprecation: Sun, 31 Dec 2024 23:59:59 GMT
Sunset: Sun, 31 Dec 2025 23:59:59 GMT
If multiple elements are deprecated, the Deprecation and Sunset headers are
expected to be set to the earliest time stamp to reflect the shortest interval
consumers are expected to get active.
Note: adding the Deprecation and Sunset header is not sufficient to gain
client consent to shut down an API or feature.
Hint: In earlier guideline versions, we used the Warning header to provide
the deprecation info to clients. However, the Warning header has less specific
semantics, will be obsolete with
draft: RFC HTTP
Caching, and our syntax was not compliant with RFC 7234 — Warning header.
SHOULD add monitoring for Deprecation and Sunset header
Clients should monitor the Deprecation and Sunset headers in HTTP responses
to get information about future sunset of APIs and API features (see SHOULD add Deprecation and Sunset header to responses).
We recommend that client owners build alerts on this monitoring information to
ensure alignment with service owners on required migration task.
Hint: In earlier guideline versions, we used the Warning header to provide
the deprecation info (see hint in SHOULD add Deprecation and Sunset header to responses).
MUST not start using deprecated APIs
Clients must not start using deprecated APIs, API versions, or API features.
8. JSON guidelines
These guidelines provide recommendations for defining JSON data at VIG. JSON here refers to RFC 7159 (which updates RFC 4627), the "application/json" media type and custom JSON media types defined for APIs. The guidelines clarify several specific cases to allow VIG’s JSON data to have an idiomatic form across teams and services.
The first ones of the following guidelines are about property names, the later ones about values.
MUST property names must be ASCII snake_case (and never camelCase): ^[a-z_][a-z_0-9]*$
-
TODO Check if snake_case can be easily activated in e.g. jackson
-
TODO Get detailed developer feedback if we want to stick to camelCase, snake_case or both
Property names are restricted to ASCII strings. The first character must be a letter, or an underscore, and subsequent characters can be a letter, an underscore, or a number.
(It is recommended to use _ at the start of property names only for keywords like _links.)
Rationale: No established industry standard exists, but many popular Internet companies prefer snake_case: e.g. GitHub, Stack Exchange, Twitter. Others, like Google and Amazon, use both - but not only camelCase. It’s essential to establish a consistent look and feel so that JSON code looks as if written by the same person.
MUST declare enum values using UPPER_SNAKE_CASE format
Enum values (using enum or x-extensible-enum) need to consistently use the UPPER_SNAKE_CASE format, e.g. VALUE or YET_ANOTHER_VALUE. This approach allows to clearly distinguish values from properties or other elements.
SHOULD define maps using additionalProperties
A "map" here is a mapping from string keys to some other type. In JSON this is represented as an object, the key-value pairs being represented by property names and property values. In Open API schema (as well as in JSON schema) they should be represented using additionalProperties with a schema defining the value type. Such an object should normally have no other defined properties.
The map keys don’t count as property names in the sense of rule 118 and can follow whatever format is natural for their domain. Please document this in the description of the map object’s schema.
Here is an example for such a map definition (the translations property):
components:
schemas:
Message:
description:
A message together with translations in several languages.
type: object
properties:
message_key:
type: string
description: The message key.
translations:
description:
The translations of this message into several languages.
The keys are [IETF BCP-47 language tags](https://tools.ietf.org/html/bcp47).
type: object
additionalProperties:
type: string
description:
the translation of this message into the language identified by the key.
An actual JSON object described by this might then look like this:
{ "message_key": "color",
"translations": {
"de": "Farbe",
"en-US": "color",
"en-GB": "colour",
"eo": "koloro",
"nl": "kleur"
}
}
SHOULD pluralize array names
To indicate they contain multiple values prefer to pluralize array names. This implies that object names should in turn be singular.
MUST not use null for boolean properties
Schema-based JSON properties that are boolean by design must not be presented as nulls. A boolean is essentially a closed enumeration of two values, true and false. If the content has a meaningful null value, strongly prefer to replace the boolean with enumeration of named values or statuses - for example accepted_terms_and_conditions with true or false can be replaced with terms_and_conditions with values yes, no and unknown.
MUST use same semantics for null and absent properties
Open API 3.x allows to mark properties as required and as nullable to
specify whether properties may be absent ({}) or null ({"example":null}).
If a property is defined to be not required and nullable (see
2nd row in Table below), this rule demands
that both cases must be handled in the exact same manner by the specification.
The following table shows all combinations and whether the examples are valid:
| required | nullable | {} | {"example":null} |
|---|---|---|---|
|
|
✗ No |
✔ Yes |
|
|
✔ Yes |
|
|
|
✗ No |
✗ No |
|
|
✔ Yes |
✗ No |
While API designers and implementers may be tempted to assign different semantics to both cases, we explicitly decide against that option, because we think that any gain in expressiveness is far outweighed by the risk of clients not understanding and implementing the subtle differences incorrectly.
As an example, an API that provides the ability for different users to
coordinate on a time schedule, e.g. a meeting, may have a resource for options
in which every user has to make a choice. The difference between undecided
and decided against any of the options could be modeled as absent and
null respectively. It would be safer to express the null case with a
dedicated Null object, e.g.
{} compared to {"id":"42"}.
Moreover, many major libraries have somewhere between little to no support for
a null/absent pattern (see
Gson,
Moshi,
Jackson,
JSON-B). Especially
strongly-typed languages suffer from this since a new composite type is required
to express the third state. Nullable Option/Optional/Maybe types could be
used but having nullable references of these types completely contradicts their
purpose.
The only exception to this rule is the JSON Merge Patch RFC 7396) which
uses null to explicitly indicate property deletion while absent properties are
ignored, i.e. not modified.
SHOULD declare optional fields as nullable: true
Optional fields should be declared as nullable: true. The reason behind this is to not differentiate between
absent ({}) and null ({"example": null}) properties and therefore, preventing confusion about their semantics.
Moreover, Jackson serializes null properties as {"example": null} by default.
MUST not treat empty string "" as null or absent property
An empty string ("") must be treated the same as any other string value, and is not an indication for an absent or null property.
SHOULD not use null for empty arrays
Empty array values can unambiguously be represented as the empty list, [].
SHOULD represent enumerations as strings
Strings are a reasonable target for values that are enumerations by design.
SHOULD name date/time properties with _at suffix (exception: birthdate)
Dates and date-time properties should end with _at to distinguish them from
boolean properties which otherwise would have very similar or even identical
names. An exception to this suffix is birthdate.
Examples:
-
created_atrather thancreated, -
modified_atrather thanmodified, -
occurred_atrather thanoccurred, and -
returned_atrather thanreturned.
SHOULD define date properties compliant with RFC 3339
Use the date and time formats defined by RFC 3339:
-
for "date" use strings matching
date-fullyear "-" date-month "-" date-mday, for example:2015-05-28 -
for "date-time" use strings matching
full-date "T" full-time, for example2015-05-28T14:07:17Z
Note that the
Open
API format "date-time" corresponds to "date-time" in the RFC) and 2015-05-28
for a date (note that the Open API format "date" corresponds to "full-date" in
the RFC). Both are specific profiles, a subset of the international standard
ISO 8601.
A zone offset may be used (both, in request and responses) — this is simply
defined by the standards. However, we encourage restricting dates to UTC and
without offsets. For example 2015-05-28T14:07:17Z rather than
2015-05-28T14:07:17+00:00. From experience we have learned that zone offsets
are not easy to understand and often not handled correctly. Note also that
zone offsets are different from local times that might be including daylight
saving time. Localization of dates should be done by the services that provide
user interfaces, if required.
When it comes to storage, all dates should be consistently stored in UTC without a zone offset. Localization should be done locally by the services that provide user interfaces, if required.
Sometimes it can seem data is naturally represented using numerical timestamps, but this can introduce interpretation issues with precision, e.g. whether to represent a timestamp as 1460062925, 1460062925000 or 1460062925.000. Date strings, though more verbose and requiring more effort to parse, avoid this ambiguity.
SHOULD define time durations and intervals properties conform to ISO 8601
Schema-based JSON properties that are by design durations and intervals could be strings formatted as recommended by ISO 8601(Appendix A of RFC 3339 contains a grammar for durations).
9. Data formats
MUST use JSON to encode structured data
Use JSON-encoded body payloads for transferring structured data. The JSON payload must follow RFC 7159 using a JSON object as top level data structure (if possible) to allow for future extension. This also applies to collection resources, where you would naturally assume an array. See also MUST always return JSON objects as top-level data structures.
Additionally, the JSON payload must comply to RFC 7493), particularly
-
Section 2.1 on encoding of characters, and
-
Section 2.3 on object constraints.
As a consequence, a JSON payload must
-
use
UTF-8encoding -
consist of valid Unicode strings, i.e. must not contain non-characters or surrogates, and
-
contain only unique member names (no duplicate names).
MAY use non-JSON media types for binary data or alternative content representations
Other media types may be used in following cases:
-
Transferring binary data or data whose structure is not relevant. This is the case if payload structure is not interpreted and consumed by clients as is. Example of such use case is downloading images in formats JPG, PNG, GIF.
-
In addition to JSON, version alternative data representations (e.g. in formats PDF, DOC, XML) may be made available through content negotiation.
SHOULD encode embedded binary data in base64url
Exposing binary data using an alternative media type is generally preferred. See the rule above.
If an alternative content representation is not desired, the binary data should
be embedded into the JSON document as a base64url-encoded string property
following RFC 7493 Section 4.4.
SHOULD prefer standard media type name application/json
Use the standard media type name application/json (or application/problem+json for MUST use problem JSON).
Custom media types beginning with x bring no advantage compared to the
standard media type for JSON, and make automated processing more difficult.
They are also discouraged by RFC 6838.
SHOULD use standardized property formats
JSON Schema and Open API define several universally useful property formats. The following table contains some additional formats that are particularly useful in an e-commerce environment.
Please notice that the list is not exhaustive and everyone is encouraged to propose additions.
type |
format |
Specification | Example |
|---|---|---|---|
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
|
|
|
|
||
|
|
||
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
||
|
|
||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
||
|
|
|
|
|
|
|
|
|
|
MUST use standard date and time formats
JSON payload
Read more about date and time formats in SHOULD define date properties compliant with RFC 3339.
HTTP headers
HTTP headers including the proprietary headers use the HTTP date format defined in RFC 7231.
SHOULD use standards for country, language and currency codes
Use the following standard formats for country, language and currency codes:
MUST define format for number and integer types
Whenever an API defines a property of type number or integer, the
precision must be defined by the format as follows to prevent clients
from guessing the precision incorrectly, and thereby changing the value
unintentionally:
| type | format | specified value range |
|---|---|---|
integer |
int32 |
integer between -231 and 231-1 |
integer |
int64 |
integer between -263 and 263-1 |
integer |
bigint |
arbitrarily large signed integer number |
number |
float |
IEEE 754-2008/ISO 60559:2011 binary32 decimal number |
number |
double |
IEEE 754-2008/ISO 60559:2011 binary64 decimal number |
number |
decimal |
arbitrarily precise signed decimal number |
The precision must be translated by clients and servers into the most
specific language types. E.g. for the following definitions the most
specific language types in Java will translate to BigDecimal for
Money.amount and int or Integer for the OrderList.page_size:
components:
schemas:
Money:
type: object
properties:
amount:
type: number
description: Amount expressed as a decimal number of major currency units
format: decimal
example: 99.95
...
OrderList:
type: object
properties:
page_size:
type: integer
description: Number of orders in list
format: int32
example: 42
10. Common data types
Definitions of data objects that are good candidates for wider usage:
MUST use the common money object
Use the following common money structure:
Money:
type: object
properties:
amount:
type: number
description: >
The amount describes unit and subunit of the currency in a single value,
where the integer part (digits before the decimal point) is for the
major unit and fractional part (digits after the decimal point) is for
the minor unit.
format: decimal
example: 99.95
currency:
type: string
description: 3 letter currency code as defined by ISO-4217
format: iso-4217
example: EUR
required:
- amount
- currency
APIs are encouraged to include a reference to the global schema for Money.
SalesOrder:
properties:
grand_total:
$ref: 'https://https://gitlab.viesure.io/open-api/api-guidelines/raw/master/models/money-1.0.0.yaml#/Money'
Please note that APIs have to treat Money as a closed data type, i.e. it’s not meant to be used in an inheritance hierarchy. That means the following usage is not allowed:
{
"amount": 19.99,
"currency": "EUR",
"discounted_amount": 9.99
}
Cons
-
Violates the Liskov Substitution Principle
-
Breaks existing library support, e.g. Jackson Datatype Money
-
Less flexible since both amounts are coupled together, e.g. mixed currencies are impossible
A better approach is to favor composition over inheritance:
{
"price": {
"amount": 19.99,
"currency": "EUR"
},
"discounted_price": {
"amount": 9.99,
"currency": "EUR"
}
}
Pros
-
No inheritance, hence no issue with the substitution principle
-
Makes use of existing library support
-
No coupling, i.e. mixed currencies is an option
-
Prices are now self-describing, atomic values
Notes
Please be aware that some business cases (e.g. transactions in Bitcoin) call for a higher precision, so applications must be prepared to accept values with unlimited precision, unless explicitly stated otherwise in the API specification.
Examples for correct representations (in EUR):
-
42.20or42.2= 42 Euros, 20 Cent -
0.23= 23 Cent -
42.0or42= 42 Euros -
1024.42= 1024 Euros, 42 Cent -
1024.4225= 1024 Euros, 42.25 Cent
Make sure that you don’t convert the "amount" field to float /
double types when implementing this interface in a specific language
or when doing calculations. Otherwise, you might lose precision.
Instead, use exact formats like Java’s
BigDecimal.
See Stack Overflow for more
info.
Some JSON parsers (NodeJS’s, for example) convert numbers to floats by default. After discussing the pros and cons we’ve decided on "decimal" as our amount format. It is not a standard Open API format, but should help us to avoid parsing numbers as float / doubles.
MUST use common field names and semantics
There exist a variety of field types that are required in multiple places. To achieve consistency across all API implementations, you must use common field names and semantics whenever applicable.
Generic fields
There are some data fields that come up again and again in API data:
-
id: the identity of the object. If used, IDs must be opaque strings and not numbers. IDs are unique within some documented context, are stable and don’t change for a given object once assigned, and are never recycled cross entities. -
xyz_id: an attribute within one object holding the identifier of another object must use a name that corresponds to the type of the referenced object or the relationship to the referenced object followed by_id(e.g.partner_idnotpartner_number, orparent_node_idfor the reference to a parent node from a child node, even if both have the typeNode). Exception: We usecustomer_numberinstead ofcustomer_idfor customer facing identification of customers due to legacy reasons. (Hint:customer_idused to be defined as internal only, technical integer key, see Naming Decision:customer_numbervscustomer_id[internal link]). -
created_at: when the object was created. If used, this must be adate-timeconstruct. Originally namedcreatedbefore adding the naming conventions for date/time properties. -
modified_at: when the object was updated. If used, this must be adate-timeconstruct. Originally namedmodifiedbefore adding the naming conventions for date/time properties. -
type: the kind of thing this object is. If used, the type of this field should be a string. Types allow runtime information on the entity provided that otherwise requires examining the Open API file. -
ETag: the ETag of an embedded sub-resource. It may be used to carry theETagfor subsequentPUT/PATCHcalls (seeETagsin result entities).
Example JSON schema:
tree_node:
type: object
properties:
id:
description: the identifier of this node
type: string
created_at:
description: when got this node created
type: string
format: 'date-time'
modified_at:
description: when got this node last updated
type: string
format: 'date-time'
type:
type: string
enum: [ 'LEAF', 'NODE' ]
parent_node_id:
description: the identifier of the parent node of this node
type: string
example:
id: '123435'
created_at: '2017-04-12T23:20:50.52Z'
modified_at: '2017-04-12T23:20:50.52Z'
type: 'LEAF'
parent_node_id: '534321'
These properties are not always strictly necessary, but making them idiomatic allows API client developers to build up a common understanding of VIG’s resources. There is very little utility for API consumers in having different names or value types for these fields across APIs.
Link relation fields
To foster a consistent look and feel using simple hypertext controls for paginating and iterating over collection values the response objects should follow a common pattern using the below field semantics:
-
self:the link or cursor in a pagination response or object pointing to the same collection object or page. -
first: the link or cursor in a pagination response or object pointing to the first collection object or page. -
prev: the link or cursor in a pagination response or object pointing to the previous collection object or page. -
next: the link or cursor in a pagination response or object pointing to the next collection object or page. -
last: the link or cursor in a pagination response or object pointing to the last collection object or page.
Pagination responses should contain the following additional array field to transport the page content:
To simplify user experience, the applied query filters may be returned using
the following field (see also GET With Body):
-
query: object containing the query filters applied in the search request to filter the collection resource.
As Result, the standard response page using pagination links is defined as follows:
ResponsePage:
type: object
properties:
self:
description: Pagination link pointing to the current page.
type: string
format: uri
first:
description: Pagination link pointing to the first page.
type: string
format: uri
prev:
description: Pagination link pointing to the previous page.
type: string
format: uri
next:
description: Pagination link pointing to the next page.
type: string
format: uri
last:
description: Pagination link pointing to the last page.
type: string
format: uri
query:
description: >
Object containing the query filters applied to the collection resource.
type: object
properties: ...
items:
description: Array of collection items.
type: array
required: false
items:
type: ...
The response page may contain additional metadata about the collection or the current page.
Address fields
Address structures play a role in different functional and use case contexts, including country variances. All attributes that relate to address information should follow the naming and semantics defined below.
addressee:
description: a (natural or legal) person that gets addressed
type: object
properties:
salutation:
description: |
a salutation and/or title used for personal contacts to some
addressee; not to be confused with the gender information!
type: string
example: Mr
first_name:
description: |
given name(s) or first name(s) of a person; may also include the
middle names.
type: string
example: Hans Dieter
last_name:
description: |
family name(s) or surname(s) of a person
type: string
example: Mustermann
business_name:
description: |
company name of the business organization. Used when a business is
the actual addressee; for personal shipments to office addresses, use
`care_of` instead.
type: string
example: Consulting Services GmbH
required:
- first_name
- last_name
address:
description:
an address of a location/destination
type: object
properties:
care_of:
description: |
(aka c/o) the person that resides at the address, if different from
addressee. E.g. used when sending a personal parcel to the
office /someone else's home where the addressee resides temporarily
type: string
example: Consulting Services GmbH
street:
description: |
the full street address including house number and street name
type: string
example: Schönhauser Allee 103
additional:
description: |
further details like building name, suite, apartment number, etc.
type: string
example: 2. Hinterhof rechts
city:
description: |
name of the city / locality
type: string
example: Berlin
zip:
description: |
zip code or postal code
type: string
example: 14265
country_code:
description: |
the country code according to
[iso-3166-1-alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)
type: string
example: DE
required:
- street
- city
- zip
- country_code
Grouping and cardinality of fields in specific data types may vary based on the specific use case (e.g. combining addressee and address fields into a single type when modeling an address label vs distinct addressee and address types when modeling users and their addresses).
11. API naming
MUST use lowercase separate words with hyphens for path segments
Example:
/shipment-orders/{shipment-order-id}
This applies to specifc path segments and not the names of path
parameters. For example {shipment_order_id} would be ok as a path
parameter.
MUST use snake_case (never camelCase) for query parameters
-
TODO Depends strongly on the decision concerning MUST property names must be ASCII snake_case (and never camelCase):
^[a-z_][a-z_0-9]*$
Examples:
customer_number, order_id, billing_address
SHOULD prefer hyphenated-pascal-case for HTTP header fields
This is for consistency in your documentation (most other headers follow this convention). Avoid camelCase (without hyphens). Exceptions are common abbreviations like "ID."
Examples:
Accept-Encoding
Apply-To-Redirect-Ref
Disposition-Notification-Options
Original-Message-ID
See also: HTTP Headers are case-insensitive (RFC 7230).
See Common headers and Proprietary headers sections for more guidance on HTTP headers.
MUST pluralize resource names
Usually, a collection of resource instances is provided (at least API should be ready here). The special case of a resource singleton is a collection with cardinality 1. Exceptions include: * "payment" * "status" * "vat"
SHOULD not use /api as base path
In most cases, all resources provided by a service are part of the public API, and therefore should be made available under the root "/" base path.
If the service should also support non-public, internal APIs
— for specific operational support functions, for example — we encourage
you to maintain two different API specifications and provide
API audience. For both APIs, you should not use /api as base path.
We see API’s base path as part of the deployment variant configuration. Therefore, this information has to be declared in the server object.
MUST avoid trailing slashes
The trailing slash must not have specific semantics. Resource paths must deliver the same results whether they have the trailing slash or not.
MUST stick to conventional query parameters
If you provide query support for searching, sorting, filtering, and paginating, you must stick to the following naming conventions:
-
q: default query parameter, e.g. used by browser tab completion; should have an entity-specific alias, e.g. sku. -
sort: comma-separated list of fields (as defined by MUST define collection format of header and query parameters) to define the sort order. To indicate sorting direction, fields may be prefixed with+(ascending) or-(descending), e.g. /sales-orders?sort=+id. -
fields: field name expression to retrieve only a subset of fields of a resource. See SHOULD support partial responses via filtering below. -
offset: numeric offset of the first element provided on a page representing a collection request. See Pagination section below. -
cursor: an opaque pointer to a page, never to be inspected or constructed by clients. It usually (encrypted) encodes the page position, i.e. the identifier of the first or last page element, the pagination direction, and the applied query filters to recreate the collection. See pagination section below. -
limit: client-suggested limit to restrict the number of entries on a page. See Pagination section below.
12. Resources
MUST avoid actions — think about resources
REST is all about your resources, so consider the domain entities that take
part in web service interaction and aim to model your API around these using
the standard HTTP methods as operation indicators. For instance, if an
application has to lock articles explicitly so that only one user may edit
them, create an article lock with PUT or POST instead of using a lock
action.
Request:
PUT /article-locks/{article-id}
The added benefit is that you already have a service for browsing and filtering article locks.
SHOULD model complete business processes
An API should contain the complete business processes containing all resources representing the process. This enables clients to understand the business process, foster a consistent design of the business process, allow for synergies from description and implementation perspective, and eliminates implicit invisible dependencies between APIs.
In addition, it prevents services from being designed as thin wrappers around databases, which normally tends to shift business logic to the clients.
SHOULD define useful resources
As a rule of thumb resources should be defined to cover 90% of all its client’s use cases. A useful resource should contain as much information as necessary, but as little as possible. A great way to support the last 10% is to allow clients to specify their needs for more/less information by supporting filtering and embedding.
MUST keep URLs verb-free
The API describes resources, so the only place where actions should appear is in the HTTP methods. In URLs, use only nouns. Instead of thinking of actions (verbs), it’s often helpful to think about putting a message in a letter box: e.g., instead of having the verb cancel in the url, think of sending a message to cancel an order to the cancellations letter box on the server side.
MUST use domain-specific resource names
API resources represent elements of the application’s domain model. Using domain-specific nomenclature for resource names helps developers to understand the functionality and basic semantics of your resources. It also reduces the need for further documentation outside the API definition. For example, "sales-order-items" is superior to "order-items" in that it clearly indicates which business object it represents. Along these lines, "items" is too general.
MUST use URL-friendly resource identifiers: [a-zA-Z0-9:._\-/]*
To simplify encoding of resource IDs in URLs, their representation must only consist of ASCII strings using letters, numbers, underscore, minus, colon, period, and - on rare occasions - slash.
Note: slashes are only allowed to build and signal resource identifiers consisting of compound keys.
MUST identify resources and sub-resources via path segments
Some API resources may contain or reference sub-resources. Embedded sub-resources, which are not top level resources, are parts of a higher level resource and cannot be used outside of its scope. Sub-resources should be referenced by their name and identifier in the path segments as follows:
/resources/{resource-id}/sub-resources/{sub-resource-id}
In order to improve the consumer experience, you should aim for intuitively
understandable URLs, where each sub-path is a valid reference to a resource or
a set of resources. E.g. if /customers/12ev123bv12v/addresses/DE_100100101 is valid,
then /customers/12ev123bv12v/addresses, /customers/12ev123bv12v and
/customers must be valid as well in principle. E.g.:
/customers/12ev123bv12v/addresses/DE_100100101
/customers/12ev123bv12v
/shopping-carts/de:1681e6b88ec1/items/1
/shopping-carts/de:1681e6b88ec1
/content/images/9cacb4d8
/content/images
Note: resource identifiers may be build of multiple other resource identifiers (see MAY expose compound keys as resource identifiers).
MAY expose compound keys as resource identifiers
If a resource is best identified by a compound key consisting of multiple other resource identifiers, it is allowed to reuse the compound key in its natural form containing slashes instead of technical resource identifier in the resource path without violating the above rule MUST identify resources and sub-resources via path segments as follows:
/resources/{compound-key-1}[delim-1]...[delim-n-1]{compound-key-n}
Example paths:
/shopping-carts/{country}/{session-id}
/shopping-carts/{country}/{session-id}/items/{item-id}
/api-specifications/{docker-image-id}/apis/{path}/{file-name}
/api-specifications/{repository-name}/{artifact-name}:{tag}
/article-size-advices/{sku}/{sales-channel}
Warning: Exposing a compound key as described above limits ability to evolve the structure of the resource identifier as it is no longer opaque.
To compensate for this drawback, APIs must apply a compound key abstraction consistently in all requests and responses parameters and attributes allowing consumers to treat these as technical resource identifier replacement. The use of independent compound key components must be limited to search and creation requests, as follows:
# compound key components passed as independent search query parameters
GET /article-size-advices?skus=sku-1,sku-2&sales_channel_id=sid-1
=> { "items": [{ "id": "id-1", ... },{ "id": "id-2", ... }] }
# opaque technical resource identifier passed as path parameter
GET /article-size-advices/id-1
=> { "id": "id-1", "sku": "sku-1", "sales_channel_id": "sid-1", "size": ... }
# compound key components passed as mandatory request fields
POST /article-size-advices { "sku": "sku-1", "sales_channel_id": "sid-1", "size": ... }
=> { "id": "id-1", "sku": "sku-1", "sales_channel_id": "sid-1", "size": ... }
Where id-1 is representing the opaque provision of the compound key
sku-1/sid-1 as technical resource identifier.
Remark: A compound key component may itself be used as another resource
identifier providing another resource endpoint, e.g /article-size-advices/{sku}.
MAY consider using (non-)nested URLs
If a sub-resource is only accessible via its parent resource and may not exist without parent resource, consider using a nested URL structure, for instance:
/shoping-carts/de/1681e6b88ec1/cart-items/1
However, if the resource can be accessed directly via its unique ID, then the API should expose it as a top level resource. For example, customer has a collection for sales orders; however, sales orders have globally unique id and some services may choose to access the orders directly, for instance:
/customers/1637asikzec1
/sales-orders/5273gh3k525a
SHOULD only use UUIDs if necessary
Generating IDs can be a scaling problem in high frequency and near real time use cases. UUIDs solve this problem, as they can be generated without collisions in a distributed, non-coordinated way and without additional server round trips.
However, they also come with some disadvantages:
-
pure technical key without meaning; not ready for naming or name scope conventions that might be helpful for pragmatic reasons, e.g. we learned to use names for product attributes, instead of UUIDs
-
less usable, because…
-
cannot be memorized and easily communicated by humans
-
harder to use in debugging and logging analysis
-
less convenient for consumer facing usage
-
quite long: readable representation requires 36 characters and comes with higher memory and bandwidth consumption
-
not ordered along their creation history and no indication of used ID volume
-
may be in conflict with additional backward compatibility support of legacy IDs
UUIDs should be avoided when not needed for large scale ID generation. Instead,
for instance, server side support with ID generation can be preferred (POST
on ID resource, followed by idempotent PUT on entity resource). Usage of
UUIDs is especially discouraged as primary keys of master and configuration
data, like brand-ids or attribute-IDs which have low ID volume but widespread
steering functionality.
Please be aware that sequential, strictly monotonically increasing numeric identifiers may reveal critical, confidential business information, like order volume, to non-privileged clients.
In any case, we should always use string rather than number type for identifiers. This gives us more flexibility to evolve the identifier naming scheme. Accordingly, if used as identifiers, UUIDs should not be qualified using a format property.
Hint: Usually, random UUID is used - see UUID version 4 in RFC 4122. Though UUID version 1 also contains leading timestamps it is not reflected by its lexicographic sorting. This deficit is addressed by ULID (Universally Unique Lexicographically Sortable Identifier). You may favour ULID instead of UUID, for instance, for pagination use cases ordered along creation time.
SHOULD limit number of resource types
To keep maintenance and service evolution manageable, we should follow "functional segmentation" and "separation of concern" design principles and do not mix different business functionalities in the same API definition. In practice this means that the number of resource types exposed via an API should be limited. In this context a resource type is defined as a set of highly related resources such as a collection, its members and any direct sub-resources.
For example, the resources below would be counted as three resource types, one for customers, one for the addresses, and one for the customers' related addresses:
/customers
/customers/{id}
/customers/{id}/preferences
/customers/{id}/addresses
/customers/{id}/addresses/{addr}
/addresses
/addresses/{addr}
Note that:
-
We consider
/customers/part of theid/preferences/customersresource type because it has a one-to-one relation to the customer without an additional identifier. -
We consider
/customersand/customers/as separate resource types becauseid/addresses/customers/also exists with an additional identifier for the address.id/addresses/{addr} -
We consider
/addressesand/customers/as separate resource types because there’s no reliable way to be sure they are the same.id/addresses
Given this definition, our experience is that well defined APIs involve no more than 4 to 8 resource types. There may be exceptions with more complex business domains that require more resources, but you should first check if you can split them into separate sub-domains with distinct APIs.
Nevertheless one API should hold all necessary resources to model complete business processes helping clients to understand these flows.
SHOULD limit number of sub-resource levels
There are main resources (with root url paths) and sub-resources (or nested resources with non-root urls paths). Use sub-resources if their life cycle is (loosely) coupled to the main resource, i.e. the main resource works as collection resource of the subresource entities. You should use ⇐ 3 sub-resource (nesting) levels — more levels increase API complexity and url path length. (Remember, some popular web browsers do not support URLs of more than 2000 characters.)
13. HTTP requests
MUST use HTTP methods correctly
Be compliant with the standardized HTTP method semantics summarized as follows:
GET
GET requests are used to read either a single or a collection resource.
Note: GET requests on collection resources should provide sufficient
filter and Pagination mechanisms.
GET with body
APIs sometimes face the problem, that they have to provide extensive structured
request information with GET, that may conflict with the size limits of
clients, load balancers, and servers. As we require APIs to be standard conform
(body in GET must be ignored on server side), API designers have to check the
following two options:
-
GETwith URL encoded query parameters: when it is possible to encode the request information in query parameters, respecting the usual size limits of clients, gateways, and servers, this should be the first choice. The request information can either be provided via multiple query parameters or by a single structured URL encoded string. -
POSTwith body content: when aGETwith URL encoded query parameters is not possible, aPOSTwith body content must be used. In this case the endpoint must be documented with the hintGET With Bodyto transport theGETsemantic of this call.
Note: It is no option to encode the lengthy structured request information using header parameters. From a conceptual point of view, the semantic of an operation should always be expressed by the resource names, as well as the involved path and query parameters. In other words by everything that goes into the URL. Request headers are reserved for general context information (see MUST use only the specified proprietary VIG headers). In addition, size limits on query parameters and headers are not reliable and depend on clients, gateways, server, and actual settings. Thus, switching to headers does not solve the original problem.
Hint: As GET With Body is used to transport extensive query parameters,
the cursor cannot any longer be used to encode the query filters in case of
cursor-based pagination. As a consequence, it is best practice to
transport the query filters in the body, while using pagination links
containing the cursor that is only encoding the page position and direction.
To protect the pagination sequence the cursor may contain a hash over all
applied query filters (See also SHOULD use pagination links where applicable).
PUT
PUT requests are used to update (in rare cases to create) entire
resources – single or collection resources. The semantic is best described
as "please put the enclosed representation at the resource mentioned by
the URL, replacing any existing resource.".
-
PUTrequests are usually applied to single resources, and not to collection resources, as this would imply replacing the entire collection -
PUTrequests are usually robust against non-existence of resources by implicitly creating before updating -
on successful
PUTrequests, the server will replace the entire resource addressed by the URL with the representation passed in the payload (subsequent reads will deliver the same payload) -
successful
PUTrequests will usually generate 200 or 204 (if the resource was updated – with or without actual content returned), and 201 (if the resource was created)
Important: It is best practice to prefer POST over PUT for creation of
(at least top-level) resources. This leaves the resource ID under control of
the service and allows to concentrate on the update semantic using PUT as
follows.
Note: In the rare cases where PUT is although used for resource creation,
the resource IDs are maintained by the client and passed as a URL path segment.
Putting the same resource twice is required to be idempotent and to result
in the same single resource instance (see MUST fulfill common method properties).
Hint: To prevent unnoticed concurrent updates and duplicate creations when
using PUT, you MAY consider to support ETag together with If-Match/If-None-Match header to allow the server to react on stricter demands that
expose conflicts and prevent lost updates. See also Optimistic locking in RESTful APIs for
details and options.
POST
POST requests are idiomatically used to create single resources on a
collection resource endpoint, but other semantics on single resources endpoint
are equally possible. The semantic for collection endpoints is best described
as "please add the enclosed representation to the collection resource
identified by the URL".
-
on a successful
POSTrequest, the server will create one or multiple new resources and provide their URI/URLs in the response -
successful
POSTrequests will usually generate 200 (if resources have been updated), 201 (if resources have been created), 202 (if the request was accepted but has not been finished yet), and exceptionally 204 withLocationheader (if the actual resource is not returned).
The semantic for single resource endpoints is best described as "please execute the given well specified request on the resource identified by the URL".
Generally: POST should be used for scenarios that cannot be covered by the
other methods sufficiently. In such cases, make sure to document the fact that
POST is used as a workaround (see GET With Body).
Note: Resource IDs with respect to POST requests are created and maintained
by server and returned with response payload.
Hint: Posting the same resource twice is not required to be idempotent
(check MUST fulfill common method properties) and may result in multiple resources. However, you SHOULD consider to design POST and PATCH idempotent to
prevent this.
PATCH
PATCH requests are used to update parts of single resources, i.e. where only
a specific subset of resource fields should be replaced. The semantic is best
described as "please change the resource identified by the URL according to my
change request". The semantic of the change request is not defined in the HTTP
standard and must be described in the API specification by using suitable media
types.
-
PATCHrequests are usually applied to single resources as patching entire collection is challenging -
PATCHrequests are usually not robust against non-existence of resource instances -
on successful
PATCHrequests, the server will update parts of the resource addressed by the URL as defined by the change request in the payload -
successful
PATCHrequests will usually generate 200 or 204 (if resources have been updated with or without updated content returned)
Note: since implementing PATCH correctly is a bit tricky, we strongly suggest
to choose one and only one of the following patterns per endpoint, unless
forced by a backwards compatible change. In preference order:
-
use
PUTwith complete objects to update a resource as long as feasible (i.e. do not usePATCHat all). -
use
PATCHwith partial objects to only update parts of a resource, whenever possible. (This is basically JSON Merge Patch, a specialized media typeapplication/merge-patch+jsonthat is a partial resource representation.) -
use
PATCHwith JSON Patch, a specialized media typeapplication/json-patch+jsonthat includes instructions on how to change the resource. -
use
POST(with a proper description of what is happening) instead ofPATCH, if the request does not modify the resource in a way defined by the semantics of the media type.
In practice, JSON Merge Patch quickly turns out to be too limited, especially when trying to update single objects in large collections (as part of the resource). In these cases JSON Patch can shown its full power while still showing readable patch requests (see also JSON patch vs. merge).
Note: Patching the same resource twice is not required to be idempotent
(check MUST fulfill common method properties) and may result in a changing result. However, you SHOULD consider to design POST and PATCH idempotent to
prevent this.
Hint: To prevent unnoticed concurrent updates when using PATCH you MAY consider to support ETag together with If-Match/If-None-Match header
to allow the server to react on stricter demands that expose conflicts and
prevent lost updates. See Optimistic locking in RESTful APIs and SHOULD consider to design POST and PATCH idempotent for details and
options.
DELETE
DELETE requests are used to delete resources. The semantic is best
described as "please delete the resource identified by the URL".
-
DELETErequests are usually applied to single resources, not on collection resources, as this would imply deleting the entire collection -
successful
DELETErequests will usually generate 200 (if the deleted resource is returned) or 204 (if no content is returned) -
failed
DELETErequests will usually generate 404 (if the resource cannot be found) or 410 (if the resource was already deleted before)
HEAD
HEAD requests are used to retrieve the header information of single
resources and resource collections.
OPTIONS
OPTIONS requests are used to inspect the available operations (HTTP
methods) of a given endpoint.
-
OPTIONSresponses usually either return a comma-separated list of methods in theAllowheader or as a structured list of link templates
Note: OPTIONS is rarely implemented, though it could be used to
self-describe the full functionality of a resource.
MUST fulfill common method properties
Request methods in RESTful services can be…
-
safe - the operation semantic is defined to be read-only, meaning it must not have intended side effects, i.e. changes, to the server state.
-
idempotent - the operation has the same intended effect on the server state, independently whether it is executed once or multiple times. Note: this does not require that the operation is returning the same response or status code.
-
cacheable - to indicate that responses are allowed to be stored for future reuse. In general, requests to safe methods are cachable, if it does not require a current or authoritative response from the server.
Note: The above definitions, of intended (side) effect allows the server to provide additional state changing behavior as logging, accounting, pre-fetching, etc. However, these actual effects and state changes, must not be intended by the operation so that it can be held accountable.
Method implementations must fulfill the following basic properties according to RFC 7231:
| Method | Safe | Idempotent | Cacheable |
|---|---|---|---|
✔ Yes |
✔ Yes |
✔ Yes |
|
✔ Yes |
✔ Yes |
✔ Yes |
|
✗ No |
⚠️ No, but SHOULD consider to design |
⚠️ May, but only if specific
|
|
✗ No |
✔ Yes |
✗ No |
|
✗ No |
⚠️ No, but SHOULD consider to design |
✗ No |
|
✗ No |
✔ Yes |
✗ No |
|
✔ Yes |
✔ Yes |
✗ No |
|
✔ Yes |
✔ Yes |
✗ No |
SHOULD consider to design POST and PATCH idempotent
In many cases it is helpful or even necessary to design POST and PATCH
idempotent for clients to expose conflicts and prevent resource duplicate
(a.k.a. zombie resources) or lost updates, e.g. if same resources may be
created or changed in parallel or multiple times. To design an idempotent
API endpoint, owners should consider to apply one of the following three
patterns.
-
A resource-specific conditional key provided via
If-Matchheader in the request. The key is in general a meta information of the resource, e.g. a hash or version number, often stored with it. It allows to detect concurrent creations and updates to ensure idempotent behavior (see MAY consider to supportETagtogether withIf-Match/If-None-Matchheader). -
A resource-specific secondary key provided as resource property in the request body. The secondary key is stored permanently in the resource. It allows to ensure idempotent behavior by looking up the unique secondary key in case of multiple independent resource creations from different clients (see SHOULD use secondary key for idempotent
POSTdesign). -
A client-specific idempotency key provided via
Idempotency-Keyheader in the request. The key is not part of the resource but stored temporarily pointing to the original response to ensure idempotent behavior when retrying a request (see MAY consider to supportIdempotency-Keyheader).
Note: While conditional key and secondary key are focused on handling concurrent requests, the idempotency key is focused on providing the exact same responses, which is even a stronger requirement than the idempotency defined above. It can be combined with the two other patterns.
To decide, which pattern is suitable for your use case, please consult the following table showing the major properties of each pattern:
| Conditional Key | Secondary Key | Idempotency Key | |
|---|---|---|---|
Applicable with |
|||
HTTP Standard |
✔ Yes |
✗ No |
✗ No |
Prevents duplicate (zombie) resources |
✔ Yes |
✔ Yes |
✗ No |
Prevents concurrent lost updates |
✔ Yes |
✗ No |
✗ No |
Supports safe retries |
✔ Yes |
✔ Yes |
✔ Yes |
Supports exact same response |
✗ No |
✗ No |
✔ Yes |
Can be inspected (by intermediaries) |
✔ Yes |
✗ No |
✔ Yes |
Usable without previous |
✗ No |
✔ Yes |
✔ Yes |
Note: The patterns applicable to PATCH can be applied in the same way to
PUT and DELETE providing the same properties.
If you mainly aim to support safe retries, we suggest to apply conditional key and secondary key patterns before the Idempotency Key pattern.
SHOULD use secondary key for idempotent POST design
The most important pattern to design POST idempotent for creation is to
introduce a resource-specific secondary key provided in the request body, to
eliminate the problem of duplicate (a.k.a zombie) resources.
The secondary key is stored permanently in the resource as alternate key or combined key (if consisting of multiple properties) guarded by a uniqueness constraint enforced server side, that is visible when reading the resource. The best and often naturally existing candidate is a unique foreign key, that points to another resource having a one-on-one relationship with the newly created resource, e.g. a parent process identifier.
A good example here for a secondary key is the shopping cart ID in an order resource.
Note: When using the secondary key pattern without Idempotency-Key, all
subsequent retries should fail with status code 409 (conflict). We suggest
to avoid 200 here unless you make sure that the delivered resource is the
original one implementing a well defined behavior. Using 204 without content
would be a similar well defined option.
MUST define collection format of header and query parameters
Header and query parameters allow to provide a collection of values, either by providing a comma-separated list of values or by repeating the parameter multiple times with different values as follows:
| Parameter Type | Comma-separated Values | Multiple Parameters | Standard |
|---|---|---|---|
Header |
|
|
|
Query |
|
|
As Open API does not support both schemas at once, an API specification must explicitly define the collection format to guide consumers as follows:
| Parameter Type | Comma-separated Values | Multiple Parameters |
|---|---|---|
Header |
|
not allowed (see RFC 7230 Section 3.2.2) |
Query |
|
|
When choosing the collection format, take into account the tool support, the escaping of special characters and the maximum URL length.
SHOULD design simple query languages using query parameters
We prefer using query parameters to describe resource-specific query languages for the majority of APIs because it’s native to HTTP, easy to extend and has excellent implementation support in HTTP clients and web frameworks.
Query parameters should have the following aspects specified:
-
Reference to corresponding property, if any
-
Value range, e.g. inclusive vs. exclusive
-
Comparison semantics (equals, less than, greater than, etc.)
-
Implications when combined with other queries, e.g. and vs. or
How query parameters are named and used is up to individual API designers. The following examples should serve as ideas:
-
name=VIG, to query for elements based on property equality -
age=5, to query for elements based on logical properties-
Assuming that elements don’t actually have an
agebut rather abirthday
-
-
max_length=5, based on upper and lower bounds (minandmax) -
shorter_than=5, using terminology-specific e.g. to length -
created_before=2019-07-17ornot_modified_since=2019-07-17-
Using terminology-specific e.g. to time: before, after, since and until
-
We don’t advocate for or against certain names because in the end APIs should be free to choose the terminology that fits their domain the best.
SHOULD design complex query languages using JSON
Minimalistic query languages based on query parameters are suitable for simple use cases with a small set of available filters that are combined in one way and one way only (e.g. and semantics). Simple query languages are generally preferred over complex ones.
Some APIs will have a need for sophisticated and more complex query languages. Dominant examples are APIs around search (incl. faceting) and product catalogs.
Aspects that set those APIs apart from the rest include but are not limited to:
-
Unusual high number of available filters
-
Dynamic filters, due to a dynamic and extensible resource model
-
Free choice of operators, e.g.
and,orandnot
APIs that qualify for a specific, complex query language are encouraged to use nested JSON data structures and define them using Open API directly. This provides the following benefits:
-
Data structures are easy to use for clients
-
No special library support necessary
-
No need for string concatenation or manual escaping
-
-
Data structures are easy to use for servers
-
No special tokenizers needed
-
Semantics are attached to data structures rather than text tokens
-
-
Consistent with other HTTP methods
-
API is defined in Open API completely
-
No external documents or grammars needed
-
Existing means are familiar to everyone
-
JSON-specific rules and most certainly needs to make use
of the GET-with-body pattern.
Example
The following JSON document should serve as an idea how a structured query might look like.
{
"and": {
"name": {
"match": "Alice"
},
"age": {
"or": {
"range": {
">": 25,
"<=": 50
},
"=": 65
}
}
}
}
Feel free to also get some inspiration from:
MUST document implicit filtering
Sometimes certain collection resources or queries will not list all the possible elements they have, but only those for which the current client is authorized to access.
Implicit filtering could be done on:
-
the collection of resources being returned on a parent
GETrequest -
the fields returned for the resource’s detail
In such cases, the implicit filtering must be in the API specification (in its description).
Consider caching considerations when implicitly filtering.
Example:
If an employee of the company Foo accesses one of our business-to-business
services and performs a , it must, for legal reasons,
not display any other business partner that is not owned or contractually
managed by her/his company. It should never see that we are doing business
also with company Bar.GET /business-partners
Response as seen from a consumer working at FOO:
{
"items": [
{ "name": "Foo Performance" },
{ "name": "Foo Sport" },
{ "name": "Foo Signature" }
]
}
Response as seen from a consumer working at BAR:
{
"items": [
{ "name": "Bar Classics" },
{ "name": "Bar pour Elle" }
]
}
The API Specification should then specify something like this:
paths:
/business-partner:
get:
description: >-
Get the list of registered business partner.
Only the business partners to which you have access to are returned.
14. HTTP status codes and errors
MUST specify success and error responses
APIs should define the functional, business view and abstract from implementation aspects. Success and error responses are a vital part to define how an API is used correctly.
Therefore, you must define all success- and service-specific error responses in your API specification. Both are part of the interface definition and provide important information for service clients to handle standard as well as exceptional situations.
Hint: In most cases it is not useful to document all technical errors, especially if they are not under control of the service provider. Thus, unless a response code conveys application-specific functional semantics or is used in a non-standard way that requires additional explanation, multiple error response specifications can be combined using the following pattern (see also MUST only use durable and immutable remote references):
responses:
...
default:
description: error occurred - see status code and problem object for more information.
content:
"application/problem+json":
schema:
$ref: 'https://gitlab.viesure.io/open-api/api-guidelines/raw/master/models/problem/schema.yaml#/Problem'
API designers should also think about a troubleshooting board as part of the associated online API documentation. It provides information and handling guidance on application-specific errors and is referenced via links from the API specification. This can reduce service support tasks and contribute to service client and provider performance.
MUST use standard HTTP status codes
You must only use standardized HTTP status codes consistently with their intended semantics. You must not invent new HTTP status codes.
RFC standards define ~60 different HTTP status codes with specific semantics (mainly RFC7231 and RFC 6585) — and there are upcoming new ones, e.g. draft legally-restricted-status. See overview of all error codes on Wikipedia or via https://httpstatuses.com/) also inculding 'unofficial codes', e.g. used by popular web servers like Nginx.
Below we list the most commonly used and best understood HTTP status codes, consistent with their semantic in the RFCs. APIs should only use these to prevent misconceptions that arise from less commonly used HTTP status codes.
Important: As long as your HTTP status code usage is well covered by the semantic defined here, you should not describe it to avoid an overload with common sense information and the risk of inconsistent definitions. Only if the HTTP status code is not in the list below or its usage requires additional information aside the well defined semantic, the API specification must provide a clear description of the HTTP status code in the response.
Success codes
| Code | Meaning | Methods |
|---|---|---|
OK - this is the standard success response |
|
|
Created - Returned on successful entity creation. You are free to return either an empty response or the created resource in conjunction with the Location header. (For more details see Common headers.) Always set the Location header. |
||
Accepted - The request was successful and will be processed asynchronously. |
||
No Content - There is no response body. |
||
Multi-Status - The response body contains status information for different parts of a batch/bulk request (see MUST use code 207 for batch or bulk requests). |
Redirection codes
| Code | Meaning | Methods |
|---|---|---|
Moved Permanently - This and all future requests should be directed to the given URI. |
|
|
See Other - The response to the request can be found under another URI using a
|
||
Not Modified - indicates that a conditional GET or HEAD request would have resulted in 200 response if it were not for the fact that the condition evaluated to false, i.e. resource has not been modified since the date or version passed via request headers If-Modified-Since or If-None-Match. |
Client side error codes
| Code | Meaning | Methods |
|---|---|---|
Bad Request - Generic / unknown error. Should also be delivered in case of input payload fails business logic validation. |
|
|
Unauthorized - the users must log in (this often means "Unauthenticated"). |
|
|
Forbidden - the user is not authorized to use this resource. |
|
|
Not Found - the resource is not found. |
|
|
Method Not Allowed - the method is not supported, see |
|
|
Not Acceptable - resource can only generate content not acceptable according to the Accept headers sent in the request. |
|
|
Request timeout - the server times out waiting for the resource. |
|
|
Conflict - request cannot be completed due to conflict, e.g. when two clients try to create the same resource or if there are concurrent, conflicting updates. |
||
Gone - resource does no longer exist, e.g. when accessing a resource that has intentionally been deleted. |
|
|
Precondition Failed - returned for conditional requests, e.g. |
||
Unsupported Media Type - e.g. clients sends request body without content type. |
||
Unprocessable Entity, e.g. syntax is correct, but unable to process the contained instructions. |
||
Locked - Pessimistic locking, e.g. processing states. |
||
Precondition Required - server requires the request to be conditional, e.g. to make sure that the "lost update problem" is avoided (see [181]). |
|
|
Too Many Requests - the client does not consider rate limiting and sent too many requests (see MAY use code 429 with headers for rate limits). |
|
Server side error codes:
| Code | Meaning | Methods |
|---|---|---|
Internal Server Error - a generic error indication for an unexpected server execution problem (here, client retry may be sensible) |
|
|
Not Implemented - server cannot fulfill the request (usually implies future availability, e.g. new feature). |
|
|
Bad Gateway - the server, while acting as a gateway or proxy, received an invalid response from the upstream server. |
|
|
Service Unavailable - service is (temporarily) not available (e.g. if a
required component or downstream service is not available) — client retry may
be sensible. If possible, the service should indicate how long the client
should wait by setting the |
|
|
Gateway Timeout - the server, while acting as a gateway or proxy, did not get a response in time from the upstream server that it needed in order to complete the request. |
|
MUST use most specific HTTP status codes
You must use the most specific HTTP status code when returning information about your request processing status or error situations.
MUST use code 207 for batch or bulk requests
Some APIs are required to provide either batch or bulk requests using
POST for performance reasons, i.e. for communication and processing
efficiency. In this case services may be in need to signal multiple response
codes for each part of an batch or bulk request. As HTTP does not provide
proper guidance for handling batch/bulk requests and responses, we herewith
define the following approach:
-
A batch or bulk request always responds with HTTP status code 207 unless a non-item-specific failure occurs.
-
A batch or bulk request may return 4xx/5xx status codes, if the failure is non-item-specific and cannot be restricted to individual items of the batch or bulk request, e.g. in case of overload situations or general service failures.
-
A batch or bulk response with status code 207 always returns as payload a multi-status response containing item-specific status and/or monitoring information for each part of the batch or bulk request.
Note: These rules apply even in the case that processing of all individual parts fail or each part is executed asynchronously!
The rules are intended to allow clients to act on batch and bulk responses in
a consistent way by inspecting the individual results. We explicitly reject
the option to apply 200 for a completely successful batch as proposed in
Nakadi’s POST
/event-types/{name}/events as shortcut without inspecting the result, as we
want to avoid risks and expect clients to handle partial
batch failures anyway.
The bulk or batch response may look as follows:
BatchOrBulkResponse:
description: batch response object.
type: object
properties:
items:
type: array
items:
type: object
properties:
id:
description: Identifier of batch or bulk request item.
type: string
status:
description: >
Response status value. A number or extensible enum describing
the execution status of the batch or bulk request items.
type: string
x-extensible-enum: [...]
description:
description: >
Human readable status description and containing additional
context information about failures etc.
type: string
required: [id, status]
Note: while a batch defines a collection of requests triggering independent processes, a bulk defines a collection of independent resources created or updated together in one request. With respect to response processing this distinction normally does not matter.
MAY use code 429 with headers for rate limits
APIs that wish to manage the request rate of clients must use the 429 (Too Many Requests) response code, if the client exceeded the request rate (see RFC 6585). Such responses may also contain header information providing further details to the client. There are two approaches a service can take for header information:
-
Return a
Retry-Afterheader indicating how long the client ought to wait before making a follow-up request. The Retry-After header can contain a HTTP date value to retry after or the number of seconds to delay. Either is acceptable but APIs should prefer to use a delay in seconds. -
Return a trio of
X-RateLimitheaders. These headers (described below) allow a server to express a service level in the form of a number of allowing requests within a given window of time and when the window is reset.
The X-RateLimit headers are:
-
X-RateLimit-Limit: The maximum number of requests that the client is allowed to make in this window. -
X-RateLimit-Remaining: The number of requests allowed in the current window. -
X-RateLimit-Reset: The relative time in seconds when the rate limit window will be reset. Beware that this is different to Github and Twitter’s usage of a header with the same name which is using UTC epoch seconds instead.
The reason to allow both approaches is that APIs can have different needs. Retry-After is often sufficient for general load handling and request throttling scenarios and notably does not strictly require the concept of a calling entity such as a tenant or named account. In turn, this allows resource owners to minimise the amount of state they have to carry with respect to client requests. The 'X-RateLimit' headers are suitable for scenarios where clients are associated with pre-existing account or tenancy structures. 'X-RateLimit' headers are generally returned on every request and not just on a 429, which implies the service implementing the API is carrying sufficient state to track the number of requests made within a given window for each named entity.
MUST use problem JSON
RFC 7807 defines a Problem JSON object and the media type
application/problem+json. Operations should return it (together with a
suitable status code) when any problem occurred during processing and you can
give more details than the status code itself can supply, whether it be caused
by the client or the server (i.e. both for 4xx or 5xx error codes).
For the Open API schema definition of the Problem JSON object see on gitlab. You can reference it by using:
responses:
503:
description: Service Unavailable
content:
"application/problem+json":
schema:
$ref: 'https://gitlab.viesure.io/open-api/api-guidelines/raw/master/models/problem/schema.yaml#/Problem'
To communicate to the API consumer which parameters cased the problem, you can extend the Problem type by an invalid_params field.
The sub-fields "type", "field", and "message" are mandatory for the schema and additional fields may be added. The possible enum values of type can be modified.
You may optionally extend the Problem JSON by the field error_code. This field a system wide unique code for machines that identifies this problem type.
The sub-fields "type", "description", and "x-extensible-enum" are mandatory for the schema and additional fields may be added. The possible enum values of type can be modified.
You may define custom problem types as extensions of the Problem JSON object, if your API needs to return specific, additional and detailed error information.
Problem type identifiers in our APIs are not meant to be resolved.
The RFC encourages that custom problem types are URI references that point to human readable documentation, but we deliberately decided against that. URLs tend to be fragile and not very stable over a longer period. Hosting documentation often requires to bind to a specific tool or have DNS records that contain volatile organization structures, e.g. team names. Another reason is that all the important parts of an API must be documented using OpenAPI anyway.
In order to stay compatible the proposed pattern for custom problem types is to use relative URI references:
-
/problems/out-of-stock -
/problems/insufficient-funds -
/problems/user-deactivated
Examples of problem types that do not satisfy our criteria:
MUST not expose stack traces
Stack traces contain implementation details that are not part of an API and on which clients should never rely. Moreover, stack traces can leak sensitive information that partners and third parties are not allowed to receive and may disclose insights about vulnerabilities to attackers.
15. Performance
SHOULD reduce bandwidth needs and improve responsiveness
APIs should support techniques for reducing bandwidth based on client needs. This holds for APIs that (might) have high payloads and/or are used in high-traffic scenarios like the public Internet and telecommunication networks. Typical examples are APIs used by mobile web app clients with (often) less bandwidth connectivity.
Common techniques include:
-
compression of request and response bodies (see SHOULD use
gzipcompression) -
querying field filters to retrieve a subset of resource attributes (see SHOULD support partial responses via filtering below)
-
ETagandIf-Match/If-None-Matchheaders to avoid re-fetching of unchanged resources (see MAY consider to supportETagtogether withIf-Match/If-None-Matchheader) -
Preferheader withreturn=minimalorrespond-asyncto anticipate reduced processing requirements of clients (see [181]) -
Pagination for incremental access of larger collections of data items
-
caching of master data items, i.e. resources that change rarely or not at all after creation (see MUST document cachable
GET,HEAD, andPOSTendpoints).
Each of these items is described in greater detail below.
SHOULD use gzip compression
Compress the payload of your API’s responses with gzip, unless there’s a good reason not to — for example, you are serving so many requests that the time to compress becomes a bottleneck. This helps to transport data faster over the network (fewer bytes) and makes frontends respond faster.
Though gzip compression might be the default choice for server payload, the
server should also support payload without compression and its client control
via Accept-Encoding request header — see also RFC
7231 Section 5.3.4. The server should indicate used gzip compression via the
Content-Encoding header.
SHOULD support partial responses via filtering
Depending on your use case and payload size, you can significantly reduce
the network bandwidth needs by supporting filtering of returned entity fields.
Here, the client can explicitly determine the subset of fields it wants to
receive via the fields query parameter. (It is analog to
GraphQL fields and simple
queries, and also applied, for instance, for
Google
Cloud API’s partial responses.)
Unfiltered
GET http://api.example.org/users/123 HTTP/1.1
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": "cddd5e44-dae0-11e5-8c01-63ed66ab2da5",
"name": "John Doe",
"address": "1600 Pennsylvania Avenue Northwest, Washington, DC, United States",
"birthday": "1984-09-13",
"friends": [ {
"id": "1fb43648-dae1-11e5-aa01-1fbc3abb1cd0",
"name": "Jane Doe",
"address": "1600 Pennsylvania Avenue Northwest, Washington, DC, United States",
"birthday": "1988-04-07"
} ]
}
Filtered
GET http://api.example.org/users/123?fields=(name,friends(name)) HTTP/1.1
HTTP/1.1 200 OK
Content-Type: application/json
{
"name": "John Doe",
"friends": [ {
"name": "Jane Doe"
} ]
}
The fields query parameter determines the fields returned with the response
payload object. For instance, (name) returns users root object with only
the name field, and (name,friends(name)) returns the name and the nested
friends object with only its name field.
Open API doesn’t support you in formally specifying different return object schemes depending on a parameter. When you define the field parameter, we recommend to provide the following description: `Endpoint supports filtering of return object fields as described in [Rule #157 SHOULD support partial responses via filtering]
<fields> ::= [ <negation> ] <fields_struct>
<fields_struct> ::= "(" <field_items> ")"
<field_items> ::= <field> [ "," <field_items> ]
<field> ::= <field_name> | <fields_substruct>
<fields_substruct> ::= <field_name> <fields_struct>
<field_name> ::= <dash_letter_digit> [ <field_name> ]
<dash_letter_digit> ::= <dash> | <letter> | <digit>
<dash> ::= "-" | "_"
<letter> ::= "A" | ... | "Z" | "a" | ... | "z"
<digit> ::= "0" | ... | "9"
<negation> ::= "!"
Note: Following the
principle of
least astonishment, you should not define the fields query parameter using
a default value, as the result is counter-intuitive and very likely not
anticipated by the consumer.
SHOULD allow optional embedding of sub-resources
Embedding related resources (also know as Resource expansion) is a great way to reduce the number of requests. In cases where clients know upfront that they need some related resources they can instruct the server to prefetch that data eagerly. Whether this is optimized on the server, e.g. a database join, or done in a generic way, e.g. an HTTP proxy that transparently embeds resources, is up to the implementation.
See MUST stick to conventional query parameters for naming, e.g. "embed" for steering of embedded resource expansion. Please use the BNF grammar, as already defined above for filtering, when it comes to an embedding query syntax.
Embedding a sub-resource can possibly look like this where an order resource has its order items as sub-resource (/order/{orderId}/items):
GET /order/123?embed=(items) HTTP/1.1
{
"id": "123",
"_embedded": {
"items": [
{
"position": 1,
"sku": "1234-ABCD-7890",
"price": {
"amount": 71.99,
"currency": "EUR"
}
}
]
}
}
MUST document cachable GET, HEAD, and POST endpoints
Caching has to take many aspects into account, e.g. general cacheability of response information, our guideline to protect endpoints using SSL and OAuth authorization, resource update and invalidation rules, existence of multiple consumer instances. As a consequence, caching is in the best case complex, e.g. with respect to consistency, in the worst case inefficient.
As a consequence, client side as well as transparent web caching should be avoided, unless the service supports and requires it to protect itself, e.g. in case of a heavily used and therefore rate-limited master data service, i.e. data items that change rarely or not at all after creation.
As default, API providers and consumers should always set the Cache-Control
header set to Cache-Control: no-store and assume the same setting, if no
Cache-Control header is provided.
Note: There is no need to document this default setting. However, please make sure that your framework attaches this header value by default, or ensure this manually, e.g. using the best practice of Spring Security as shown below. Any setup deviating from this default must be sufficiently documented.
Cache-Control: no-cache, no-store, must-revalidate, max-age=0
If your service requires caching support, please observe the following rules:
-
Document all cacheable
GET,HEAD, andPOSTendpoints by declaring the support ofCache-Control,Vary, andETagheaders in the response. Note: you must not define theExpiresheader to prevent redundant and ambiguous definition of cache lifetime. A sensible default documentation of these headers is given below. -
Take care to specify the ability to support caching by defining the right caching boundaries, i.e. time-to-live and cache constraints, by providing sensible values for
Cache-ControlandVaryin your service. We will explain best practices below. -
Provide efficient methods to warm up and update caches, e.g. as follows:
-
In general, you should support
ETagTogether WithIf-Match/If-None-MatchHeader on all cacheable endpoints. -
For larger data items support
HEADrequests or more efficientGETrequests withIf-None-Matchheader to check for updates. -
For small data sets provide full collection
GETrequests supportingETag, as well asHEADrequests orGETrequests withIf-None-Matchto check for updates. -
For medium sized data sets provide full collection
GETrequests supportingETagtogether with Pagination and<entity-tag>filteringGETrequests for limiting the response to changes since the provided<entity-tag>. Note: this is not supported by generic client and proxy caches on HTTP layer.
-
Hint: For proper cache support, you must return 304 without content on a
failed HEAD or GET request with If-None-Match: <entity-tag> instead
of 412.
components:
headers:
- Cache-Control:
description: |
The RFC 7234 Cache-Control header field is providing directives to
control how proxies and clients are allowed to cache responses results
for performance. Clients and proxies are free to not support caching of
results, however if they do, they must obey all directives mentioned in
[RFC-7234 Section 5.2.2](https://tools.ietf.org/html/rfc7234) to the
word.
In case of caching, the directive provides the scope of the cache
entry, i.e. only for the original user (private) or shared between all
users (public), the lifetime of the cache entry in seconds (max-age),
and the strategy how to handle a stale cache entry (must-revalidate).
Please note, that the lifetime and validation directives for shared
caches are different (s-maxage, proxy-revalidate).
type: string
required: false
example: "private, must-revalidate, max-age=300"
- Vary:
description: |
The RFC 7231 Vary header field in a response defines which parts of
a request message, aside the target URL and HTTP method, might have
influenced the response. A client or proxy cache must respect this
information, to ensure that it delivers the correct cache entry (see
[RFC-7231 Section
7.1.4](https://tools.ietf.org/html/rfc7231#section-7.1.4)).
type: string
required: false
example: "accept-encoding, accept-language"
Hint: For ETag source see MAY consider to support ETag together with If-Match/If-None-Match header.
The default setting for Cache-Control should contain the private directive
for endpoints with standard OAuth authorization, as well as the
must-revalidate directive to ensure, that the client does not use stale cache
entries. Last, the max-age directive should be set to a value between a few
seconds (max-age=60) and a few hours (max-age=86400) depending on the change
rate of your master data and your requirements to keep clients consistent.
Cache-Control: private, must-revalidate, max-age=300
The default setting for Vary is harder to determine correctly. It highly
depends on the API endpoint, e.g. whether it supports compression, accepts
different media types, or requires other request specific headers. To support
correct caching you have to carefully choose the value. However, a good first
default may be:
Vary: accept, accept-encoding
Anyhow, this is only relevant, if you encourage clients to install generic HTTP layer client and proxy caches.
Note: generic client and proxy caching on HTTP level is hard to configure.
Therefore, we strongly recommend to attach the (possibly distributed) cache
directly to the service (or gateway) layer of your application. This relieves
from interpreting the Vary header and greatly simplifies interpreting the
Cache-Control and ETag headers. Moreover, is highly efficient with respect
to caching performance and overhead, and allows to support more
advanced cache update and warm up patterns.
Anyhow, please carefully read RFC 7234 before adding any client or proxy cache.
16. Pagination
MUST support pagination
Access to lists of data items must support pagination to protect the service against overload as well as for best client side iteration and batch processing experience. This holds true for all lists that are (potentially) larger than just a few hundred entries.
There are two well known page iteration techniques:
-
Offset/Limit-based pagination: numeric offset identifies the first page entry
-
Cursor/Limit-based — aka key-based — pagination: a unique key element identifies the first page entry (see also Facebook’s guide)
The technical conception of pagination should also consider user experience-related issues.
As mentioned in this
article,
jumping to a specific page is far less used than navigating via next/prev
page links (See SHOULD use pagination links where applicable). This favours cursor-based over offset-based
pagination.
Note: To provide a consistent look and feel of pagination patterns, you must stick to the common query parameter names defined in MUST stick to conventional query parameters.
MUST adhere to standardized pagination object
When using pagination make sure to conform to the standardized pagination. A copy of the schema can be found on gitlab or below.
All fields except sort are mandatory, but you may extend the schema with custom fields.
|
Important
|
The endpoint that uses the pagination model must include the query parameters page and size.
This makes a pageable object identifiable for the linter.
|
This may look as follows:
PagedSomething:
title: PagedSomething
type: object
properties:
entries:
type: array
description: An array representing the current page.
items:
$ref: '#/components/schemas/Something'
empty:
type: boolean
description: Is `true` if the current page is empty.
first:
type: boolean
description: Is `true` if the current page is the first page (i.e. `page_number == 0`).
last:
type: boolean
description: Is `true` if the current page is the last page (i.e. `page_number == total_pages - 1`).
page_number:
type: integer
format: int32
description: The number of the current page. The first page number is 0.
page_size:
type: integer
format: int32
description: The number of entries that can potentially fit on a page.
number_of_elements:
type: integer
format: int32
description: The number of entries present on the current page. Can be `<= page_size`.
element_offset:
type: integer
format: int32
description: The offset of the first element of this page relative to the first page (`element_offset = page_number * page_size`).
total_pages:
type: integer
format: int32
description: The total number of pages available.
total_elements:
type: integer
format: int32
description: The total number of pages available.
sort_fields:
type: array
description: |
The query results are sorted according to the order in this array.
items:
$ref: '#/components/schemas/Sort'
Sort:
title: Sort
type: object
properties:
property:
type: string
description: The used sorting property.
order:
type: string
description: The used sorting order (ascending or descending).
x-extensible-enum:
- ASC
- DESC
Something:
type: object
title: Something
properties:
number:
type: integer
SHOULD use pagination links where applicable
To simplify client design, APIs should support simplified hypertext
controls for pagination over collections whenever applicable. Beside next
this may comprise the support for prev, first, last, and self as
link relations (see also Link relation fields for
details).
The page content is transported via items, while the query object may
contain the query filters applied to the collection resource as follows:
{
"self": "http://my-service.vig.com/resources?cursor=<self-position>",
"first": "http://my-service.vig.com/resources?cursor=<first-position>",
"prev": "http://my-service.vig.com/resources?cursor=<previous-position>",
"next": "http://my-service.vig.com/resources?cursor=<next-position>",
"last": "http://my-service.vig.com/resources?cursor=<last-position>",
"query": {
"query-param-<1>": ...,
"query-param-<n>": ...
},
"items": [...]
}
Note: In case of complex search requests, e.g. when GET With Body is
required, the cursor may not be able to encode all query filters. In this
case, it is best practice to encode only page position and direction in the
cursor and transport the query filter in the body - in the request as well
as in the response. To protect the pagination sequence, in this case it is
recommended, that the cursor contains a hash over all applied query
filters for pagination request validation.
Remark: You should avoid providing a total count unless there is a clear need to do so. Very often, there are significant system and performance implications when supporting full counts. Especially, if the data set grows and requests become complex queries and filters drive full scans. While this is an implementation detail relative to the API, it is important to consider the ability to support serving counts over the life of a service.
17. Hypermedia
MUST use REST maturity level 2
We strive for a good implementation of REST Maturity Level 2 as it enables us to build resource-oriented APIs that make full use of HTTP verbs and status codes. You can see this expressed by many rules throughout these guidelines, e.g.:
Although this is not HATEOAS, it should not prevent you from designing proper link relationships in your APIs as stated in the rules below.
MUST use full, absolute URI
Links to other resources must always use full, absolute URIs.
Motivation: Exposing any form of relative URI (no matter if the relative URI uses an absolute or relative path) introduces avoidable client side complexity. It also requires clarity on the base URI, which might not be given when using features like embedding sub-resources. The primary advantage of non-absolute URIs is reduction of the payload size, which is better achievable by following the recommendation to use gzip compression
18. Common headers
This section describes some headers which have raised the most questions in our daily usage or which might be useful in particular circumstances but not widely known.
MUST use Content-* headers correctly
Content or entity headers are headers with a Content- prefix. They describe
the content of the body of the message and they can be used in both, HTTP
requests and responses. Commonly used content headers include but are not
limited to:
-
Content-Dispositioncan indicate that the representation is supposed to be saved as a file, and the proposed file name. -
Content-Encodingindicates compression or encryption algorithms applied to the content. -
Content-Lengthindicates the length of the content (in bytes). -
Content-Languageindicates that the body is meant for people literate in some human language(s). -
Content-Locationindicates where the body can be found otherwise (MAY useContent-Locationheader for more details]). -
Content-Rangeis used in responses to range requests to indicate which part of the requested resource representation is delivered with the body. -
Content-Typeindicates the media type of the body content.
MAY use standardized headers
Use this list and mention its support in your Open API definition.
MAY use Content-Location header
The Content-Location header is optional and can be used in successful write
operations (PUT, POST, or PATCH) or read operations (GET, HEAD) to
guide caching and signal a receiver the actual location of the resource
transmitted in the response body. This allows clients to identify the resource
and to update their local copy when receiving a response with this header.
The Content-Location header can be used to support the following use cases:
-
For reading operations
GETandHEAD, a different location than the requested URI can be used to indicate that the returned resource is subject to content negotiations, and that the value provides a more specific identifier of the resource. -
For writing operations
PUTandPATCH, an identical location to the requested URI can be used to explicitly indicate that the returned resource is the current representation of the newly created or updated resource. -
For writing operations
POSTandDELETE, a content location can be used to indicate that the body contains a status report resource in response to the requested action, which is available at provided location.
Note: When using the Content-Location header, the Content-Type header
has to be set as well. For example:
GET /products/123/images HTTP/1.1
HTTP/1.1 200 OK
Content-Type: image/png
Content-Location: /products/123/images?format=raw
SHOULD use Location header instead of Content-Location header
As the correct usage of Content-Location with respect to semantics and
caching is difficult, we discourage the use of Content-Location. In most
cases it is sufficient to direct clients to the resource location by using
the Location header instead without hitting the Content-Location-specific
ambiguities and complexities.
More details in RFC 7231 7.1.2 Location, 3.1.4.2 Content-Location
MAY consider to support ETag together with If-Match/If-None-Match header
When creating or updating resources it may be necessary to expose conflicts
and to prevent the 'lost update' or 'initially created' problem. Following
RFC 7232 "HTTP: Conditional Requests" this can be accomplished best
by supporting the ETag header together with the If-Match or If-None-Match
conditional header. The content of an ETag: <entity-tag> header is either
(a) a hash of the response body, (b) a hash of the last modified field of the
entity, or (c) a version number or identifier of the entity version.
To expose conflicts between concurrent update operations via PUT, POST, or
PATCH, the If-Match: <entity-tag> header can be used to force the server to
check whether the version of the updated entity conforms to the requested
<entity-tag>. If no matching entity is found, the operation is supposed a to
respond with status code 412 - precondition failed.
Beside other use cases, If-None-Match: * can be used in a similar way to
expose conflicts in resource creation. If any matching entity is found, the
operation is supposed to respond with status code 412 - precondition
failed.
The ETag, If-Match, and If-None-Match headers can be defined as follows
in the API definition:
components:
headers:
- ETag:
description: |
The RFC 7232 ETag header field in a response provides the entity-tag of
a selected resource. The entity-tag is an opaque identifier for versions
and representations of the same resource over time, regardless whether
multiple versions are valid at the same time. An entity-tag consists of
an opaque quoted string, possibly prefixed by a weakness indicator (see
[RFC 7232 Section 2.3](https://tools.ietf.org/html/rfc7232#section-2.3).
type: string
required: false
example: W/"xy", "5", "5db68c06-1a68-11e9-8341-68f728c1ba70"
- If-Match:
description: |
The RFC7232 If-Match header field in a request requires the server to
only operate on the resource that matches at least one of the provided
entity-tags. This allows clients express a precondition that prevent
the method from being applied if there have been any changes to the
resource (see [RFC 7232 Section
3.1](https://tools.ietf.org/html/rfc7232#section-3.1).
type: string
required: false
example: "5", "7da7a728-f910-11e6-942a-68f728c1ba70"
- If-None-Match:
description: |
The RFC7232 If-None-Match header field in a request requires the server
to only operate on the resource if it does not match any of the provided
entity-tags. If the provided entity-tag is `*`, it is required that the
resource does not exist at all (see [RFC 7232 Section
3.2](https://tools.ietf.org/html/rfc7232#section-3.2).
type: string
required: false
example: "7da7a728-f910-11e6-942a-68f728c1ba70", *
Please see Optimistic locking in RESTful APIs for a detailed discussion and options.
MAY consider to support Idempotency-Key header
When creating or updating resources it can be helpful or necessary to ensure a
strong idempotent behavior comprising same responses, to prevent duplicate
execution in case of retries after timeout and network outages. Generally, this
can be achieved by sending a client-specific unique request key – that is not
part of the resource – via Idempotency-Key header.
The unique request key is stored temporarily, e.g. for 24 hours, together with the response and the request hash (optionally) of the first request in a key cache regardless of whether it succeeded or failed. The service can now look up the unique request key in the key cache and serve the response from the key cache instead of re-executing the request to ensure idempotent behavior. Optionally, it can check the request hash for consistency before serving the response. If the key is not in the key store, the request is executed as usual and the response is stored in the key cache.
This allows clients to safely retry requests after timeouts, network outages, etc. while receiving the same response multiple times. Note: The request retry in this context requires to send the exact same request, i.e. updates of the request that would change the result are off-limits. The request hash in the key cache can protect against this misbehavior. The service is recommended to reject such a request using status code 400.
Important: To grant a reliable idempotent execution semantic, the resource and the key cache have to be updated with hard transaction semantics – considering all potential pitfalls of failures, timeouts, and concurrent requests in a distributed systems. This makes a correct implementation exceeding the local context very hard.
The Idempotency-Key header must be defined as follows, but you are free to
choose your expiration time:
components:
headers:
- Idempotency-Key:
description: |
The idempotency key is a free identifier created by the client to
identify a request. It is used by the service to identify subsequent
retries of the same request and ensure idempotent behavior by sending
the same response without executing the request a second time.
Clients should be careful as any subsequent requests with the same key
may return the same response without further check. Therefore, it is
recommended to use an UUID version 4 (random) or any other random
string with enough entropy to avoid collisions.
Idempotency keys expire after 24 hours. Clients are responsible to stay
within this limits, if they require idempotent behavior.
type: string
format: uuid
required: false
example: "7da7a728-f910-11e6-942a-68f728c1ba70"
Hint: The key cache is not intended as request log, and therefore should have a limited lifetime, else it could easily exceed the data resource in size.
Note: The Idempotency-Key header unlike other headers in this section
is not standardized in an RFC. Our only reference are the usage in the
Stripe API. However, as it
fits not into our section about Proprietary headers and we did not want
to change the header name and semantic, we decided to treat it as any other
common header.
19. Proprietary headers
This section shares definitions of proprietary headers that must be named consistently because they address overarching service-related concerns. Whether services support these concerns or not is optional; therefore, the Open API specification is the right place to make this explicitly visible. Use the parameter definitions of the resource HTTP methods.
MUST use only the specified proprietary VIG headers
As a general rule, proprietary HTTP headers should be avoided. However, they can still be useful in cases where context needs to be passed through multiple services in an end-to-end fashion. As such, a valid use case for a proprietary header is providing context information, which is not a part of the actual API, but is needed by subsequent communication.
From a conceptual point of view, the semantics and intent of an operation should always be expressed by URLs path and query parameters, the method, and the content. Headers are more often used to implement functions close to the protocol considerations, such as flow control, content negotiation, and authentication. Thus, headers are reserved for general context information (RFC 7231).
X- headers were initially reserved for unstandardized parameters, but the
usage of X- headers is deprecated (RFC 6648). This complicates
the contract definition between consumer and producer of an API following
these guidelines, since there is no aligned way of using those headers.
Because of this, these guidelines restrict which X- headers can be used
and how they are used.
The Internet Engineering Task Force’s states in RFC 6648 that
company-specific header names should incorporate the organization’s name.
We aim for backward compatibility, and therefore keep the X- prefix.
The following proprietary headers have been specified by this guideline for usage so far. Remember that HTTP header field names are not case-sensitive.
| Header field name | Type | Description | Header field value example |
|---|---|---|---|
String |
For more information see X-Forwarded-Proto. |
https |
|
String |
For more information see X-Forwarded-Host. |
id42.example-cdn.com |
|
String |
API Gateways typically route requests by path prefixes.
For example, http://example.com/foo/* might redirect to service A.
But service A does not know about |
|
|
String |
The |
|
|
String |
The |
46ebbcdc-3cf9-4d09-9e11-f3b4575bc1cb |
|
String |
The |
abc123 |
Exception: The only exception to this guideline are the conventional
hop-by-hop X-RateLimit- headers which can be used as defined in MAY use code 429 with headers for rate limits.
20. API Operation
MUST publish Open API specification
All service applications must publish Open API specifications of their external APIs. While this is optional for internal APIs, i.e. APIs marked with the component-internal API audience group, we still recommend to do so to profit from the API management infrastructure.
MUST monitor API usage
Owners of APIs used in production must monitor API services to get information about the clients' usage. This information, for instance, is useful to identify potential review partners for API changes.
Hint: A preferred way of client detection implementation is by logging the client-id retrieved from the OAuth token.
21. SOAP Guidelines
The following sections contain deviations from the common guidelines and are specific to SOAP APIs.
A SOAP service contract can be comprised of several documents and definitions that are assembled together to build a complete technical interface.
For example, a given Web service contract can consist of:
-
one (sometimes more) WSDL definitions
-
one (usually more) XML Schema definitions
-
some (sometimes no) WS Policy definitions
Furthermore, every definition document can be shared by other Web service contracts, e.g.:
-
a centralized XML Schema definition will commonly be used by multiple WSDL definitions
-
a centralized WS Policy definition will commonly be applied to multiple WSDL definitions
-
an abstract WSDL description can be imported by multiple specific WSDL descriptions or vice versa
Of all the different parts of a Web service contract, the part that establishes the fundamental technical interface is the abstract description of the WSDL definition. This represents the core of a Web service contract and is then further extended and detailed through schema definitions, policy definitions, and one or more specific WSDL descriptions. When we need to create a new version of a Web service contract, we can therefore assume that there has been a change in the abstract WSDL description or one of the contract documents that relates to the abstract WSDL description.
Fine and Coarse-Grained Constraints
Versioning changes are generally related to the increase or reduction of the quantity or granularity of constraints.
1
2
3
4
5
6
7
8
9
<xsd:element name="LineItem" type="LineItemType"/>
<xsd:complexType name="LineItemType">
<xsd:sequence>
<xsd:element name="productID" type="xsd:string"/> <!--(1)-->
<xsd:element name="productName" type="xsd:string"/> <!--(2)-->
<xsd:any minOccurs="0" maxOccurs="unbounded" namespace="##any" processContents="lax"/> <!--(3)-->
</xsd:sequence>
<xsd:anyAttribute namespace="##any"/>
</xsd:complexType>
-
(1,2) represent parts of the message definition with a fine level of constraint granularity, e.g. elements having specific names and data types.
-
(3) on the other hand shows a very coarse-grained level of granularity, e.g. element and attribute wildcards.
The use of the terms “fine-grained” and “coarse-grained” is highly subjective. What may be a fine-grained constraint in one contract may not be in another. The point is to understand how these terms can be applied when comparing parts of a message definition or when comparing different message definitions with each other.
Versioning and Compatibility
The number one concern when developing and deploying a new version of a service contract is the impact it will have on other parts of the enterprise that have formed or will form dependencies on it. This measure of impact is directly related to how compatible the new contract version is with the old version and its surroundings in general. This section establishes the fundamental types of compatibility that relate to the content and design of new contract versions.
Backward Compatibility
A new version of a Web service contract that continues to support consumer programs designed to work with the old version, is considered backward compatible. From a design perspective, this means that the new contract has not changed in such a way that it can impact existing consumer programs that are already using the contract.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<definitions name=”Purchase Order” targetNamespace="https://vig.com/contract/po" xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="https://vig.com/contract/po"
xmlns:po="https://vig.com/schema/po">
<!-- ...-->
<portType name="ptPurchaseOrder">
<operation name="opSubmitOrder">
<input message="tns:msgSubmitOrderRequest"/>
<output message="tns:msgSubmitOrderResponse"/>
</operation>
<operation name="opCheckOrderStatus">
<input message="tns:msgCheckOrderRequest"/>
<output message="tns:msgCheckOrderResponse"/>
</operation>
<operation name="opChangeOrder">
<input message="tns:msgChangeOrderRequest"/>
<output message="tns:msgChangeOrderResponse"/>
</operation>
<operation name="opCancelOrder">
<input message="tns:msgCancelOrderRequest"/>
<output message="tns:msgCancelOrderResponse"/>
</operation>
<operation name="opGetOrder">
<input message="tns:msgGetOrderRequest"/>
<output message="tns:msgGetOrderResponse"/>
</operation>
</portType>
</definitions>
Another backward compatible change is adding an optional element. In example 3, an available element is added to the LineItemType complex type. This has no impact on existing consumers because they are not required to provide this element in their messages. New consumers or consumer programs redesigned to work with this schema can optionally provide the available element.
1
2
3
4
5
6
7
8
9
10
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="https://vig.com/schema/po" xmlns="https://vig.com/schema/po">
<xsd:element name="LineItem" type="LineItemType"/>
<xsd:complexType name=“LineItemType”>
<xsd:sequence>
<xsd:element name=“productID” type=“xsd:string”/>
<xsd:element name=“productName” type="xsd:string"/>
<xsd:element name=“available” type="xsd:boolean" minOccurs=”0”/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
Forward Compatibility
When a Web service contract is designed in such a manner that it can support a range of future consumer programs, it is considered to have an extent of forward compatibility. This means that the contract can essentially accommodate how consumer programs will evolve over time. The most common means by which forward compatibility is attempted in message definitions is through the use of wildcards as seen in example 4.
1
2
3
4
5
6
7
8
9
10
11
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="https://vig.com/schema/po" xmlns="https://vig.com/schema/po">
<xsd:element name="LineItem" type="LineItemType"/>
<xsd:complexType name=“LineItemType”>
<xsd:sequence>
<xsd:element name=“productID” type=“xsd:string”/>
<xsd:element name=“productName” type="xsd:string"/>
<xsd:any namespace="##any" processContents="lax" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:anyAttribute namespace="##any"/>
</xsd:complexType>
</xsd:schema>
In this example, the xsd:any and xsd:anyAttribute elements are added to allow for a range of unknown elements and data to be accepted by the Web service contract. In other words, the schema is being designed in advance to accommodate unforeseen changes in the future. It is important to understand that forward compatibility is by no means an exact science. A service with a forward compatible contract will often not be able to process all message content. Its contract is simply designed to accept a broader range of data unknown at the time of its design.
Compatible Changes
When we make a change to a Web service contract that does not negatively affect its existing consumers, the change itself is considered a compatible change. A simple example of a compatible change is when we set the minOccurs attribute of an element from “1” to “0”, effectively turning a required element into an optional one, as shown in example 5.
1
2
3
4
5
6
7
8
9
10
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="https://vig.com/schema/po" xmlns="https://vig.com/schema/po">
<xsd:element name="LineItem" type="LineItemType"/>
<xsd:complexType name=“LineItemType”>
<xsd:sequence>
<xsd:element name=“productID” type=“xsd:string”/>
<xsd:element name=“productName” type="xsd:string" minOccurs=”0”/>
<xsd:element name=“available” type="xsd:boolean" minOccurs=”0”/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
List of common compatible changes:
-
Adding a new WSDL operation definition and associated message definitions
-
Adding a new WSDL port type definition and associated operation definitions
-
Adding new WSDL binding and service definitions
-
Adding a new optional XML Schema element or attribute declaration to a message definition
-
Reducing the constraint granularity of an XML Schema element or attribute of a message definition type
-
Adding a new XML Schema wildcard to a message definition type
-
Adding a new optional WS Policy assertion
-
Adding a new WS Policy alternative
Incompatible Changes
If after a change a contract is no longer compatible with consumers, it is considered to have received an incompatible change.
1
2
3
4
5
6
7
8
9
10
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="https://vig.com/schema/po" xmlns="https://vig.com/schema/po">
<xsd:element name="LineItem" type="LineItemType"/>
<xsd:complexType name=“LineItemType”>
<xsd:sequence>
<xsd:element name=“productID” type=“xsd:string”/>
<xsd:element name=“productName” type="xsd:string" minOccurs=”3”/>
<xsd:element name=“available” type="xsd:boolean" minOccurs=”3”/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
What was formerly an optional element is now required. This will certainly affect existing consumers that are not designed to comply with this new constraint, because adding a new required element introduces a mandatory constraint upon the contract.
List of common incompatible changes:
-
Renaming an existing WSDL operation definition
-
Removing an existing WSDL operation definition
-
Changing the message exchange pattern (MEP) of an existing WSDL operation definition
-
Adding a fault message to an existing WSDL operation definition
-
Adding a new required XML Schema element or attribute declaration to a message definition
-
Increasing the constraint granularity of an XML Schema element or attribute declaration of a message definition
-
Renaming an optional or required XML Schema element or attribute in a message definition
-
Removing an optional or required XML Schema element or attribute or wildcard from a message definition
-
Adding a new required WS-Policy assertion or expression
-
Adding a new ignorable WS-Policy expression (most of the time)
Version Identifiers
Any SOAP service-related document must use Semantic Versioning (refer to MUST use semantic versioning)
Version Strategies
Even though there is no de facto versioning technique for the WSDL, XML Schema, and WS Policy content that comprises Web service contracts, a number of common and advocated versioning approaches have emerged, each with its own benefits and tradeoffs.
In this section three known strategies are described:
-
Strict - Any compatible or incompatible changes result in a new version of the service contract. This approach does not support backward or forward compatibility.
-
Flexible - Any incompatible change results in a new version of the service contract and the contract is designed to support backward compatibility but not forward compatibility.
-
Loose - Any incompatible change results in a new version of the service contract and the contract is designed to support backward compatibility and forward compatibility.
Strategy #1: The Strict Strategy (New Change, New Contract)
The simplest approach to Web service contract versioning is to require that a new version of a contract be issued whenever any kind of change is made to any part of the contract.
This is commonly implemented by changing the target namespace value of a WSDL definition (and possibly the XML Schema definition) every time a compatible or incompatible change is made to the WSDL, XML Schema, or WS Policy content-related to the contract. Namespaces are used for version identification instead of a version attribute because changing the namespace value automatically forces a change in all consumer programs that need to access the new version of the schema that defines the message types. Because both compatible and incompatible changes will result in a new contract version, this approach supports neither backward nor forward compatibility.
Pros and Cons
The benefit of this strategy is that you have full control over the evolution of the service contract and because backward and forward compatibility are intentionally disregarded, you do not need to concern yourself with the impact of any change in particular (because all changes effectively break the contract).
On the downside, by forcing a new namespace upon the contract with each change you are guaranteeing that all existing service consumers will no longer be compatible with any new version of the contract. Consumers will only be able to continue communicating with the Web service while the old contract remains available alongside the new version or until the consumers themselves are updated to conform to the new contract.
Therefore, this approach will increase the governance burden of individual services and will require careful transitioning strategies. Having two or more versions of the same service co-exist at the same time can become a common requirement for which the supporting service inventory infrastructure needs to be prepared.
Strategy #2: The Flexible Strategy (Backward Compatibility)
A common approach used to balance practical considerations with an attempt at minimizing the impact of changes to Web service contracts is to allow compatible changes to occur without forcing a new contract version, while not attempting to support forward compatibility at all.
This means that any backward compatible change is considered safe in that it ends up extending or augmenting an established contract without affecting any of the service’s existing consumers. A common example of this is adding a new operation to a WSDL definition or adding an optional element declaration to a message’s schema definition.
As with the Strict strategy, any change that breaks the existing contract does result in a new contract version, usually implemented by changing the target namespace value of the WSDL definition and potentially also the XML Schema definition.
Pros and Cons
The primary advantage of this approach is that it can be used to accommodate a variety of changes while consistently retaining the contract’s backward compatibility. However, when compatible changes are made, these changes become permanent and cannot be reversed without introducing an incompatible change. Therefore, a governance process is required during which each proposed change is evaluated so that contracts do not become overly bloated or convoluted. This is an especially important consideration for agnostic services that are heavily reused.
Strategy #3: The Loose Strategy (Backward and Forward Compatibility)
As with the previous two approaches, this strategy requires that incompatible changes result in a new service contract version. The difference here is in how service contracts are initially designed.
Instead of accommodating known data exchange requirements, special features from the WSDL, XML Schema, and WS Policy languages are used to make parts of the contract intrinsically extensible so that they remain able to support a broad range of future, unknown data exchange requirements.
For example:
-
The anyType attribute value provided by the WSDL 2.0 language allows a message to consist of any valid XML document.
-
XML Schema wildcards can be used to allow a range of unknown data to be passed in message definitions.
-
Ignorable policy assertions can be defined to communicate service characteristics that can optionally be acknowledged by future consumers.
Pros and Cons
The fact that wildcards allow undefined content to be passed through Web service contracts provides a constant opportunity to further expand the range of acceptable message element and data content. On the other hand, the use of wildcards will naturally result in vague and overly coarse service contracts that place the burden of validation on the underlying service logic.
MUST Use a Contract First approach to develop SOAP Web Services
In the contract-first web service, the "contract" (a WSDL definition of operations and endpoints and XML schema of the messages) is created first, without actually writing any service code.
MUST Use the flexible version strategy
As described in Strategy #2: The Flexible Strategy (Backward Compatibility)
Appendix A: References
This section collects links to documents we refer to and base our guidelines on.
Publications, specifications and standards
-
RFC 3339: Date and Time on the Internet: Timestamps
-
RFC 4122: A Universally Unique IDentifier (UUID) URN Namespace
-
RFC 4627: The application/json Media Type for JavaScript Object Notation (JSON)
-
RFC 8288: Web Linking
-
RFC 6585: Additional HTTP Status Codes
-
RFC 6902: JavaScript Object Notation (JSON) Patch
-
RFC 7159: The JavaScript Object Notation (JSON) Data Interchange Format
-
RFC 7230: Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing
-
RFC 7231: Hypertext Transfer Protocol (HTTP/1.1): Semantics and Content
-
RFC 7232: Hypertext Transfer Protocol (HTTP/1.1): Conditional Requests
-
RFC 7233: Hypertext Transfer Protocol (HTTP/1.1): Range Requests
-
RFC 7234: Hypertext Transfer Protocol (HTTP/1.1): Caching
-
RFC 7240: Prefer Header for HTTP
-
RFC 7396: JSON Merge Patch
-
RFC 7807: Problem Details for HTTP APIs
-
RFC 4648: The Base16, Base32, and Base64 Data Encodings
-
ISO 8601: Date and time format
-
ISO 3166-1 alpha-2: Two letter country codes
-
ISO 639-1: Two letter language codes
-
ISO 4217: Currency codes
-
BCP 47: Tags for Identifying Languages
Dissertations
-
Roy Thomas Fielding - Architectural Styles and the Design of Network-Based Software Architectures: This is the text which defines what REST is.
Appendix B: Tooling
This is not a part of the actual guidelines, but might be helpful for following them. Using a tool mentioned here doesn’t automatically ensure you follow the guidelines.
API first integrations
The following frameworks were specifically designed to support the API First workflow with Open API YAML files (sorted alphabetically):
-
Swagger Codegen: template-driven engine to generate client code in different languages by parsing Swagger Resource Declaration
-
Swagger Codegen Tooling: plugin for Maven that generates pieces of code from Open API specification
-
Swagger Plugin for IntelliJ IDEA: plugin to help you easily edit Swagger specification files inside IntelliJ IDEA
The Swagger/Open API homepage lists more Community-Driven Language Integrations, but most of them do not fit our API First approach.
Support libraries
These utility libraries support you in implementing various parts of our RESTful API guidelines (sorted alphabetically):
-
Problem: Java library that implements application/problem+json
-
Problems for Spring Web MVC: library for handling Problems in Spring Web MVC
-
Jackson Datatype Money: extension module to properly support datatypes of javax.money
-
Tracer: call tracing and log correlation in distributed systems
-
TWINTIP Spring Integration: API discovery endpoint for Spring Web MVC
Appendix C: Best practices
The best practices presented in this section are not part of the actual guidelines, but should provide guidance for common challenges we face when implementing RESTful APIs.
Optimistic locking in RESTful APIs
Introduction
Optimistic locking might be used to avoid concurrent writes on the same entity,
which might cause data loss. A client always has to retrieve a copy of an
entity first and specifically update this one. If another version has been
created in the meantime, the update should fail. In order to make this work,
the client has to provide some kind of version reference, which is checked by
the service, before the update is executed. Please read the more detailed
description on how to update resources via PUT in the HTTP Requests
Section.
A RESTful API usually includes some kind of search endpoint, which will then return a list of result entities. There are several ways to implement optimistic locking in combination with search endpoints which, depending on the approach chosen, might lead to performing additional requests to get the current version of the entity that should be updated.
ETag with If-Match header
An ETag can only be obtained by performing a GET request on the single
entity resource before the update, i.e. when using a search endpoint an
additional request is necessary.
Example:
< GET /orders
> HTTP/1.1 200 OK
> {
> "items": [
> { "id": "O0000042" },
> { "id": "O0000043" }
> ]
> }
< GET /orders/BO0000042
> HTTP/1.1 200 OK
> ETag: osjnfkjbnkq3jlnksjnvkjlsbf
> { "id": "BO0000042", ... }
< PUT /orders/O0000042
< If-Match: osjnfkjbnkq3jlnksjnvkjlsbf
< { "id": "O0000042", ... }
> HTTP/1.1 204 No Content
> HTTP/1.1 412 Precondition failed
Pros
-
RESTful solution
Cons
-
Many additional requests are necessary to build a meaningful front-end
ETags in result entities
The ETag for every entity is returned as an additional property of that entity.
In a response containing multiple entities, every entity will then have a
distinct ETag that can be used in subsequent PUT requests.
In this solution, the etag property should be readonly and never be expected
in the PUT request payload.
Example:
< GET /orders
> HTTP/1.1 200 OK
> {
> "items": [
> { "id": "O0000042", "etag": "osjnfkjbnkq3jlnksjnvkjlsbf", "foo": 42, "bar": true },
> { "id": "O0000043", "etag": "kjshdfknjqlowjdsljdnfkjbkn", "foo": 24, "bar": false }
> ]
> }
< PUT /orders/O0000042
< If-Match: osjnfkjbnkq3jlnksjnvkjlsbf
< { "id": "O0000042", "foo": 43, "bar": true }
> HTTP/1.1 204 No Content
> HTTP/1.1 412 Precondition failed
Pros
-
Perfect optimistic locking
Cons
-
Information that only belongs in the HTTP header is part of the business objects
Version numbers
The entities contain a property with a version number. When an update is performed, this version number is given back to the service as part of the payload. The service performs a check on that version number to make sure it was not incremented since the consumer got the resource and performs the update, incrementing the version number.
Since this operation implies a modification of the resource by the service, a
POST operation on the exact resource (e.g. POST /orders/O0000042) should be
used instead of a PUT.
In this solution, the version property is not readonly since it is provided
at POST time as part of the payload.
Example:
< GET /orders
> HTTP/1.1 200 OK
> {
> "items": [
> { "id": "O0000042", "version": 1, "foo": 42, "bar": true },
> { "id": "O0000043", "version": 42, "foo": 24, "bar": false }
> ]
> }
< POST /orders/O0000042
< { "id": "O0000042", "version": 1, "foo": 43, "bar": true }
> HTTP/1.1 204 No Content
or if there was an update since the GET and the version number in the
database is higher than the one given in the request body:
> HTTP/1.1 409 Conflict
Pros
-
Perfect optimistic locking
Last-Modified / If-Unmodified-Since
In HTTP 1.0 there was no ETag and the mechanism used for optimistic locking
was based on a date. This is still part of the HTTP protocol and can be used.
Every response contains a Last-Modified header with a HTTP date. When
requesting an update using a PUT request, the client has to provide this
value via the header If-Unmodified-Since. The server rejects the request, if
the last modified date of the entity is after the given date in the header.
This effectively catches any situations where a change that happened between
GET and PUT would be overwritten. In the case of multiple result entities,
the Last-Modified header will be set to the latest date of all the entities.
This ensures that any change to any of the entities that happens between GET
and PUT will be detectable, without locking the rest of the batch as well.
Example:
< GET /orders
> HTTP/1.1 200 OK
> Last-Modified: Wed, 22 Jul 2009 19:15:56 GMT
> {
> "items": [
> { "id": "O0000042", ... },
> { "id": "O0000043", ... }
> ]
> }
< PUT /block/O0000042
< If-Unmodified-Since: Wed, 22 Jul 2009 19:15:56 GMT
< { "id": "O0000042", ... }
> HTTP/1.1 204 No Content
Or, if there was an update since the GET and the entities last modified is
later than the given date:
> HTTP/1.1 412 Precondition failed
Pros
-
Well established approach that has been working for a long time
-
No interference with the business objects; the locking is done via HTTP headers only
-
Very easy to implement
-
No additional request needed when updating an entity of a search endpoint result
Cons
-
If a client communicates with two different instances and their clocks are not perfectly in sync, the locking could potentially fail
Conclusion
We suggest to either use the ETag in result entities or Last-Modified
/ If-Unmodified-Since approach.
Appendix D: Changelog and References
This change log only contains major changes made after March 2020 by viesure, twinformatics or VIG.
Non-major changes are editorial-only changes or minor changes of existing guidelines, e.g. adding new error code. Major changes are changes that come with additional obligations, or even change an existing guideline obligation. The latter changes are additionally labeled with "Rule Change" here.
To see a list of all changes, please have a look at the commit list in Github.
Rule Changes
-
2020-03-22:Added initial draft of SOAP guidelines (SOAP Guidelines). -
2020-03-11:Added a TODO item to mark sections that need adapations to fit our needs. -
2020-03-10:Added a section for SOAP guidelines (SOAP Guidelines). -
2020-08-19:Adapted pattern for OAuth2 scopes (Security. -
2020-08-19:Removed host name rule. -
2020-09-11:Removed reference to Cursor bases pagination -
2020-09-11:Added Rule V003 "MUST adhere to standardized pagination object"