Guideline for creating RESTful APIs at Acuris, in order to drive consistency between our APIs.
GET /companies and PUT /companies not GET /getCompanies/companies not /company. It avoids confusion about whether we’re talking about a single resource or a collection and more directly maps to how it might be written in code. e.g.
GET /companies -> companies
POST /companies -> companies.push(data)
GET /companies/1 -> companies[1]
PUT /companies/1 -> companies[1] = { name:'foo', sector: 'bar' }
GET /companies/1/name -> companies[1].name
$ curl http://api.example.com/v1/reviews/269
[
{
"country": "Argentina",
"province": "Mendoza Province",
"variety": "Cabernet Sauvignon",
"kind": "review",
"price": "15.0",
"geography": "Argentina#Mendoza Province#Agrelo#",
"description": "Immediately this smells raisiny, but with time secondary scents of cinnamon and Middle Eastern spices come into play. The body is soft and extracted, with modest acidity. Flavors of baked black fruits, fig paste, chocolate and herbs follow the nose, as does a chocolaty, rich finish. Drink through 2016.",
"id": "269",
"designation": "Single Vineyard Reserva",
"winery": "Lamadrid",
"region": "Agrelo",
"points": "88"
}
]
Accept in request headers and Content-Type in the response headers to indicate the media type of the response. Most of our APIs respond with JSON so setting the Content-Type to application/json would be a sensible default in the case that no Accept header is passed $ curl -i http://api.example.com/resources?unknownParam=true
HTTP/1.1 400 Bad Request
{
"message":"There were unknown parameters in the request \"unknownParam\"",
"validParams":[
"after",
"before",
"size",
"countries",
"q",
"fields",
"sort"
]
}
"Value1 !== value1"http://api.example.com/v1/reviews?countries=germany,spainq parameter containing the url encoded string to be searched.
e.g. show only reviews that mention “acidity” http://api.example.com/v1/reviews?q=aciditysize and offset parameters to define the bounds of the pages. If a size is not passed, fallback to a sensible default+ and - characters to determine field sort order. e.g. show all reviews by their review score descending: http://api.example.com/v1/reviews?sort=+points<, >, !=, or other logical operators, consider passing the query in the body of the request as illustrated in the Elasticsearch APIX-Correlation-Id) in the error response to allow the request to be traced through your system which can either be passed in as a request header or generated on the server by your client.$ curl -i http://api.example.com/error
HTTP/1.1 504 Internal Server Error
{
"error": {
"code":"ISS-12",
"message": "An error was received from the database, please try again later"
}
}
500 status code504 status to inform the client that the service is temporarily unavailable and the request should be retried429 error code along with a Retry-After headerWhere possible, stick to common conventions for query parameter names as follows:
| Parameter Name | Description |
|---|---|
mmgid |
the ID of a resource in the Prime database. Ensure that it is structured as <source>-<id-within-source> e.g prime-1234 or docrepo-10201 |
fields |
if allowing the client to specify a subset of fields to be returned |
sort |
to allow the ordering of returned results. For specific ordering use + for ascending and - for descending |
before and after |
if using cursoring, provide the previous and next cursor values. Where after is a pointer to the last resource in the current page and before is a pointer to the first resource in the current page. |
offset |
if using pagination, provide the offset of the first item and use in conjunction with size to provide the number of items per page |
size |
the number of items returned in the response |
/v1/companies/prime-123latest version. This removes ambiguity around what the API will respond withv1, v2) not (v1, v1.1)X-Correlation-Id/companies and /companies/ should return the same response