Student Senior BlogBlogs

How to Create a Simple API Using Node.js

Sahil Verma
September 26, 2025
3 min read
Banner for How to Create a Simple API Using Node.js

AI Summary

Tap Generate and let AI do the magic

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 booksGET /books
  • Get a single book by IDGET /books/:id
  • Add a new bookPOST /books
  • Delete a bookDELETE /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.

#nodejs#expressjs#api#restapi#backend#javascript#webdevelopment#coding#tutorial#beginners
How to Create a Simple API Using Node.js | Student Senior Blogs