What Is an API and REST Basics
Overview
Every app, website and service talks to other software through an API. REST is the most common style for building one on top of HTTP. In this lesson you will learn the rules that make a REST API predictable and easy to use.
Detailed Notes
What is an API
An API (Application Programming Interface) is an agreed way for one piece of software to ask another to do something. The caller does not need to know how the other side works inside; it only needs to know what to ask and what it will get back.
Think of a restaurant menu. You pick a dish and the kitchen prepares it. You never see the kitchen. The menu is the API.
REST (Representational State Transfer) is a style of API built on HTTP, which you met in the Network Essentials chapter. In REST, everything is a resource (a user, an order, a product) identified by a URL, and you act on it with standard HTTP methods.
The problem it solves
Without a shared convention, every team invents its own URLs, verbs and error formats. Clients then need custom code for each service. REST reuses what HTTP already gives us, so any developer or tool can guess how an API behaves.
How it works
Resources and URLs. Use nouns, not verbs, and plural names:
GET /users- the list of usersGET /users/42- user 42GET /users/42/orders- orders belonging to user 42
HTTP methods say what to do with the resource:
- GET - read it. Never changes data.
- POST - create a new one (the server picks the ID).
- PUT - replace a resource fully.
- PATCH - change part of a resource.
- DELETE - remove it.
Status codes say how it went:
- 2xx success: 200 OK, 201 Created, 204 No Content.
- 3xx redirect: 301 Moved Permanently.
- 4xx client error: 400 Bad Request, 401 Unauthorized (not logged in), 403 Forbidden (not allowed), 404 Not Found, 429 Too Many Requests.
- 5xx server error: 500 Internal Server Error, 503 Service Unavailable.
Idempotent means doing it once or many times leaves the same result. GET, PUT and DELETE are idempotent: deleting user 42 five times still leaves user 42 deleted. POST is not: sending it twice can create two orders. This matters because networks fail. If a client times out, it does not know whether the request worked, so it wants to safely retry. For payments, clients send an idempotency key (a unique ID per attempt) so the server can spot a repeat and ignore it.
Pagination. Never return a million rows at once. Return one page at a time:
- Offset:
GET /users?limit=20&offset=40. Simple, but slow on huge tables and can skip or repeat items when data changes. - Cursor:
GET /users?limit=20&after=abc123. The server returns a marker for where you stopped. Faster and stable, so it is preferred for big or fast-changing lists.
Versioning. Once clients depend on your API, you cannot change it freely. Put a version in the URL (/v1/users) or a header, and keep old versions running until clients move. Adding new optional fields is safe; removing or renaming fields is a breaking change that needs a new version.
REST is also stateless: each request carries everything the server needs (such as a token), so any server can handle it. That makes it easy to add more servers behind one address.
A simple example
Creating and reading an order:
POST /v1/orders
{ "item": "book", "qty": 1 }
201 Created
Location: /v1/orders/981
GET /v1/orders/981
200 OK
{ "id": 981, "item": "book", "qty": 1, "status": "placed" }
Asking for an order that does not exist returns 404 Not Found, and a missing field returns 400 Bad Request.
System design example
A mobile shopping app calls GET /v1/products?limit=20&after=... to browse and POST /v1/orders with an idempotency key to buy. Because the API is stateless, you can run many identical servers and spread requests across them. Because it is versioned, you can ship /v2 for new app releases while old phones keep working on /v1.
Summary
- An API is a contract: ask this way, get that back.
- REST uses nouns in URLs, HTTP methods for actions and status codes for results.
- GET, PUT and DELETE are idempotent; POST is not, so use idempotency keys for safe retries.
- Paginate lists; cursors scale better than offsets.
- Version your API so you can change it without breaking existing clients.
Architecture Diagram
The client sends a method and URL; the server answers with a status code and data.
- 1The client sends an HTTP method and a resource URL.
- 2The server runs the request without remembering earlier ones.
- 3It replies with a status code such as 200 or 404.
- 4The body carries the resource data, usually JSON.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

