Module 7 · Lesson 3

Backend APIs

Build server endpoints, route requests to handlers, and respond with JSON.

Audio: Backend APIs
0:000:00

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/:id route that removes a user from an in-memory list
  • Return 400 Bad Request if a POST /users body is missing the name field
  • Add a query-string filter so GET /users?role=admin returns only matching users

Code Playground

Edit the code below and click Run to see the output. Switch between languages using the tabs.

Loading editor...

Enjoying the lesson? Unlock the full Full-Stack Web Development from $4.99/mo.

See plans →