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

What is an API?

An API (Application Programming Interface) is a contract that lets one program use another. When a weather app shows the forecast, a shop takes a card payment, or you "Sign in with Google", one program is calling another program's API.

The restaurant analogy

You (the client) don't walk into the kitchen. You read the menu (the API documentation), tell the waiter what you want in a standard way (a request), and get your food back (a response). The kitchen (the server) can change its recipes, staff and equipment; as long as the menu stays the same, you never notice.

That's the core idea: an API hides how something works and exposes what it can do.

Kinds of APIs

Kind Example
Web APIs (this track) GET https://api.example.com/products/42 over HTTP
Library APIs Math.round(2.5), fs.readFile(): functions a library offers
Operating system APIs Opening files, starting processes
Browser APIs fetch, localStorage, the DOM

Web APIs are how frontends talk to backends, how mobile apps get their data, and how companies connect their systems (payments with Stripe, messages with Twilio, maps with Google). Most use HTTP and JSON.

A request and a response

Every call to a web API is an HTTP request answered by a response:

GET /products/42 HTTP/1.1            ← method and path
Host: api.myshop.example             ← headers
Accept: application/json

HTTP/1.1 200 OK                      ← status code
Content-Type: application/json       ← headers

{"id":42,"name":"Mug","price":8.99}  ← body

Let's build a tiny API and call it, exactly as a frontend would. (The editors in this track run a Node.js server in your browser; fetch calls it over a simulated network.)

Node.js · editable

The client doesn't know (or care) that the server keeps products in an object. Tomorrow it could use PostgreSQL; the API would look exactly the same.

Parts of a web API

Part Example Purpose
Base URL https://api.myshop.example/v1 Where the API lives
Endpoint /products/42 A specific resource or action
Method GET, POST, PUT, PATCH, DELETE What to do
Headers Authorization: Bearer …, Content-Type Metadata: who you are, what format
Query parameters ?category=kitchen&page=2 Filtering, sorting, paging
Body {"name": "Mug"} Data sent with POST/PUT/PATCH
Status code 200, 201, 404, 500 How it went

Calling a public API

From your own computer, you can call any public API with fetch (in Node or the browser) or with curl in a terminal:

bash
curl https://api.github.com/users/octocat
curl -i https://api.github.com/users/octocat     # -i also shows status and headers
js
const res = await fetch('https://api.github.com/users/octocat');
const user = await res.json();
console.log(user.name, user.public_repos);

Tools such as Postman, Insomnia, Bruno or VS Code's REST Client let you explore APIs with a friendly interface.

Styles of web API

Style Idea Used for
REST Resources at URLs, manipulated with HTTP methods Most public and internal APIs
GraphQL One endpoint; the client asks for exactly the fields it needs Complex frontends (GitHub, Shopify)
gRPC Binary messages over HTTP/2, generated client code Fast service-to-service calls
Webhooks The server calls you when something happens Payment notifications, Git pushes
WebSockets / SSE A long-lived connection for live updates Chat, live dashboards, AI streaming

This track focuses on REST and GraphQL, and covers webhooks and real-time APIs at the end.

Practice

  1. Add a GET /products endpoint that returns all products as an array, and call it.
  2. Add a query parameter: GET /products?maxPrice=10 should only return products up to that price.
  3. Print every header of a response with for (const [name, value] of response.headers) console.log(name, value).
✎ Check your understanding

1.What does an API hide from its users?

2.In GET /products/42?currency=EUR, what is currency=EUR?

3.Which part of a response tells you whether the request succeeded?