Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

API
APIs: REST & GraphQL
0/11 completed
Beginner
What is an API?HTTP methods, status codes and headersJSON
Intermediate
REST design principlesErrors and validationPagination, filtering and sortingGraphQL basics
Advanced
Versioning and cachingRate limiting and idempotencyOpenAPI documentation
Pro
Webhooks, real-time APIs and API security
Tracks / APIs: REST & GraphQL / Intermediate

REST design principles

REST (Representational State Transfer) is the most common style of web API. Its core idea is simple: everything is a resource with a URL, and you use the standard HTTP methods to work with it. A well-designed REST API feels obvious: once you've seen one endpoint, you can guess the rest.

Resources and URLs

Think in nouns (things), not verbs (actions). Use plural names for collections:

Request Meaning
GET /products List products
POST /products Create a product
GET /products/42 Get product 42
PATCH /products/42 Update product 42
PUT /products/42 Replace product 42
DELETE /products/42 Delete product 42

The method says what to do; the URL says what to do it to. Compare:

✖ Verb-style ✔ REST
POST /createProduct POST /products
GET /getProduct?id=42 GET /products/42
POST /deleteProduct/42 DELETE /products/42
GET /products/42/delete DELETE /products/42

Relationships

Nest a resource under its parent when it can't exist without it, but keep nesting shallow (one level is usually enough):

GET  /customers/7/orders          orders of customer 7
POST /customers/7/orders          create an order for customer 7
GET  /orders/501                  an order by its own id (not /customers/7/orders/501)
GET  /orders/501/items            the items of order 501

Actions that aren't CRUD

Some operations are genuinely actions: cancelling an order, resending an email, publishing a draft. Two good options:

POST /orders/501/cancel           an action sub-resource (pragmatic and clear)
POST /orders/501/cancellations    model the action as a resource you create
PATCH /orders/501 {"status": "cancelled"}   if it's just a field change

URL style

  • Lowercase, words separated by hyphens: /order-items, not /OrderItems or /order_items.
  • No file extensions (/products.json) and no trailing slashes.
  • IDs in the path, filters in the query string: /products?category=kitchen&sort=-price.

A complete resource

Here's a full REST resource for tasks, following the conventions above:

Node.js · editable

Notice the consistency: every success wraps its result in data, every error has the same error shape, creation returns 201 with a Location, deletion returns 204.

The REST constraints

REST was described by Roy Fielding in 2000. Its key constraints, in practical terms:

Constraint In practice
Client–server Frontend and backend evolve independently
Stateless Each request carries everything needed (e.g. its auth token); the server keeps no per-client conversation state between requests, so any server instance can handle any request
Cacheable Responses say whether they can be cached (Cache-Control, ETag)
Uniform interface Resources identified by URLs, manipulated with standard methods, self-describing messages
Layered system Load balancers, caches and gateways can sit in between, invisibly

Linking resources

Responses can include links to related resources and next steps, so clients don't have to build URLs themselves. Full "HATEOAS" is rare in practice, but links for pagination and related items are very common and useful:

json
{
  "data": { "id": 501, "status": "paid", "customerId": 7 },
  "links": {
    "self": "/orders/501",
    "customer": "/customers/7",
    "items": "/orders/501/items",
    "cancel": "/orders/501/cancel"
  }
}

Designing for the client

  • Design the API before writing code: list the resources, the endpoints and example requests and responses, and review them with the people who'll use it.
  • Return what the client needs in one request: an order with its items, rather than forcing ten follow-up calls (or support ?include=items).
  • Be consistent everywhere: naming, date formats, error format, pagination. Consistency matters more than any single choice.
  • Don't expose your database: the API is a contract; table structure is an implementation detail that will change.

Practice

  1. Design the endpoints (method + URL) for a blog: posts, comments on posts, publishing a draft, and listing a user's posts.
  2. Add PUT /tasks/:id to the example, which requires both title and status.
  3. Add a links object with self to each task in the responses.
✎ Check your understanding

1.Which is the RESTful way to delete review 9 of product 42?

2.What does "stateless" mean in REST?

3.Where should filters like status=done go?

← JSON