A REST API is a set of rules that allows two computers to communicate over the internet using the HTTP protocol. It acts as a translator, enabling a client, such as a web browser, to request specific data or actions from a server using standard web addresses.

HTTP Methods Define the Action

REST relies on standard HTTP methods to tell the server exactly what to do with a resource. Each method has a specific purpose, which ensures that the server understands the intent of the request without needing complex instructions. You use these methods to manage data in a predictable way.

The GET method is used to retrieve data. When you enter a URL in your browser, you are performing a GET request to fetch a webpage or an image. This action is read-only and does not change the state of the data on the server. For example, requesting a list of books from a library database uses GET.

The POST method creates a new resource. You use this when submitting a form or signing up for a new account. The data you send is contained in the body of the request, rather than the URL. This allows you to send larger amounts of information securely to the server for storage.

The PUT method updates an existing resource. Unlike POST, which creates something new, PUT replaces a specific resource entirely with new data. If you change your entire profile bio and profile picture at once, the application likely uses a PUT request to overwrite the old record with the new one.

The PATCH method performs a partial update. This is more efficient than PUT when you only need to change one small detail. If you only update your phone number in a settings menu, a PATCH request sends only that specific piece of data to the server, leaving the rest of your profile untouched.

The DELETE method removes a resource. This is a straightforward command that tells the server to erase a specific entry. When you click a trash icon to remove a photo from a gallery, the client sends a DELETE request to the server's specific address for that image.

Resources and URIs Organise the Data

In a REST system, every piece of information is treated as a resource. A resource can be a user, a product, a photo, or a document. To find these resources, REST uses Uniform Resource Identifiers, or URIs, which look like standard web addresses. These URIs are structured hierarchically to make them intuitive.

A well-structured URI uses nouns instead of verbs. You do not use words like /getUsers or /deleteProduct in the address. Instead, you use /users or /products. The action is defined by the HTTP method, not the URI. This separation keeps the API clean and easy to maintain as it grows.

To access a specific item, you append a unique identifier to the resource path. For example, /users/123 targets the specific user with the ID 123. This allows the server to pinpoint exactly which record to retrieve, update, or delete. Using IDs ensures that requests are precise and prevents the accidental modification of the wrong data.

You can also use query parameters to filter or sort resources. These appear after a question mark in the URI. For example, /products?category=books tells the server to return only the products that belong to the books category. This reduces the amount of data transferred over the network, making the application feel faster for the user.

JSON Formats the Communication

While REST can technically use different formats, JSON (JavaScript Object Notation) is the standard for modern APIs. JSON is a lightweight text format that is easy for humans to read and easy for machines to parse. It uses key-value pairs to organize data, similar to a dictionary.

A typical JSON response looks like a set of curly braces containing labels and values. For example, a user resource might look like this: { "id": 123, "name": "Jane Doe", "email": "jane@example.com" }. The labels are always in quotes, and the values can be strings, numbers, or booleans. This consistency allows any programming language to understand the data regardless of how the server was built.

One common misconception is that REST is a programming language or a specific piece of software. In reality, REST is an architectural style, which is a set of guidelines. You can build a REST API using Python, JavaScript, Ruby, Java, or any other language. As long as the server follows the rules of HTTP and uses URIs to identify resources, it is considered RESTful.

The server also sends an HTTP status code with every response. These three-digit numbers tell the client if the request was successful. A 200 OK means the request worked. A 201 Created confirms a new resource was successfully added via POST. A 404 Not Found means the URI does not exist. A 500 Internal Server Error indicates a crash on the server side. These codes allow the client to react appropriately, such as showing an error message to the user.

An Example of a REST Transaction

To see how these pieces fit together, imagine a simple API for a digital bookstore. The server holds a database of books, and the client is a mobile app. The app needs to update the price of a specific book.

The app identifies the book with ID 50. The current price is 15.00 USD, and the app wants to change it to 12.00 USD. The app sends the following request:

  1. Method: PATCH
  2. URI: /books/50
  3. Body (JSON): { "price": 12.00 }

The server receives this request. It looks up the book with ID 50 in the database. It sees the existing record and updates only the price field from 15.00 to 12.00. The server then sends back a response to the app:

  1. Status Code: 200 OK
  2. Body (JSON): { "id": 50, "title": "The Great Novel", "price": 12.00, "status": "updated" }

The app receives the 200 OK code and the updated JSON. It then refreshes the screen to show the new price to the user. This entire exchange happens in milliseconds. Because the app used PATCH instead of PUT, it did not have to send the book's title, author, or description back to the server, saving bandwidth.

Statelessness Ensures Scalability

A core rule of REST is that it must be stateless. This means the server does not remember anything about the client between requests. Every single request from the client must contain all the information the server needs to understand and complete the task. The server does not store a "session" for the user.

If you are logged into an app, the server does not remember that you are logged in based on a previous request. Instead, the client sends an authentication token, usually in the HTTP header, with every single request. This token acts like a digital ID card. The server checks the token, verifies who you are, and then processes the request.

Statelessness is important because it allows the API to scale. If a website becomes very popular, the owners can add ten more servers to handle the traffic. Because no server stores session data, a client's first request can go to Server A, and their second request can go to Server B without any loss of information. The servers do not need to talk to each other to remember who the user is.

To implement this, you should ensure your client-side code manages the state. The app should store the user's token and preferences locally. When a request is made, the app attaches these details. This keeps the server lightweight and fast, as it only has to process the current request and return a response before moving to the next task.