DEV Community

Lokesh
Lokesh

Posted on

REST API

What Is an API?

API stands for Application Programming Interface.

In simple terms, an API allows different applications to communicate with each other.

For example, a frontend application can send a request to a server and receive data in return.

What Is a REST API?

REST stands for Representational State Transfer.

A REST API is a common way for applications to communicate over the internet using HTTP.

For example, an e-commerce application may use an API to:

  • Get all products.
  • Get a single product.
  • Add a new product.
  • Update a product.
  • Delete a product.

HTTP Methods

REST APIs commonly use different HTTP methods to perform different operations.

1. GET

GET is used to retrieve data.

Example:

GET /products
Enter fullscreen mode Exit fullscreen mode

This can be used to get a list of products.

fetch("https://dummyjson.com/products")
    .then((response) => response.json())
    .then((data) => {
        console.log(data);
    });
Enter fullscreen mode Exit fullscreen mode

2. POST

POST is used to send new data to the server.

For example, we can use it to create a new product or user.

POST /products
Enter fullscreen mode Exit fullscreen mode

3. PUT

PUT is commonly used to replace or update a resource.

Example:

PUT /products/1
Enter fullscreen mode Exit fullscreen mode

4. PATCH

PATCH is used to partially update a resource.

For example, we may only want to update a product's price.

PATCH /products/1
Enter fullscreen mode Exit fullscreen mode

5. DELETE

DELETE is used to remove data.

Example:

DELETE /products/1
Enter fullscreen mode Exit fullscreen mode

What Is an Endpoint?

An endpoint is a specific URL used to access a resource through an API.

For example:

/products
Enter fullscreen mode Exit fullscreen mode

This endpoint may be used to access product data.

Another example:

/products/1
Enter fullscreen mode Exit fullscreen mode

This endpoint may be used to access a specific product.

REST API and JSON

REST APIs commonly send and receive data in JSON format.

Example:

{
    "id": 1,
    "name": "Laptop",
    "price": 50000
}
Enter fullscreen mode Exit fullscreen mode

The frontend can receive this data and display it to the user.

Simple Example

Imagine an e-commerce website.

The frontend sends a request:

GET /products
Enter fullscreen mode Exit fullscreen mode

The server responds with product data:

[
    {
        "id": 1,
        "name": "Laptop"
    },
    {
        "id": 2,
        "name": "Mobile"
    }
]
Enter fullscreen mode Exit fullscreen mode

The frontend can then display these products on the webpage.

Important REST API Concepts

Here are some important things to remember:

  • API – Allows applications to communicate.
  • REST – A common architectural style for designing web APIs.
  • Endpoint – A URL used to access a resource.
  • GET – Retrieves data.
  • POST – Creates new data.
  • PUT – Replaces or updates a resource.
  • PATCH – Partially updates a resource.
  • DELETE – Removes data.
  • JSON – A common format for exchanging data.

REST APIs are an important part of modern web development. They allow frontend and backend applications to communicate with each other.

Top comments (0)