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 / Advanced

Versioning and caching

Once other people depend on your API, two new problems appear: how to change it without breaking them, and how to serve them fast without doing the same work over and over. Versioning answers the first; HTTP caching the second.

What breaks clients?

Safe (non-breaking) changes Breaking changes
Adding a new endpoint Removing or renaming an endpoint
Adding an optional request field Making an optional field required
Adding a field to a response Removing or renaming a response field
Adding a new enum value (if clients ignore unknown ones) Changing a field's type ("42" → 42)
Accepting more input formats Changing status codes or error formats

Most API evolution can be done with safe changes only: add, don't change or remove. Clients should help by ignoring fields they don't recognise (the robustness principle).

Versioning strategies

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

Strategy Example Notes
URL path /v1/products, /v2/products The most common: visible, easy to route and test
Header Accept: application/vnd.myshop.v2+json or API-Version: 2 Clean URLs, harder to try in a browser
Query parameter /products?version=2 Easy, but mixes versioning with filters
Date-based Stripe-Version: 2026-10-28 Each account is pinned to the version it started with
Node.js · editable

A deprecation process

  1. Announce the change and the timeline (changelog, email, docs).
  2. Add Deprecation and Sunset headers to the old version's responses.
  3. Measure: log which clients still call the old version and contact them.
  4. Remove it only after the sunset date, and when traffic has dropped.

HTTP caching

The fastest request is one you never have to process. HTTP has a powerful, standard caching system: browsers, CDNs and proxies all understand it.

Cache-Control

The server says how a response may be cached:

Header Meaning
Cache-Control: max-age=3600 Anyone may reuse it for an hour
Cache-Control: public, max-age=86400 Shared caches (CDNs) may store it too
Cache-Control: private, max-age=60 Only the user's own browser may store it (personal data)
Cache-Control: no-cache Store it, but check with the server before each reuse
Cache-Control: no-store Never store it (bank statements, tokens)
Cache-Control: max-age=0, s-maxage=300 Browsers revalidate; the CDN keeps it for 5 minutes

Static files with a content hash in their name (app.3f9a2c.js) can be cached for a year (max-age=31536000, immutable). API responses usually need shorter times or revalidation.

ETags and conditional requests

An ETag is a fingerprint of a response. The client remembers it and sends it back in If-None-Match. If the data hasn't changed, the server answers 304 Not Modified with no body, saving bandwidth and parsing. Express generates ETags for you automatically:

Node.js · editable

Browsers do all of this automatically. With fetch in Node you add the If-None-Match header yourself, plus cache: 'no-cache': without it, fetch treats a request with a conditional header as a forced reload and asks the server for a fresh copy.

Optimistic concurrency with If-Match

ETags also prevent lost updates: two people load the same document, both edit it, and the second save silently overwrites the first. With If-Match, an update only succeeds if the resource is still the version the client saw:

Node.js · editable

412 Precondition Failed tells Rahul his copy is stale, instead of silently destroying Asha's work.

Server-side caching

HTTP caching happens in browsers and CDNs. Inside your backend you can also cache expensive results (popular product lists, rendered pages, third-party API answers) in memory or in Redis, with an expiry time. The hard part is invalidation: delete or update the cached value whenever the underlying data changes. (The Redis track covers caching patterns.)

Practice

  1. Add Cache-Control: public, max-age=300 to a product list endpoint and explain who may cache it.
  2. In the ETag example, change the description instead of the price and check that the third request returns 200 again.
  3. Add header-based versioning: GET /products returns the v2 shape when the request has API-Version: 2, and the v1 shape otherwise.
✎ Check your understanding

1.Which of these changes is NOT breaking for existing clients?

2.What does a 304 Not Modified response contain?

3.What does a 412 Precondition Failed response to a PUT with If-Match mean?

← GraphQL basics