Pages

Sunday, January 7, 2024

RESTful API


REST is the most common communication standard between the two computers over internet. API stands for Application programming interface, It is the way for two computers to talk to each other. The common API standard used by most mobile and web applications to talk to web servers is called REST. Stands for Representational State Transfer. Rest is not a specification, it is a loose set of rules that has been the common standard for building web API since early 2000s. An API that follows the REST standard is called an RESTful API. Some real life examples are Google Maps, Stripe (With the Stripe dashboard mobile app, you can securely log in to your Stripe account and manage your payments on the go).

A RESTful API organizes resources into a set of unique URIs, or Uniform resource identifiers. The URI differentiate different types of resources on a server. Eg The resources should be grouped by noun not verb.An API to get all products should be slash products and not slash getALLProducts. A client interacts with a resource by making a request to the endpoint for the resource over HTTP/HTTPS. The request has a very specific format, the URI for the resource to access, the URI is preceded by an HTTP verb which tells the server what we want to do with resource. 

A POST request means we want to create a new resource

GET means we want to read a data about an existing resource

PUT is for updating an existing resource

DELETE is for removing existing resource

CRUD stands for all above

In the body of these request, there could be optional HTTP request body that contains a custom payload of data, usually encoded in JSON. The server receives the request, process it, and format a result into a response. The first line of response contains the HTTP status code to tell the client what happened to the request. A well implemented RESTful API returns proper HTTP status code.

The 200 level means the request was successful

400 level means something is wrong with our request, could be incorrect syntax

500 level means something went wrong at the server level eg service was un-available

A well behaved client could choose to retry the failed request with 500 level status code, means some actions are not idempotent (implies that a certain operation can be applied many times, without changing the result. Essentially, it is like multiplying a number by zero) and those requires extra care when retrying. When an API is idempotent, making multiple identical request has the same effect as making a single request. This is usually not the case for POST request to create a new resource. 

The response body is optional and could contain the data payload and usually formatted in JSON.

A REST implementation should be stateless. It means the two parties need not to store any information about each other, and every request and response is independent from all others. This leads to web applications that are easy to scale and well behaved. If an API endpoint returns a huge amount of data use pagination. A common pagination scheme uses "limit" and "offset" as parameters. If they are not specified server should assume sensible default values.Lastly versioning of API is very important. Versioning allows an implementation to provide backward compatibility so that if we introduce breaking changes from one version to other, consumer get enough time to move to the next version. There are many ways to version a API, the most striaghtforward is to prefix the version before the resource on the URI eg /v1/products, /v2/products.

There are other popular API options like GraphQL and gRPC.

###

A RESTful API (Representational State Transfer Application Programming Interface) is an architectural style for designing networked applications. It relies on a stateless, client-server communication protocol, typically using HTTP or HTTPS, and employs standard HTTP methods such as GET, POST, PUT, DELETE, and PATCH for data manipulation. Here are some common use cases for RESTful APIs:

1. **Web Services**: RESTful APIs are commonly used to expose web services that allow clients to interact with server-side resources over the web. These services can provide access to a wide range of functionality, such as user authentication, data retrieval, data manipulation, and business logic execution.


2. **Mobile Applications**: RESTful APIs are often used to power mobile applications by providing access to backend services and data. Mobile apps can use RESTful APIs to perform tasks such as user authentication, fetching and displaying data from a server, submitting form data, and updating user preferences.


3. **Single Page Applications (SPAs)**: SPAs built using JavaScript frameworks like Angular, React, or Vue.js often use RESTful APIs to communicate with backend servers. The API endpoints allow the SPA to fetch data from the server, update the UI based on user interactions, and send data back to the server for processing.


4. **IoT (Internet of Things)**: RESTful APIs can be used to enable communication between IoT devices and backend servers. IoT devices can use HTTP requests to send sensor data to a server for processing and analysis, receive commands or configuration updates from the server, and trigger actions based on server responses.


5. **Integration with Third-Party Services**: RESTful APIs are commonly used to integrate with third-party services and platforms, such as payment gateways, social media platforms, mapping services, weather APIs, and more. Developers can use RESTful API endpoints provided by these services to access their functionality and data from within their own applications.


6. **Microservices Architecture**: In a microservices architecture, RESTful APIs are used to enable communication between different microservices. Each microservice exposes a set of RESTful endpoints that allow other microservices or client applications to interact with its functionality and data.


7. **Content Management Systems (CMS)**: RESTful APIs are used in content management systems to provide programmatic access to content and site management features. Developers can use RESTful API endpoints to create, read, update, and delete content on a website, as well as manage users, roles, and permissions.


8. **E-commerce Platforms**: RESTful APIs are commonly used in e-commerce platforms to enable integration with third-party services such as payment gateways, shipping providers, and inventory management systems. Developers can use RESTful API endpoints to perform tasks such as processing orders, updating product information, and managing customer accounts.


Overall, RESTful APIs are a versatile and widely used approach for building scalable, interoperable, and extensible web services and applications. They enable seamless communication between client applications and backend servers, allowing developers to build complex, distributed systems that can be easily extended and integrated with other services and platforms.





No comments:

Post a Comment