A Simple Guide to REST APIs
Learn what a REST API is, how it works, and how to build one with Node.js and Express. Start building your first REST API today.
Have you ever wondered how apps talk to each other? When you check the weather on your phone or send a message, your app is talking to another computer. This happens through something called an API, and one of the most common types is a REST API.
In this guide, we’ll learn what a REST API is, how it works, and how to create a super simple one with Node.js and Express.
Table of contents
- What is an API?
- What is a REST API?
- How REST APIs work
- Creating a simple REST API (Using Node.js and Express)
- Testing your API
- Conclusion
- Resources
1. What is an API?
An API (Application Programming Interface) is like a waiter in a restaurant. You tell the waiter what you want, and they bring it to you from the kitchen. An API lets one program request information or send instructions to another.
2. What is a REST API?
REST stands for REpresentational State Transfer. It’s a set of rules that lets programs talk to each other using simple HTTP methods like:
- GET – to get data
- POST – to add new data
- PUT – to update data
- DELETE – to delete data
A REST API uses these methods to interact with resources, like a list of books, users, or photos.
3. How REST APIs work
Imagine you have a website about books. You can build a REST API so other apps can:
- Get a list of all books
- Add a new book
- Edit a book
- Delete a book
Each action will be done by calling a specific URL and method.
Example:
GET /books→ Get all booksPOST /books→ Add a bookPUT /books/1→ Edit book with ID 1DELETE /books/1→ Delete book with ID 1
4. Creating a simple REST API (Using Node.js and Express)
Let’s build a tiny REST API for managing books.
Step 1: Set up your project
Make a new folder, then run these commands in your terminal:
npm init -y
npm install express
Step 2: Create index.js with this code:
const express = require('express');
const app = express();
app.use(express.json());
let books = [
{ id: 1, title: 'Harry Potter' },
{ id: 2, title: 'Lord of the Rings' }
];
// Get all books
app.get('/books', (req, res) => {
res.json(books);
});
// Add a book
app.post('/books', (req, res) => {
const newBook = {
id: books.length + 1,
title: req.body.title
};
books.push(newBook);
res.status(201).json(newBook);
});
// Start server
app.listen(3000, () => {
console.log('API is running on http://localhost:3000');
});
5. Testing your API
You can use a tool like Postman or your browser (for GET requests) to test the API. Try going to:
http://localhost:3000/books
You’ll see the list of books!
Conclusion
REST APIs are everywhere. Learning how a REST API works is a great step into web development. Once you understand the basics, you can build apps, websites, or even mobile apps that talk to each other.
Keep it simple, practice a lot, and have fun building!
