DEV Community

Cover image for Save Hours on API Testing: Auto-Generate Postman Collections from Your Express.js App πŸ§ͺ
Ali nazari
Ali nazari

Posted on

Save Hours on API Testing: Auto-Generate Postman Collections from Your Express.js App πŸ§ͺ

Shipping a well-documented API can feel like a second job. You build your Express routes, then spend even more time hand-crafting Postman collections so teammates and QA can exercise your endpoints.

What if you could generate that collection automaticallyβ€”without extra config, manual exports, or tedious copy-pasting?

Enter express-to-postman, a zero-config CLI that introspects your running Express app (JS or TS), bundles up all your local route modules, and spits out a fully valid Postman v2.1 collectionβ€”grouped by URL segment and ready to import.


πŸš€ Key Benefits

  • Instant Collection Generation
    Point the CLI at your entry file (app.js or app.ts) and get a complete Postman JSON in one command.

  • Zero Boilerplate
    No decorators or special annotationsβ€”just your normal app.get() / router.use() calls.

  • TypeScript-Ready
    Transpiles .ts on the fly, bundles your local modules, and externalizes your npm deps automatically.

  • Organized Output
    Routes are grouped by their first path segment (/users, /comments, etc.), so your collection stays tidy.

  • CLI & Programmatic
    Use it from the terminal or integrate generateCollection() into your build scripts, CI pipelines, or docs generator.


🎯 How It Works

  1. Bundle Your App
    Under the hood, express-to-postman uses [esbuild] to bundle all local .js/.ts files into a single temporary ESM module. External npm packages (like express, cors, etc.) remain as imports.

  2. Introspect Routes
    It dynamically imports that bundle, walks through the Express router stack, and extracts every route’s path and HTTP methodβ€”even those nested under sub-routers.

  3. Emit Postman JSON
    It then constructs a v2.1 Postman Collection, grouping items by first path segment, and writes it to your chosen output file.


✨ Quick Start

# Install globally
npm install -g express-to-postman

# Generate a collection in one go
express-to-postman \
  -i ./src/app.ts \
  -o ./postman.collection.json
Enter fullscreen mode Exit fullscreen mode
# Or as a dev dependency & npm script
npm install --save-dev express-to-postman

# package.json
"scripts": {
  "postman": "express-to-postman -i ./src/server.js -o ./collections/api.json"
}
Enter fullscreen mode Exit fullscreen mode

πŸ“ Example

Given this simple app.ts:

import express from 'express'
import userRouter from './routes/users'
export const app = express()

app.use('/users', userRouter)
app.get('/', (_req, res) => res.send('OK'))
Enter fullscreen mode Exit fullscreen mode

Running:

express-to-postman -i src/app.ts -o postman.json
Enter fullscreen mode Exit fullscreen mode

Produces a JSON with two foldersβ€”users and /β€”and three requests:

  • GET /users
  • POST /users/:id
  • GET /

All ready for import into Postman or Newman.


πŸ”§ Advanced Tips

  • Verbose Mode: Add -v to see intermediate logs and the full JSON in your terminal.
  • CI Integration: Run express-to-postman in your pipeline to always ship up-to-date API collections.
  • Programmatic Use:
  import { generateCollection } from 'express-to-postman'
  await generateCollection('./dist/app.js', './api.postman.json', true)
Enter fullscreen mode Exit fullscreen mode

πŸ™Œ Try It Today!

Stop spending hours maintaining Postman collections by hand. Install express-to-postman in seconds and let it keep your API docs in sync with your code.

npm install -g express-to-postman
Enter fullscreen mode Exit fullscreen mode

If you found this helpful, feel free to share

Let’s connect!!: 🀝

LinkedIn
GitHub

Top comments (4)

Collapse
 
silentwatcher_95 profile image
Ali nazari

I built express-to-postman to save you hours of manual Postman-collection maintenanceβ€”let me know how it fits into your workflow!

πŸ”— Repo & Docs: github.com/your-username/express-t...

πŸ’¬ I’m eager to hear your feedback or any feature requests (auth detection, custom grouping, examples). Drop a line below or open an issue on GitHub!

Collapse
 
nathan_tarbert profile image
Nathan Tarbert

pretty cool tbh, been burned too many times by out-of-date docs. you think devs ignore stuff like this because the old way feels safer or they just get stuck in habits?

Collapse
 
silentwatcher_95 profile image
Ali nazari

manually writing docs can feel safer since you control the details and avoid auto-generated errors or outdated info.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.