6.API

API

Application Programming Interface: a piece of software that can be used by another piece of software, in order to allow applications to talk to each other.

REST Architecture ti build APIs

REST - Representational States Transfer is a way of building APIs in logical way, making them easy to consume.

To build RESTful APIs, means APIs that follow REST architecure * Separate API into logical resources. * Expose structured, resource-based URLs * Use HTTP methods (verbs) -- verbs should be in HTTP methods, not URLs (endpoint). * send data as JSON * be stateless

Resource is an object or representation of something, which has data associated to it. Any information that can be named can be a resource.
Eg: tours, reviews

Endpoints should contain only resources(nouns), and use HTTP methods for actions.

To Create a resource - POST
To read a resource - GET
To update a resource - PUT or PATCH
To delete a resource - DELETE

Thus, HTTP methods are used for CRUD(create, read, update and delete) operations.

To send the JSON data to client, we usually perform response formatting. The standard format we mostly use is JSend. This contains status message and data with original data.

We have some other standards for response formatting like JSON:API and OData JSON Protocol.

Finally, RESTful API should always be stateless.
Stateless RESTful API: All state is handled on the client. This means that each request must contain all the information necessary to process a certain request. The server should not have to remember previous requests.