A REST API is a particular kind of [[API]] served over [[HTTP]]. REST stands for REpresentational State Transfer. A REST API is not necessarily just an API served over HTTP, however. Typically, it is a kind of API that conforms to specific standards.
At the heart of a REST API is resources. Resources can be thought of as objects or types. They are the primary means through which one interacts with a REST API.
A resource is a thing. A blog resource might be represented with the URL path:
/blog
(the domain is omitted)
HTTP verbs are a crucial part of a REST API. There are six HTTP verbs:
GET - retrieves a resource. IdempotentPOST - creates a resource. Side effects expectedPUT - updates a resource. IdempotentPATCH - updates a resource. IdempotentDELETE - deletes a resource. Side effects expectedOPTIONS - lists actions allowed on a resource. IdempotentExamples:
GET /blog/1 - retrieves a blog
GET /blog - retrieves a list of blogs
PUT /blog/1 - with some body, updates blog 1
DELETE /blog/1 - deletes blog 1
POST /blog - creates a new blog
GET /blog/1/posts - retrieves a list of posts
POST /blog/1/posts - creates a new post for blog 1
PUT /blog/1/posts/2 - deletes a post.
The next piece of a REST API are links. Links describe relationships between objects.
When a resource is retrieved via a GET, it should return a body of links describing what options are possible with that resource.
{
"type": "blog",
"id": 1,
"name": "first blog",
"links": {
"self": "/blog/1",
"posts": "/blog/1/posts"
}
}
Finally, metadata describes the type information of a resource.
{
"type": "blog",
"id": 1,
"attributes": {
"name": "foo blog"
},
"meta": {
"type": "blog",
"attributes": {
"name": {
"type": "string",
"maxLength": 20
}
}
}
}
For any attribute specified on the resource, the meta must describe it.