APIs and Contracts
An overview of how backend APIs are designed, structured, and maintained through clear contracts.
What an API Actually Is
An API (Application Programming Interface) is the interface through which clients communicate with backend systems and access system functionality.
- APIs expose system functionality so external clients can interact with a backend.
- Clients such as web apps, mobile apps, or other services send requests to APIs.
- APIs define the structure of requests and the format of responses returned by the server.
Details
In modern software systems, applications rarely interact with backend logic directly. Instead, they communicate through APIs, which act as controlled entry points into the system.
A client — such as a browser, mobile application, or another backend service — sends a request to a specific API endpoint. The API receives that request, interprets the information it contains, and forwards it to the appropriate backend logic. The backend then performs the necessary work, such as retrieving data from a database or executing application rules, before returning a response back to the client.
This structured communication allows systems to remain modular. Frontend applications, mobile apps, and external services can interact with the same backend functionality as long as they follow the API's defined request and response format.
Because APIs define how requests must be structured and what responses will look like, they create a predictable interface between different parts of a system. This interface allows independently developed components to communicate reliably without needing to know how the internal backend implementation works.
HTTP Methods
HTTP methods define the type of operation a client wants to perform when interacting with an API endpoint.
- HTTP methods communicate the intended action of a request.
- Different methods correspond to different operations on resources.
- APIs use HTTP methods together with endpoints to define system behavior.
Details
When a client sends a request to an API endpoint, it must specify what type of operation it wants to perform. This operation is expressed using an HTTP method.
The HTTP method works together with the endpoint URL to define the meaning of the request.
For example, a request like GET /users asks the server to retrieve user data. A request like POST /users asks the server to create a new user. A request such as DELETE /users/42 instructs the backend to remove a specific user.
Several HTTP methods are commonly used in APIs.
GET retrieves existing data from the server.
POST creates new resources.
PUT replaces an existing resource with new data.
PATCH updates part of an existing resource.
DELETE removes a resource from the system.
By combining endpoints with HTTP methods, APIs clearly define the operations that clients are allowed to perform on system resources.
REST and Resource-Based APIs
REST (Representational State Transfer) is a common API design approach where systems expose resources instead of actions.
- REST APIs organize endpoints around resources such as users, orders, or products.
- Operations are defined using HTTP methods instead of action-based URLs.
- Resource-based design leads to more consistent and predictable APIs.
Details
In API design, one approach is to structure endpoints around actions, where each URL describes what the system should do. For example, an endpoint like POST /createUser directly encodes the action into the URL.
This approach works for small systems but becomes difficult to maintain as the number of features grows. Endpoints become inconsistent, and it becomes harder to predict how new functionality will be exposed.
REST takes a different approach by organizing APIs around resources such as users, orders, or products. Instead of encoding actions in the URL, the endpoint represents the resource, and the HTTP method defines the operation.
For example, POST /users creates a new user, while GET /users retrieves users. This separation leads to a consistent pattern across the entire API.
Because of this structure, RESTful APIs are easier to understand, scale, and extend. Developers can interact with new endpoints more easily since they follow the same predictable design.
REST Principles
REST APIs follow a set of principles that make systems predictable, scalable, and easier to maintain.
- Requests are stateless, meaning each request contains all necessary information.
- Resources are identified using consistent and structured URLs.
- HTTP methods are used correctly to represent operations.
- APIs follow a uniform and predictable interface design.
Details
REST is built on a set of design principles that help APIs remain consistent and reliable as systems grow.
One key principle is stateless communication. Each request must contain all the information needed for the server to process it. The server does not rely on previous requests, which makes the system easier to scale and distribute.
Another principle is resource-based design. APIs represent entities such as users or orders as resources, and these resources are identified using URLs like /users or /orders.
REST also relies on standard HTTP semantics. HTTP methods such as GET, POST, PUT, PATCH, and DELETE are used to clearly express the operation being performed on a resource.
Finally, REST enforces a uniform interface. Endpoints follow consistent patterns, making APIs easier to understand and reducing confusion when interacting with different parts of a system.
Status Codes
HTTP status codes allow servers to communicate the outcome of a request in a standardized and predictable way.
- Status codes indicate whether a request succeeded, failed due to client input, or failed due to server issues.
- They are grouped into categories based on their first digit.
- Clients rely on status codes to decide how to handle responses.
Details
When a server processes a request, it must communicate the result back to the client. HTTP status codes provide a standardized way to describe that outcome.
Status codes are grouped into categories. Codes in the 2xx range indicate success, meaning the request was processed correctly. Codes in the 4xx range indicate client errors, such as invalid input or missing resources. Codes in the 5xx range indicate server errors, meaning something went wrong on the backend.
Common examples include 200 OK, which indicates a successful request, and 201 Created, which confirms that a new resource was created. Errors such as 400 Bad Request indicate invalid input, while 401 Unauthorized signals missing or invalid authentication. A 404 Not Found response means the requested resource does not exist, and 500 Internal Server Error indicates a failure on the server.
Clients use these status codes to determine how to proceed. For example, a client may retry a request after a server error or prompt a user to correct input after a client error.
Idempotency
An operation is idempotent if repeating the same request produces the same result without causing additional side effects.
- Idempotent operations can be safely repeated without changing the outcome.
- PUT and DELETE are typically idempotent, while POST is usually not.
- Idempotency is critical for handling retries and network failures.
Details
In distributed systems, requests can fail, time out, or be retried multiple times. Because of this, backend systems must be designed to handle repeated requests safely.
An operation is considered idempotent if sending the same request multiple times results in the same final state. For example, calling DELETE /users/10 once removes the user. Calling it again does not change anything further because the user is already deleted.
Similarly, PUT /users/10 replaces the resource with the same data each time, so repeating the request does not create additional side effects.
In contrast, POST /orders is typically not idempotent because each request creates a new order. Repeating the same request can result in duplicate resources.
Idempotency is important because systems often retry requests automatically when failures occur. Without idempotency, retries could lead to inconsistent data, duplicated operations, or unintended side effects.
API Versioning
API versioning allows backend systems to evolve without breaking existing clients by maintaining multiple versions of an API.
- Changes to APIs can break clients that depend on older behavior.
- Versioning allows multiple API versions to coexist safely.
- Clients can choose which version of the API to interact with.
Details
As backend systems evolve, APIs often need to change. New features may be added, data formats may be updated, or existing behavior may be modified.
The problem is that clients such as web apps, mobile apps, or third-party integrations rely on existing API behavior. If the API changes without warning, those clients can break.
API versioning solves this by allowing multiple versions of an API to exist at the same time. Older clients can continue using the original version, while newer clients can adopt updated versions.
One common approach is URL versioning, where the version is included in the path, such as /v1/users and /v2/users. Another approach is header versioning, where the client specifies the version using a header like Accept: application/vnd.api.v2.
By versioning APIs, backend systems can evolve safely while maintaining compatibility with existing clients.
The API Contract
An API contract defines how clients and backend systems communicate, ensuring requests and responses follow a predictable and reliable structure.
📍 Endpoint
The entry point defined in the contract.
- The contract defines available endpoints and how they are accessed.
- It specifies the structure of requests and responses.
- Status codes and behavior expectations are clearly defined.
Details
An API contract acts as an agreement between the client and the backend system. It defines exactly how communication should happen so both sides can interact without ambiguity.
When a client sends an HTTP request, it must follow the contract by using the correct endpoint, providing the expected data format, and including all required information. The backend system processes the request based on these rules and returns a structured HTTP response.
The contract defines several critical aspects of the API, including the available endpoints, the format of request data, the structure of responses, the meaning of status codes, and the expected behavior of each operation.
Because of this contract, clients do not need to understand how the backend is implemented internally. As long as they follow the defined interface, communication remains consistent and reliable.
In practice, API contracts are essential for collaboration between frontend and backend teams, as well as for integrations between independent systems.
Question Section
1 / 5
This track is locked
Buy this track once to unlock all of its lessons.