No history yet

API Request Lifecycle

The Request-Response Conversation

At its core, an API interaction is a structured conversation. A client sends a request to a server, and the server sends back a response. Think of it like ordering at a restaurant: you (the client) make a specific request from the menu, and the kitchen (the server) prepares and returns your order. This entire exchange, from the moment you ask to the moment you get your food, is the request-response cycle.

Lesson image

APIs operate through a request response cycle between a client and a server

For this conversation to work, both sides need to understand a shared language. In the world of web APIs, this language is the Hypertext Transfer Protocol, or HTTP for short. It defines the rules of engagement: how a request should be structured, what actions can be performed, and how the server should report the outcome.

Actions and Intentions: HTTP Verbs

When you make a request, you're not just asking for something; you're stating an intention. Do you want to retrieve information, create something new, update an existing item, or delete it? HTTP uses specific verbs, or methods, to communicate this intent. The four most common verbs correspond directly to the fundamental database operations known as CRUD: Create, Read, Update, and Delete.

HTTP VerbCRUD OperationPurpose
GETReadRetrieve a resource or a collection of resources.
POSTCreateCreate a new resource.
PUTUpdateReplace an existing resource with new data.
DELETEDeleteRemove a specific resource.

Using these verbs makes an API predictable and easy to understand. When a developer sees GET /users/123, they immediately know the goal is to retrieve information about the user with ID 123. If they see DELETE /users/123, the intent is just as clear.

Anatomy of a Request

An API request is more than just a verb and a URL. It's a complete package of information the server needs to fulfill the request. Let's break down its components.

A request consists of a method, an endpoint (URL), headers, and sometimes a body.

The endpoint is the specific URL you're sending the request to. It's composed of a base URL (like https://api.example.com) and a path that identifies the resource (/users). Sometimes, the path includes a specific identifier, called a path parameter, to target a single item.

GET /users/42

Here, 42 is a path parameter that tells the server we want the user with that specific ID.

For filtering or sorting, we use query parameters. These are key-value pairs added to the end of the URL after a question mark (?).

GET /users?role=admin&sort=lastName

This request asks for all users with the role of admin, sorted by their last name.

Headers are like the envelope for your request. They contain metadata, which is data about the request itself. This includes things like authentication credentials (API keys or tokens), the format of the data you're sending (like Content-Type: application/json), and the data formats you'll accept in return.

GET /api/v1/articles?page=2 HTTP/1.1
Host: example.com
Authorization: Bearer <your_api_token>
Accept: application/json

Finally, the body contains the actual data you want to send to the server. It's typically used with POST and PUT requests. For instance, when creating a new user with a POST request, the user's details (name, email) would be in the body, usually formatted in JSON (JavaScript Object Notation). GET and DELETE requests almost never have a body.

The Server's Reply: Status Codes

After the server processes the request, it sends back a response. The most critical part of this response is the status code, a three-digit number that quickly tells you the outcome. You've probably seen 404 Not Found before. These codes are grouped into classes.

Code RangeClassMeaning
200-299SuccessThe request was successfully received, understood, and accepted.
300-399RedirectionFurther action needs to be taken to complete the request.
400-499Client ErrorThe request contains bad syntax or cannot be fulfilled.
500-599Server ErrorThe server failed to fulfill an apparently valid request.

Knowing these ranges is essential for handling API responses. A 2xx code means your operation worked. A 4xx code means you did something wrong—maybe you forgot an API key (401 Unauthorized), requested a non-existent resource (404 Not Found), or sent invalid data (400 Bad Request). A 5xx code indicates a problem on the server's end, and there's nothing you can do but wait or report the issue.

This whole loop—client request, server processing, and server response—is the fundamental rhythm of the web. Understanding each component allows you to build and interact with APIs effectively, diagnose problems, and create seamless digital experiences.