Backend APIs
A backend API is a program that listens for HTTP requests and returns data, usually as JSON. Behind every web app and mobile app is a backend doing the real work — saving records, running business logic, talking to databases.
Endpoints and Routing
An endpoint is the combination of an HTTP method and a path: GET /users, POST /users, DELETE /users/42. The server's job is to look at each incoming request, match it to a handler function, run that function, and send the result back. The mapping from (method, path) to handler is called routing. Real frameworks (Express, Flask, Spring) provide elegant routing APIs, but underneath they all do what the example code does: a dictionary lookup.
JSON: The Wire Format
Modern APIs almost always speak JSON (JavaScript Object Notation). JSON has four primitive types (string, number, boolean, null) and two collection types (object and array). Every mainstream language has a built-in or one-line library to parse and serialize it. The Content-Type: application/json header tells the client what to expect.
Path Parameters and Query Strings
Real APIs need dynamic paths. GET /users/:id extracts the user ID from the URL. Query strings like ?page=2&limit=20 carry optional parameters. A good router parses both into a structured object so handlers can use req.params.id or req.query.page directly.
Status Codes Are Part of the API
Returning the right status code is just as important as returning the right body. A POST that creates a row should return 201 Created. A request for a missing record should return 404 Not Found. A failure on the server's side is 500, while bad input from the client is 400. Clients use these codes to decide whether to retry, show an error, or update the UI.
Try It Yourself
- Add a
DELETE /users/:idroute that removes a user from an in-memory list - Return
400 Bad Requestif aPOST /usersbody is missing thenamefield - Add a query-string filter so
GET /users?role=adminreturns only matching users