RESTful API
A RESTful API is an interface design approach based on the REST architectural style, widely used for exchanging data between web services and systems. REST was introduced by Roy Fielding in his 2000 doctoral dissertation and emphasises statelessness, client-server separation, a uniform interface and cacheability. At its core, a RESTful API performs create, read, update and delete (CRUD) over HTTP.
Characteristics
- Stateless: every request carries all the information it needs, such as authentication; the server keeps no client state between requests.
- Client-server: the client owns the interface and the experience, the server owns storage and business logic, and the two evolve independently.
- Uniform interface: standard HTTP verbs (GET, POST, PUT, DELETE and so on) are used consistently, which makes the API easy to understand and use.
- Cacheable: responses can be marked cacheable, improving performance and reducing server load.
- Layered: intermediaries such as load balancers and proxies improve scalability and security.
- Code on demand: servers may send code or scripts for the client to execute, extending client capability.
HTTP verbs
- GET — read a resource; no side effects.
- POST — create a resource or submit data; the server creates it.
- PUT — update an existing resource, overwriting it with the supplied representation.
- DELETE — remove a resource.
- PATCH — partially update a resource.
URL design
A RESTful API identifies resources with uniform resource identifiers, one unique URL each. Relationships are expressed through URL hierarchy.
Example:
GET /books— list every book.GET /books/{id}— fetch one book by id.POST /books— create a book.PUT /books/{id}— update a book.DELETE /books/{id}— delete a book.
Status codes
- 200 OK — success, with a body.
- 201 Created — the resource was created.
- 204 No Content — success, with no body.
- 400 Bad Request — the request is invalid or malformed.
- 401 Unauthorized — authentication is required.
- 404 Not Found — the resource does not exist.
- 500 Internal Server Error — the server failed.
Example
A small book management API built with Node.js and Express.
Server (Node.js + Express)
const express = require('express');
const app = express();
app.use(express.json());
let books = [
{ id: 1, title: '1984', author: 'George Orwell' },
{ id: 2, title: 'To Kill a Mockingbird', author: 'Harper Lee' }
];
// list every book
app.get('/books', (req, res) => {
res.json(books);
});
// fetch one book
app.get('/books/:id', (req, res) => {
const book = books.find(b => b.id === parseInt(req.params.id));
if (!book) return res.status(404).send('Book not found');
res.json(book);
});
// create a book
app.post('/books', (req, res) => {
const book = {
id: books.length + 1,
title: req.body.title,
author: req.body.author
};
books.push(book);
res.status(201).json(book);
});
// update a book
app.put('/books/:id', (req, res) => {
const book = books.find(b => b.id === parseInt(req.params.id));
if (!book) return res.status(404).send('Book not found');
book.title = req.body.title;
book.author = req.body.author;
res.json(book);
});
// delete a book
app.delete('/books/:id', (req, res) => {
const book = books.find(b => b.id === parseInt(req.params.id));
if (!book) return res.status(404).send('Book not found');
const index = books.indexOf(book);
books.splice(index, 1);
res.status(204).send();
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}...`));Summary
A RESTful API is a simple, flexible and extensible way to design a web service: standard HTTP verbs and the HTTP protocol make client-server communication predictable and efficient. It scales from a small project to a large distributed system.
