Student Senior BlogBlogs

How to Create a Simple API Using Node.js

Sahil Verma
September 26, 2025
3 min read
#nodejs#expressjs#api#restapi#backend#javascript#webdevelopment#coding#tutorial#beginners
Banner for 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:

bash
1mkdir book-api 2cd book-api 3npm init -y

๐Ÿ“ฆ Step 2: Install Express

Express is a popular framework for building APIs with Node.js.

bash
1npm install express

๐Ÿ“ Step 3: Create the API

Create a file server.js and add the following code:

js
1const 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:

bash
1node server.js

You should see:

๐Ÿ“š Book API running on http://localhost:4000

๐Ÿ“Œ Step 5: Test the API

1. Get all books

bash
1curl http://localhost:4000/books

2. Get a book by ID

bash
1curl http://localhost:4000/books/1

3. Add a new book

bash
1curl -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

bash
1curl -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.