DEV Community

Modgil
Modgil

Posted on

Designing REST APIs

What are API's and why do we need them?

API (Application Programming Interface) comes into picture when you want to interact with a system to retrieve some information or perform a task. API's act as mediator between the client and server. It serves the information to the client from the server whenever called.

There are many advantages of API's like API's make data accessible, API's help in making product seamless from start to finish.

What are REST API's?

Representational State Transfer, a way of accessing web services in a simple way. In simple words it is an API which follows REST Design Principles. REST API's provide high flexibility and easiness to developers as REST API's can be developed using any programming language and supports a variety of data formats. Only requirement is that it should follow the REST Principle.
A Restful system includes a client who requests for the information and a server who has the information.It is important to create REST API according to industry standards which results in ease of development and easy client adoption.
Architectural Constraints of RESTful API:

  • Uniform Interface
  • Stateless
  • Cacheable
  • Client-Server
  • Layered System
  • Code on Demand We will discuss these REST API’s constraints in detail, we should consider these points before designing an API.

-> Client-Server : API should follow client server architecture. A server having many endpoints for different functionalities and client (Application) consuming them using HTTP requests. There should not be any dependency between client and server applications. The only information required for a client should be URI (Uniform Resource Identifier) of the requested resource.

-> Uniform Interface : Deciding resource URI’s, these URI’s are nothing but endpoints for API’s. URI should be the same for similar kinds of requests. For e.g. :
Information for user profiles for different users should come from the same URI. URI for the same can look like -> /profile , /profile{id} .

-> Stateless : Communication between client and server should be stateless.Stateless means there will be no information from past transactions, each request is separate and independent of each other.

-> Cacheable : Whenever required, or we need to make an application more scalable we can cache the response on the client side. For e.g.: If we are making the same request again and again, we can simply cache the response . It will lead to a more scalable product as less requests will be made.

-> Layered System : REST API’s use a layered system. A layered system means a system with different units of functionality. These layers serve different purposes like providing security, authentication, load balancing or any other functionality according to our requirements. We can decide the layers of our API according to the business requirements while designing it.

-> Code on Demand : Servers can send some kind of executable scripts to clients to increase or change functionality on the client side. It's an optional constraint.
If a service violates any other constraint, it cannot strictly be referred to as RESTful.

Working of REST API’s

REST API’s communicate via HTTP requests to perform CRUD operations on DB.
Request headers and parameters are also important in REST API calls as they include information such as authorisation, URI, caching and more.
Request headers and parameters along with HTTP status codes are used for designing REST API’s.
Request API implements many HTTP request methods and before designing the API we should consider them, we need to decide which kind of request we are going to make to perform the required task.

HTTP Request Meaning
GET Retrieve information
POST Send information
PUT/PATCH Update information
DELETE Delete information

Status Codes :
Status codes are the codes which a website's server sends to the browser to indicate whether a request can be fulfilled or not . Different response codes indicate different messages. We know that response can be either successful or failed but in order to know the reason for failure or success we can use these status codes. Understanding of status codes and their meaning is very important while designing the REST APIs.

Success Codes : 200 - OK , 201 - Created , 204 - No Content

Error Codes :

Error from client Side: 400 - Bad Request, 401 - Unauthorised, 403 - Forbidden, 404 - Not Found

Server Error : 500 - Internal Error , 501 - Not Implemented , 502 - Bad Gateway, 503 - Service Unavailable, 504 - Gateway Timeout

Top comments (0)