Rest API and Best Practices

created:

updated:

tags: web api

What does REST mean?

REST is an acronym for representational state transfer, and it is “a pattern for client and server communications over a network”.

REST is not a specification but a set of guildines on how to architect a network-connected software system.”

REST provides a set of architectural constraints in regards to performance, scalaibility, simplicity, and reliability in the system.

  • Stateless: “The server won’t maintain any state betwee requests from the client.”
  • Client-Server: “The client and server must be decoupled from each other, allowing each to develop independently.”
  • Cacheable: “The data retrieved from the server should be cacheable either by the client or by the server.”
  • Uniform interface: “The server will provide a uniform interface for accessing resources without defining their representation.”
  • Layered system: “The client may access the resources on the server indirectly through other layers such as a proxy or load balancer.”
  • Code on demand (optional): The server may transfer code to the client that it can run, such as JavaScript for a single-page application."

Rest Web Service

A REST web service is any web service that adheres to REST architecture constraints. These web services expose their data to the outside world through an API. REST APIs provide access to web service data through public web URLs.

You access data from a REST API by sending an HTTP request to a specific URL and processing the response.

API Endpoints

A REST API exposes a set of public URLs that client applications use to access the resources of a web service. These URLs, in the context of an API, are called endpoints.

Characteristics of a well-designed API

  • Easy to read and work with
  • Hard to misuse with informative feedback and not-too-strict guidlines
  • Complete and concise

What are URLs

URL stands for “Uniform Resource Locator” and identifies the online location of a resource.

“A resource has data, relationships to other resources, and methods that operate against it to allow for accessing and manipulating the associated information.”

“A group of resources is called a collection.”

Requests

“An API should strive towards completion, and provide all the required information, data and resources to help developers integrate with them in a seamless manner.”

“The amount of data the resource exposes should also be taken into account. If you’re trying to expose a lot, there can be negative implications on the server, especially with regards to load and performance.”

Responses

Errors

“For an API, errors are a great way to provide context to using an API”.

Error Status Code

  • Client-side errors: 4xx response code
  • Server-side errors: 5xx response code
  • Successful response: 2xx response code

Providing informative precise and concise error message and response code are a great way to improve user’s experience on API and prevent misuse.

References