DEMYSTIFYING REST APIs: Everything you need to know

DEMYSTIFYING REST APIs: Everything you need to know

In today's interconnected world, the web and mobile applications are at the heart of our digital lives.
At the core of these applications are REST APIs, which serve as the backbone for communication between different systems over the internet.
In this article, we'll take a closer look at what APIs are, what REST APIs are, the necessary features of REST APIs, how they work, and where they can be used in modern software development.

WHAT IS AN API?

An Application Programming Interface(API) is a set of protocols that simplifies communication such as data exchange between software applications.

In simpler terms, an API is like a waiter at a restaurant. Just as a waiter takes your order, delivers it to the kitchen, and brings your food back to you, an API takes a request from one software application, communicates that request to another application, and then returns the response to the original application.

What are REST APIs?

Representational State Transfer(REST) is a software architecture created in the 2000s to improve the performance, scalability, and simplicity of APIs.
The Rest architecture does this by using the HYPERTEXT TRANSFER PROTOCOL(HTTP) method.

Rest is based on a set of rules that define how the client and the server should interact and also, how an API should be built.
Some of these rules are:

  • the API should be separated into two groups which are, the client and the server

  • the API should always say everything it needs so it can carry out the request without missing any vital information

  • the APIs should allow different software systems to talk to each other in a way everyone can understand

FEATURES OF THE REST ARCHITECTURE

Some key features of the REST ARCHITECTURE you should know are:

  1. The Rest Architecture is Stateless
    This means that the state is handled by the client(browser). An example of a state is whether the client is logged in or what page is the client currently viewing. Therefore any request sent to the server must contain all the necessary information the server needs to process the request.

  2. The Rest Architecture is Resource-based
    Rest Apis are organized around resources. A Resource is a representation of something which has data associated with it. Any information that can be named can be a resource - for example, users, products, pricing, etc.

  3. The Rest Architecture uses a Uniform-interface
    The Rest architecture makes use of a uniform interface, Meaning that they use the standard HTTP methods and media types(JSON or XML) to communicate between client and server. This makes Rest APIs significantly faster.

  4. The Client-server architecture
    The Client-server architecture means that the client and server are separated from each other. This helps to improve flexibility and the client and server can evolve separately over time.

HOW DO THE REST APIs WORK

Certainly, by now we know the fundamentals already. Let's now learn how a Rest API works.

Let's say we send a request to a Rest API, which means we are asking for specific information or we want the API to perform a certain task. Typically these requests are made using the HTTP methods.
The Rest API will then process your request. If your request can be carried out, It will then send a response in a structured format.

To receive a response from the API, you must first, know the request you need to make to receive your desired information.
Typically, we have 5 types of requests which can also be called HTTP methods, they include:

  1. GET
    We use GET requests to retrieve information from a resource in an API.
    If this request is valid, you will receive a response from the API with the 200 status code which means "OK".
    If for some reason an error arises, you will most likely get a 404 status code which means "NOT FOUND".

  2. POST
    We use POST requests to create a new resource or create a subordinate resource inside a parent resource on an API.
    When a new resource or a subordinate resource is created successfully the API sends a status code of 201 which means "CREATED".
    It also includes a location header containing links to the newly created resources.

  3. PUT
    We use PUT requests to replace an existing resource.
    When you send a PUT request you are telling the API to replace a specific resource with an updated version.
    A successful update returns a 200(OK) status code.

  4. PATCH
    We use PATCH requests to update particular information on a resource without replacing the entire resource.
    For example, Let's say you have an online bookstore. If you want to update the price of a book.
    Instead of sending a PUT request which will replace the entire resource, you can send a PATCH request with a new price of the specific book and the server will update the price of the specific book without affecting others.

  5. DELETE
    The DELETE request, just as the name implies is used to delete a resource.
    Upon a successful delete, you will get the 200 "OK" status code.

USES OF REST APIs

They can be used in quite several ways. Some of which are:

  • Web applications

  • Mobile applications

  • Cloud applications

  • IoT devices

COMMON CHALLENGES OF REST APIs

In addition to the limitations imposed by design and architecture, there are various challenges that individuals may encounter when working with REST APIs. These challenges can involve complex concepts such as:

  • Endpoint consistency - paths of endpoints should be consistent by following common web standards, which may be difficult to manage.

  • API versioning - endpoint URLs shouldn't be invalidated when used internally or with other applications.

  • Long response times and too much data - the amount of returned resources can increase in size in time, adding to increased load and response times.

  • Authentication - use common authentication methods such as HTTP basic authentication (which allows for a base64-encoded username: password string), API keys, JSON Web Tokens, and other access tokens. OAuth 2.0, for example, is good for access control.

  • Requests and data - requests may have more data and metadata than needed or more requests may be needed to obtain all the data. APIs can be adjusted for this.

CONCLUSION

REST APIs allow different computer systems to communicate with each other over the internet, providing a scalable and flexible approach to designing web services.
While there may be some challenges when working with REST APIs, they remain a popular choice for developers due to their simplicity and ability to support a wide range of programming languages and platforms.