When you start to build API there is always in your mind how can I make sure my all APIs are interlinked. How can a developer access these API without doing too much reengineering of your APIs? How all API URL can easily and well document through current API?
HATEOAS, an abbreviation for Hypermedia as the Engine of Application State, is a constraint of the REST application architecture. API Hypermedia provides a placeholder in existing API framework, so that you can define and document all of methods related with existing API. A truly RESTful API is with hypertext. By using hypermedia in responses we can offer links between existing endpoint and next possible API endpoints with documentation and corresponding actions. Defining hypermedia within your API leads to standardize your API call and reduce duplicate effort.
There are two key things that make hypermedia APIs useful within APIs:
- shared other APIs information so that developers can communicate with the API
- Documenting and guiding developers so that they can take action along the way
You can define Hypermedia several ways in APIs. Here are few famous Hypermedias available.
1. Hypertext Application Language (HAL) -HAL is an open specification describing a generic structure for RESTful resources. HAL provides its linking capability with a convention which says that a resource object has a reserved property called “_links”. HAL supports JSON and XML.
"_links": {
"self": {
"href": "https://api.vanrish.com/api/user/matthew"
}
},
"id": "matthew",
"name": "Matthew Walter"
2. Collection+JSON – Collection+JSON is a JSON-based read/write hypermedia-type designed to support management and querying of simple collections. A typical Collection+JSON contain a set of links, list of items, a queries collection, and a template object.
{ "collection" :
{
"version" : "1.0",
"href" : "https://api.vanrish.com/user/friends/",
"links" : [
{"rel" : "feed", "href" : "https://api.vanrish.com/user/friends/rss"}
]
}
}
3. JSON-LD — JSON-LD (JavaScript Object Notation for Linked Data ) is a lightweight Linked Data format based on JSON. JSON-LD is designed around the concept of a “context” to provide additional mappings from JSON to an Resource Description Framework(RDF) model. This linking is supported by JSON format.
{
"@context": "https://vanrish.com/api/1.0/user.jsonld",
"@id": "https://vanrish.com/api/1.0/matthew",
"name": "Matthew Walter",
"age": "40",
"homepage": "https://www.vanrish.com/"
}
4. Siren— Siren is a hypermedia specification for representing entities, offering structures to communicate information about entities. An Entity is a URI-addressable resource that has properties and actions associated with it. It may contain sub-entities and navigational links. Siren supports JSON and XML format.
{
"class": [ "order" ],
"properties": {
"orderNumber": 24,
"itemCount": 3,
"status": "pending"
},
"entities": [
{
"class": [ "items", "collection" ],
"rel": [ "https://api.vanrish.com/rels/order-items" ],
"href": "https://api.vanrish.com/orders/24/items"
},
{
"class": [ "info", "customer" ],
"rel": [ "https://api.vanrish.com/rels/customer" ],
"properties": {
"customerId": "5675433",
"name": "Matthew Walter"
},
"links": [
{ "rel": [ "self" ], "href": "https://api.vanrish.com/customers/5675433" }
]
}
],
"links": [
{"rel": [ "self" ], "href": "https://api.vanrish.com/orders/24" },
{ "rel": [ "previous" ], "href": "https://api.vanrish.com/orders/23" },
{ "rel": [ "next" ], "href": "https://api.vanrish.com/orders/25" }
]
}
5. JSON API — JSON API is a specification for how a client should request that resources be fetched or modified, and how a server should respond to those requests. Jason API ensures separation between client and server and also reducing the number of call without impacting discoverability. This is Json based and one of popular hypermedia for API.
{
"type": "user",
"id": "1",
"attributes": {
"title": "User Information"
},
"relationships": {
"company": {
"links": {
"self": "https://api.vanrish.com/articles/1/relationships/user",
"related": "https://api.vanrish.com/articles/1/user"
},
"data": { "type": "people", "id": "9" }
}
},
"links": {
"self": "https://api.vanrish.com/articles/1"
}
}
These are few famous API hypermedia, developer are using to link their APIs and document. Along with all these hypermedia there are some more less popular hypermedia like Uber, Mason, Cj, Yahapi, Paypal,OData and CPHL.
There are few draw back about using hypermedia within API.
- More data transport through network for hypermedia.
- It makes complex to process and understand these links within APIs.
API Hypermedia is still not yet standardize. Most of these API hypermedias are still evolving and coming with new standard. This is one of the most active communities and developers are coming forward with their new API hypermedia concept.
Rajnish Kumar is CTO of Vanrish Technology with Over 25 years experience in different industries and technology. He is very passionate about innovation and latest technology like APIs, IOT (Internet Of Things), Artificial Intelligence (AI) ecosystem and Cybersecurity. He present his idea in different platforms and help customer to their digital transformation journey.