- Status: Work in progress
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.
The media type of a Yahapi document is application/json
.
There are no plans to register a Yahapi-specific media type.
A Yahapi document MUST contain at least one resource. A minimum valid document is:
{}
A resource object MUST have a type
property if it contains different properties than other resources in the same collection.
All resource object with the same type
MUST have the same set of properties.
The following is correct:
GET /products/9016
{
"type": "food",
"id": 9016,
"expiration_date": "2015-03-06"
}
GET /products/9017
{
"type": "non-food",
"id": 9017
}
The following is incorrect assuming expirationDate
is mandatory:
GET /products/9016
{
"type": "food",
"id": 9016,
"expiration_date": "2015-03-06"
}
GET /products/9017
{
"type": "food",
"id": 9017
}
A resource object SHOULD contain a links
property containing valid URLs keyed by their relationship relative to the current resource.
{
…
"links": {
"self": { "href": "/orders/001342" },
"payment": { "href": "/payment/53415" }
}
}
When a link
property is set it SHOULD contain a self
-link.
Every links
object MUST contain a href
property (hypertext reference) with a valid URL.
A links
object MAY contain additional properties for the purpose of content negation.
A resource MAY contain a meta
property with properties describing the design and specification of the resource object.
A collection resource is a list of resource objects and may support ordering, filtering and/or paginating through them.
A collection resource MUST be homogeneous and contain only elements with a shared set of properties.
The following is invalid if either items.name
or items.id
are mandatory:
{
"items": [
{ name: "John" },
{ id: 1234 }
]
}
A collection resource MAY contain different types
if all types share the same abstraction. In the following example all items are considered a product
:
{
"products": [
{
"type": "food",
"id": 4897884,
"expiration_date": "2015-03-06"
},
{
"type": "non-food",
"id": 9016
}
]
}
A collection resource too large to fit in a single message usually supports some way to paginate through the collection results.
A response of a paginated collection resource looks something like this:
GET /products?offset=20&limit=10
{
"products": [ … ],
"links": {
"next": { "href": "https://api.example.com/products?offset=30&limit=10" },
"prev": { "href": "https://api.example.com/products?offset=10&limit=10" }
},
"meta": {
"total": 46,
"limit": 10,
"offset": 20
}
}
A paginated collection resource SHOULD support offset
and limit
url query parameters to paginate through a collection resource.
GET /products?offset=20&limit=10
A paginated collection resource MUST return links.next
and links.prev
to paginate through the collection unless there is no next or previous page.
A paginated collection resource MAY return the number of items available in the collection using meta.total
.
A paginated collection resource SHOULD return the number of records returned using meta.limit
.
A paginated collection resource SHOULD return the number of items skipped using meta.offset
.
A paginated collection resource MAY return the number of items returned in the response using meta.size
.
There are no strict rules about the default ordering of a collection. A collection may be ordered by default in ascending or descending order for any property that makes the most sense for your use case.
A collection resource MAY support client sort in a collection by adding a sort
parameter to the query string.
GET /products?sort=type
The default order of a sort
query parameter MUST be ascending.
To sort a collection in descending order the value of a sort
query parameter MUST start with a minus symbol (-
).
GET /products?sort=-expirationDate
A collection resource supporting multiple sort criteria SHOULD allow multiple criteria in the sort
query string separated by comma's:
GET /products?sort=type,-expirationDate
The query string in the example above sorts products by type
in ascending order first, and sorts all items of the same type
by expirationDate
in descending order.
An object or array inside a resource representation is referred to as an embedded resource.
Embedding resources in a response can simplify client logic, save bandwith and reduce network latency. Usually embedded resources have a strong relationship with the host resource.
An embedded resource object MAY have a type
, links
, link
and/or meta
property.
An embedded resource MAY be referenced within the host resource object in the their links
with a name identical to the embedded resource key.
GET /persons/john
{
"name": "John",
"address": {
"street": "221A Baker Street",
"type": "home_address"
},
"links": {
"self": { "href": "/persons/john" },
"address": { "href": "https://api.example.com/addresses/16342" }
}
}
The example above shows the resource person
with an embedded resource address
.
An embedded collection resource SHOULD NOT support pagination.
An embedded collection resource SHOULD NOT support ordering.
An embedded collection resource should be kept simple and contain either the entire or most relevant subset of items. Embedding a collection resource is an optimization to support the most common use cases for your API, no more.
An error object MUST be returned when the HTTP status code is in the 400-499 range.
An error object SHOULD be returned when the HTTP status code is in the 500-599 range.
Example:
{
"code": "validation_error",
"message": "One or more request parameters are invalid",
"errors": [
{
"code": "invalid_type",
"path": "/parentId",
"message": "invalid type: string (expected number)"
}, {
"code": "maximum_decimals",
"path": "/amount",
"message": "must have no more than 2 decimals"
}
]
}
An error SHOULD contain a code
property with an error description suitable for machine interpretation.
An error SHOULD contain a message
property with an error description in a human-readable format.
An error MAY contain an errors
property with a list of sub-errors.
Sub-errors are used when multiple errors were detected that fall within the same category.
A sub-error is a normal error but SHOULD NOT contain status
or errors
properties.
A sub-error MAY contain a path
property to let the client identify which request parameter was the cause of the problem.
Nested objects in error.path
are separated by a slash (e.g. /root/nested/property
).
Array elements in error.path
are identified by their index between brackets starting with zero (e.g. /root/children[0]/name
).
{
"files": [
{
"id": "123",
"extension": "png"
},
{
"id": 56,
"extension": "png"
}
]
}
The request above might return the following error response:
{
"code": "validation_error",
"message": "One or more request parameters are invalid",
"errors": [
{
"code": "invalid_type",
"path": "/files[1]/id",
"message": "invalid type: number (expected string)"
}
]
}
Your character encoding MUST be UTF-8.
Content-Type: application/json; charset=utf-8
Your property names MUST should be written consistently in either lowerCamelCase or snake_case.
Your property names MUST be predictable, avoid property names that are dynamically generated by the context.
Predictable API's are easier to parse in static languages and are better supported in technologies such as JSON Path and JSON Schema.
All dates MUST be formatted using ISO 8601.
Avoid unix timestamps.
Your API SHOULD use HTTPS encrypting with SSL/TLS.
If you need to support cross-domain requests you SHOULD use CORS, not JSONP.
2015-05-20:
- Renamed
previous
toprev
due to http://www.iana.org/assignments/link-relations/link-relations.xhtml
2015-05-17:
- Weakened embedded resource identity from MUST to MAY.
- Revised text of embedded resource representation.
2015-05-16:
- Weakened
meta.total
from SHOULD to MAY. - Added
meta.size
which MAY contain the number of items returned in the response.
2015-05-15:
- Removed
error.status
property. - Removed reserved words.
- Links are allowed to contain additional properties for the purpose of content negotiation.
prev
link renamed toprevious
.- Strengthened resource type requirement from SHOULD to MUST.
2015-04-06:
- Error response no longer wrapped within
error
object. - Weakened
error.status
requirement from SHOULD to MAY. - Removed absolute url requirement for
links
. - Strengthened resource type requirement from MAY to SHOULD.
2014-08-06:
- First draft