Find a course

Module 5: Designing APIs and Beyond RESTLesson 11 of 1214 min

Designing a Clean REST API

Design endpoints other developers can guess: consistent naming, sensible nesting, versioning without breaking clients, and documentation with OpenAPI.

Step 1 of 13Learn

Designing for the developer on the other side

An API's users are developers, and they judge it the way anyone judges a tool: can I guess how it works? A well-designed API lets someone who has used three endpoints predict the fourth.

Take a small task-tracking service. A clean design:

GET    /v1/projects                     list projects
POST   /v1/projects                     create a project
GET    /v1/projects/{projectId}         get one project
PATCH  /v1/projects/{projectId}         update a project
DELETE /v1/projects/{projectId}         delete a project
GET    /v1/projects/{projectId}/tasks   list a project's tasks
POST   /v1/projects/{projectId}/tasks   add a task to a project
GET    /v1/tasks/{taskId}               get one task directly
PATCH  /v1/tasks/{taskId}               update a task

Conventions that make it predictable:

  • Plural nouns for collections, always: /projects, /tasks, never mixing in /project.
  • Lowercase paths, with hyphens for multi-word names: /time-entries.
  • One level of nesting for ownership (/projects/{id}/tasks), and direct top-level access for items that have their own global ID (/tasks/{taskId}).
  • One naming style for JSON fields across the whole API, such as camelCase dueDate everywhere.
  • The same shapes everywhere: every list response wrapped the same way, every error in the same format, every timestamp in ISO 8601 UTC.

Consistency beats cleverness. Even a slightly odd convention, applied everywhere, is easier to use than a perfect one applied half the time.

Step 2 of 13Practice · Spot the mistake

A team drafts endpoints for a recipes API. Click the ones that break the conventions used by the rest of the list.

Step 3 of 13Diagram

A consistent recipes API

A consistent recipes APIHierarchy diagram: /v1, /recipes, /recipes/{id}, /recipes/{id}/ingredients, /recipes/{id}/reviews, /users, /users/{id}, /users/{id}/recipes./v1/recipes/recipes/{id}/recipes/{id}/ingredients/recipes/{id}/reviews/users/users/{id}/users/{id}/recipes

Tap a shape with to see more.

/v1
/recipes

GET lists, POST creates.

/recipes/{id}

GET, PATCH, DELETE one recipe.

/recipes/{id}/ingredients
/recipes/{id}/reviews
/users
/users/{id}
/users/{id}/recipes

The same recipe objects, filtered by author.

Step 4 of 13Learn

Shaping responses

Beyond URLs, the JSON itself needs design decisions.

Wrap lists in an object. Instead of returning a bare array, return an object with the items and the paging information:

{
  "data": [ { "id": "t_81", "title": "Draft brief", "done": false } ],
  "nextCursor": "eyJpZCI6InRfODEifQ",
  "hasMore": true
}

An object can grow new fields later, such as a totalCount, without breaking clients. A bare array can't.

Use clear types. Booleans for yes/no ("done": false, not "done": "no"), numbers for quantities, ISO 8601 strings for timestamps ("2026-03-03T10:15:02Z"), and explicit units in names when ambiguity is possible (durationSeconds, priceCents).

Be careful with null vs. missing. Decide and document whether an empty optional field is sent as null or left out, and do the same thing everywhere.

Return the resource after changes. Responding to POST or PATCH with the full, updated resource saves the client a follow-up GET and shows server-generated values such as IDs and timestamps.

Use opaque, stable IDs. Many APIs use strings such as "t_81" or UUIDs rather than sequential integers. Sequential IDs leak information (how many orders a shop has) and make it easy to guess other records' URLs, which raises the stakes if an authorization check is ever missing.

Step 5 of 13Practice · Select all that apply

Which response design choices make an API easier to evolve and use? Select all that apply.

Select all that apply.

Step 6 of 13Learn

Versioning without breaking clients

Once other people's code depends on your API, changing it is risky. The key distinction:

Non-breaking (additive) changes are safe:

  • adding a new endpoint;
  • adding an optional request parameter;
  • adding a new field to a response (well-written clients ignore fields they don't know).

Breaking changes can crash existing clients:

  • removing or renaming a field or endpoint;
  • changing a field's type, such as "id": 42 becoming "id": "42";
  • making an optional parameter required;
  • changing what a status code or an error means.

When a breaking change is unavoidable, you release a new version and keep the old one running for a while. Common approaches:

  • In the path: /v1/orders and /v2/orders. Most common and easiest to see.
  • In a header: Accept: application/vnd.example.v2+json or a custom API-Version: 2026-03-01 header. Keeps URLs clean; some large providers use date-based versions this way.
  • In the query string: /orders?version=2. Less common.

Whatever the method, announce changes early, document a deprecation timeline (for example, "v1 will be retired in 12 months"), and consider sending a Deprecation or Sunset header so client developers notice. Clients, in turn, should ignore unknown fields rather than failing on them; this simple habit makes additive changes truly safe.

Step 7 of 13Practice · Sort into groups

Classify each change to a public API.

Tap an item, then tap its group (or drag it there).

Step 8 of 13Diagram

Retiring an old API version politely

Retiring an old API version politelyProcess diagram: Launch v2 alongside v1 → Announce deprecation → Migration period → Watch remaining v1 traffic → Retire v1.Launch v2 alongsidev1AnnouncedeprecationMigration periodWatch remaining v1trafficRetire v1

Tap a shape with to see more.

Launch v2 alongside v1

Both run in parallel; nothing breaks on day one.

Announce deprecation

Changelog, emails, and Deprecation/Sunset headers with a date.

Migration period

Often 6 to 12 months or more for public APIs, with a guide.

Watch remaining v1 traffic

Contact the heaviest remaining users directly.

Retire v1

Return 410 Gone with a link to the migration guide.

Step 9 of 13Learn

Documentation and the OpenAPI standard

An API without documentation might as well not exist. Good docs include, for every endpoint: the method and path, a description, parameters with types and whether they're required, a sample request, sample responses for success and errors, and the authentication needed. Plus general pages on authentication, rate limits, pagination, errors and versioning.

Writing all that by hand and keeping it in sync with the code is hard. That's why most teams describe their APIs with the OpenAPI Specification (formerly known as Swagger): a standard, machine-readable YAML or JSON file that describes every endpoint.

paths:
  /tasks/{taskId}:
    get:
      summary: Get one task
      parameters:
        - name: taskId
          in: path
          required: true
          schema: { type: string }
      responses:
        "200":
          description: The task
        "404":
          description: No task with this ID

From one OpenAPI file, tools can generate interactive documentation where developers try requests in the browser, client libraries in many languages, server stubs, mock servers and automated tests. The data shapes inside are described with JSON Schema, which can also validate incoming requests.

Some teams write the OpenAPI file first and build to it (design-first); others generate it from annotated code (code-first). Design-first makes it easier to review an API with its future users before any code is written.

Step 10 of 13Practice · Choose one

What is the main advantage of describing an API in an OpenAPI file rather than only in hand-written web pages?

Step 11 of 13Diagram

One OpenAPI file, many outputs

One OpenAPI file, many outputsParts diagram: openapi.yaml, Interactive docs, Client libraries, Mock server, Request validation, Contract tests.openapi.yamlInteractivedocsClientlibrariesMock serverRequestvalidationContracttests

Tap a shape with to see more.

openapi.yaml
Interactive docs

Pages with a "try it" button for every endpoint.

Client libraries

Generated code for many languages.

Mock server

Front-end teams can build before the backend is finished.

Request validation

The server rejects requests that break the schema.

Contract tests

Catch accidental breaking changes in CI.

Step 12 of 13Note

Step 13 of 13Practice · True or false

Adding a new optional field to a JSON response is generally a non-breaking change.