REST API
A REST API (Representational State Transfer Application Programming Interface) is an architectural style that allows two computer systems to communicate over the internet. It acts as a translator, enabling a client (like a mobile app or a browser) to request data or trigger actions on a server using standard web protocols.
Because REST relies on the existing infrastructure of the internet—specifically HTTP—it is the most widely used standard for building web services.
Core Concepts of REST
To understand how a REST API works, it helps to look at its fundamental building blocks:
1. Resources and URIs
In REST, everything is considered a resource (a user, a product, an image, a statistical data point). Each resource is identified by a unique address called a URI (Uniform Resource Identifier) or an endpoint.
https://api.example.com/v1/players(Targets the collection of players)https://api.example.com/v1/players/27(Targets a specific player with ID 27)
2. HTTP Methods (The Actions)
REST uses standard HTTP verbs to define what action you want to perform on a resource. This maps perfectly to standard CRUD (Create, Read, Update, Delete) operations:
| HTTP Method | CRUD Action | Description | Example |
|---|---|---|---|
| GET | Read | Retrieves data from the server without modifying it. | Get details of a player. |
| POST | Create | Submits new data to the server to create a resource. | Add a new player to the system. |
| PUT | Update | Replaces an entire existing resource with updated data. | Update all fields of a specific player record. |
| PATCH | Update | Partially updates an existing resource (only changes specific fields). | Update just a single field, like a player's team. |
| DELETE | Delete | Removes a specific resource from the server. | Delete a player record. |
3. Statelessness
This is a critical rule of REST. Stateless means that the server does not store any memory or "context" about past requests from a client.
Every single request sent from a client to a server must contain all the information needed to understand and process it (including authentication credentials, tokens, or parameters). The server treats every request as completely brand new.
4. Data Formats
While REST technically supports multiple formats (like XML, HTML, or plain text), JSON (JavaScript Object Notation) is overwhelmingly the most common format used today because it is lightweight and easy for both humans to read and machines to parse.
How a REST API Request & Response Looks
When a client communicates with a REST API, it sends a request and receives a response back.
The Request
A typical request includes:
- The Endpoint URL
- The HTTP Method (e.g.,
GET) - Headers: Metadata containing information like authentication tokens or the preferred data format (e.g.,
Content-Type: application/json). - Body: The actual data payload (used primarily with
POST,PUT, andPATCH).
The Response
The server processes the request and sends back a response containing:
- HTTP Status Code: A 3-digit number indicating the result of the request.
200 OK: Success.201 Created: Successfully created a new resource.400 Bad Request: The server couldn't understand the request (client error).401 Unauthorized: You need to authenticate first.404 Not Found: The requested resource doesn't exist.500 Internal Server Error: The server crashed or ran into a problem.
- Response Body: The requested data, usually formatted as JSON.
JSON
// Example JSON response for a player data request
{
"id": 27,
"name": "Mike Trout",
"position": "Outfielder",
"status": "Active"
}
Why REST is So Popular
- Simplicity: It uses existing HTTP standards, meaning developers don't need to install complex protocols to start using it.
- Scalability: Because it is stateless, servers don't get bogged down trying to remember client sessions, making it easy to scale applications.
- Separation of Client and Server: The frontend (what the user sees) and the backend (where data lives) are completely independent. You can change your mobile app design entirely without touching the API, or swap out your backend database without changing how the app requests data.