How to Create a Simple API Using Node.js

APIs (Application Programming Interfaces) are the backbone of modern applications. From mobile apps to web platforms, they all rely on APIs to fetch and send data.
In this blog, weโll learn how to build a simple REST API using Node.js and Express. To make it purposeful, our API will manage a Book Collection ๐.
๐ What Weโll Build
Weโll create an API that allows you to:
- Get all books โ
GET /books
- Get a single book by ID โ
GET /books/:id
- Add a new book โ
POST /books
- Delete a book โ
DELETE /books/:id
๐ Step 1: Setup Project
Open your terminal and create a new folder:
bash1mkdir book-api 2cd book-api 3npm init -y
๐ฆ Step 2: Install Express
Express is a popular framework for building APIs with Node.js.
bash1npm install express
๐ Step 3: Create the API
Create a file server.js
and add the following code:
js1const express = require("express"); 2const app = express(); 3const PORT = 4000; 4 5// Middleware to parse JSON 6app.use(express.json()); 7 8// Temporary in-memory "database" 9let books = [ 10 { id: 1, title: "The Pragmatic Programmer", author: "Andrew Hunt" }, 11 { id: 2, title: "Clean Code", author: "Robert C. Martin" }, 12]; 13 14// GET all books 15app.get("/books", (req, res) => { 16 res.json(books); 17}); 18 19// GET a single book by ID 20app.get("/books/:id", (req, res) => { 21 const book = books.find(b => b.id === parseInt(req.params.id)); 22 if (!book) return res.status(404).json({ message: "Book not found" }); 23 res.json(book); 24}); 25 26// POST a new book 27app.post("/books", (req, res) => { 28 const newBook = { 29 id: books.length + 1, 30 title: req.body.title, 31 author: req.body.author, 32 }; 33 books.push(newBook); 34 res.status(201).json(newBook); 35}); 36 37// DELETE a book 38app.delete("/books/:id", (req, res) => { 39 const bookIndex = books.findIndex(b => b.id === parseInt(req.params.id)); 40 if (bookIndex === -1) return res.status(404).json({ message: "Book not found" }); 41 42 const deletedBook = books.splice(bookIndex, 1); 43 res.json(deletedBook[0]); 44}); 45 46// Start the server 47app.listen(PORT, () => { 48 console.log(`๐ Book API running on http://localhost:${PORT}`); 49});
โถ Step 4: Run the API
Start your server with:
bash1node server.js
You should see:
๐ Book API running on http://localhost:4000
๐ Step 5: Test the API
1. Get all books
bash1curl http://localhost:4000/books
2. Get a book by ID
bash1curl http://localhost:4000/books/1
3. Add a new book
bash1curl -X POST http://localhost:4000/books \ 2-H "Content-Type: application/json" \ 3-d '{"title": "JavaScript: The Good Parts", "author": "Douglas Crockford"}'
4. Delete a book
bash1curl -X DELETE http://localhost:4000/books/1
๐ฏ Why This Matters
With just a few lines of code, youโve built a working REST API that:
- Handles different HTTP methods (GET, POST, DELETE)
- Uses routes with parameters
- Returns JSON responses
This is the foundation of backend development. You can now:
- Connect it to a database (MongoDB, MySQL, PostgreSQL)
- Add authentication (JWT)
- And much more
โ Final Thoughts
APIs are everywhere โ powering your favorite apps, social media platforms, and even IoT devices. By building this simple API, youโve taken your first step into backend development with Node.js.
๐ Next challenge: extend this Book API with update functionality (PUT /books/:id
) and connect it to a real database.