๐Ÿš€ How to Use My Node.js API with Swagger: A Beginner's Guide with Metaphors, Examples & Project Ideas

Santosh NcSantosh Nc
4 min read

Have you ever built a simple machine โ€” maybe a LEGO car, a paper plane, or even a sandcastle with tunnels? Thatโ€™s exactly what an API is: a system with defined parts that serve a purpose and can be used by others. In this post, Iโ€™ll walk you through how to understand and use my Node.js REST API, how Swagger makes it easy, and how you can build your own mini-projects with it. Whether you're a DevOps learner, a full-stack newbie, or just API-curious, this post is for you.


๐Ÿงฑ What is This API?

This API is a simple inventory management system โ€” think of it like a digital cupboard. You can:

  • Add items to the cupboard (POST /api/items)

  • See what's inside (GET /api/items)

  • Update an item (PUT /api/items/:id)

  • Remove an item (DELETE /api/items/:id)

  • Check if the API is alive (GET /api/health)

It runs on Node.js + Express on Rocky Linux, and now comes with full Swagger documentation for ease of use.


๐Ÿ” What is Swagger? (The Tour Guide ๐Ÿงญ)

Swagger is like a tour guide that walks you through every door and switch in your API. Instead of reading a boring manual, Swagger gives you an interactive dashboard where you can:

  • See all API endpoints

  • Understand what they do

  • Try them out directly from the browser

Live Example:
If your API is running on http://localhost:3000, you can access Swagger UI at:

http://localhost:3000/api-docs

It looks like this:


๐Ÿ› ๏ธ Setting Up: Quick Start

If you're a beginner, here's how you can get started using my API:

1. Clone and Install

git clone https://github.com/yourname/api_mgmt.git
cd api_mgmt
npm install

2. Run It Locally

npm start

Now open http://localhost:3000/api-docs to view Swagger UI.


๐Ÿงช How to Use Each Endpoint (With Real-World Metaphors)

๐Ÿ” /api/health โ€“ Is the Machine On?

This endpoint checks the "pulse" of the server. It's like asking: โ€œHey, are you awake?โ€

curl http://localhost:3000/api/health

Response:

{ "status": "ok", "message": "API is running" }

๐Ÿ“ฆ /api/items โ€“ The Inventory Shelf

โž• POST /api/items โ€” Add an Item (๐Ÿ›’)

Imagine putting a book on a shelf. Use this to add a new item.

Request:

{
  "id": 1,
  "name": "Notebook",
  "quantity": 5
}

๐Ÿ‘€ GET /api/items โ€” View All Items (๐Ÿ‘“)

This is like opening the cupboard to see whatโ€™s inside.

โœ๏ธ PUT /api/items/:id โ€” Update an Item (๐Ÿช›)

Change an itemโ€™s quantity or name.

๐Ÿ—‘๏ธ DELETE /api/items/:id โ€” Remove an Item (๐Ÿšฎ)

Take an item off the shelf.

All of this is interactive via Swagger โ€” click, fill, and test!


๐Ÿ’ก Project Ideas Using This API

Here are some beginner-friendly project ideas:

1. ๐Ÿ“Š Inventory Tracker Web App

Use this API as a backend and build a frontend in React/Vue. Visualize item counts in a dashboard.

2. ๐Ÿ›’ Shopping Cart CLI

Write a terminal-based app in Python or Node.js that interacts with this API. Practice CRUD operations via CLI.

3. ๐Ÿ“ฑ Mobile Checklist App

Use Flutter or React Native to consume this API as a โ€œto-doโ€ or inventory checklist.

4. ๐Ÿ“ฆ IoT Inventory Counter

Simulate devices adding items to the API (like sensors in a warehouse). Use it to understand POST requests and automation.


๐Ÿ“˜ Bonus: How Swagger is Set Up

You can set up Swagger using two packages:

npm install swagger-ui-express swagger-jsdoc

Then in server.js:

const swaggerUi = require('swagger-ui-express');
const swaggerJsDoc = require('swagger-jsdoc');

const options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'Simple Node.js API',
      version: '1.0.0',
    },
  },
  apis: ['./routes/*.js'], // JSDoc comments live here
};

const swaggerSpec = swaggerJsDoc(options);
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));

Then use JSDoc-style comments in routes/items.js:

/**
 * @swagger
 * /api/items:
 *   get:
 *     summary: Get all items
 *     responses:
 *       200:
 *         description: List of items
 */

๐Ÿš€ Whatโ€™s Next?

Youโ€™ve just learned how to use a simple API with real documentation, tested it, and maybe even dreamed up a few apps. Want to keep going?

Next steps:

  • ๐Ÿ” Add authentication (e.g., JWT tokens)

  • ๐Ÿณ Containerize the API with Docker

  • ๐Ÿงช Write test cases with Jest or Supertest

  • โ˜๏ธ Deploy it to the cloud (AWS/DigitalOcean)


๐Ÿ—ฃ๏ธ Final Thoughts

APIs donโ€™t have to be intimidating. With Swagger, they become approachable โ€” like walking into a smart home where everything is labeled and responsive. This is your first step toward full-stack confidence or DevOps wizardry.

๐Ÿ› ๏ธ Try the API. Break it. Rebuild it. Extend it. Thatโ€™s how you grow.


๐Ÿ‘‰ GitHub - api_mgmt

0
Subscribe to my newsletter

Read articles from Santosh Nc directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Santosh Nc
Santosh Nc

I believe, "Hard work beats Talent when Talent doesn't work hard". A Technophile specialising in DevOps.Currently Employed at DevOps Engineer. Shaping my career with Jenkins,Docker, Automation,Poweshell, Python and other devops tools.