Skip to content

TST-Technology/sync-postmanCollection

Repository files navigation

🔄 Automate Your Postman Collection Sync with Agentic Dev Mode In NodeJs

🧠 What's the Problem?

Every time we create or update an API in the backend (Node.js), we have to manually go to Postman, add the route, method, request body, expected response, and organize it into folders. It's repetitive, error-prone, and a waste of time—especially when we're iterating fast in development.

🤯 What If This Process Was Automated?

Imagine this:

  1. You tell your AI agent to create/update an Express route
  2. It auto-generates the updated Postman collection JSON
  3. And with a single command, your Postman app is updated instantly.

🛠️ Step-by-Step Setup: Automate Postman Collection Sync

✅ Step 1: Agentic Command to Generate Postman Collection

Tell your agent (e.g., inside VS Code + Cursor or a CLI tool) to:

Read all Express route files in this project and generate a Postman collection JSON containing:

  • All routes and methods
  • Example request and response bodies
  • Use variable for base URL (e.g., {{base_url}})
  • Group endpoints by filename (as folders in Postman)

This generates a postman_collection.json file in your project root.

✅ Step 2: Create syncPostmanCollection.js Script

In your project root, create a file syncPostmanCollection.js:

"use strict";

const fs = require("fs");
const axios = require("axios");

// Configuration
const POSTMAN_API_KEY = "PMAK-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";  // Replace with your API Key
const WORKSPACE_ID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";     // Replace with your workspace ID
const COLLECTION_UID = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";   // Optional: If updating existing collection

// Read collection from file
const collection = JSON.parse(
    fs.readFileSync("postman_collection.json", "utf8")
);

// API request headers
const headers = {
    "X-Api-Key": POSTMAN_API_KEY,
    "Content-Type": "application/json"
};

/**
 * Uploads or updates the Postman collection
 */
async function uploadCollection() {
    try {
        if (COLLECTION_UID) {
            // Update existing collection
            await axios.put(
                `https://api.getpostman.com/collections/${COLLECTION_UID}`,
                { collection },
                { headers }
            );
            console.log("✅ Collection updated in Postman");
        } else {
            // Create new collection
            await axios.post(
                "https://api.getpostman.com/collections",
                { collection, workspace: WORKSPACE_ID },
                { headers }
            );
            console.log("✅ Collection created in Postman");
        }
    } catch (err) {
        console.error("❌ Failed to sync with Postman:", err.response?.data || err);
    }
}

// Execute the upload
uploadCollection();

✅ Step 3: Add Script in package.json

Add this script to your package.json:

{
    "scripts": {
        "sync:postman": "node syncPostmanCollection.js"
    }
}

Now you can run:

npm run sync:postman

✅ Step 4: Making New Routes with a Simple Agent Command

Whenever you add or update a route, just tell your agent:

In router `user.js`, add `createUser` POST API.

Then, update the `postman_collection.json` and run `npm run sync:postman`

The agent:

  1. Updates your Express code
  2. Modifies the postman_collection.json
  3. Syncs Postman in one shot 🎯

💥 Benefits of This Setup

Traditional Workflow Agentic Sync Workflow
Manual Postman updates after every route change Just speak/update code, Postman syncs automatically
Repetitive and error-prone Fully automated and always up-to-date
Wasted 10–15 mins per update Saved hours every day
Tedious folder creation in Postman Auto-grouped by filename

🗣️ Final Thoughts

If you're working in an AI-augmented coding environment, this Postman sync system is a game-changer. It keeps your API docs updated automatically, and removes a major pain point from backend workflows.

This can be used in all backend projects, and it's fantastic. Set it up once, and forget about manual Postman updates forever.

📎 Ready to Try?

Set this up in your project today and let your agent handle the rest!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published