DEV Community

Cover image for Streamline Your API Workflow with 2 Essential Postman Configurations
Saleh Khatri
Saleh Khatri

Posted on

3 1 1 1 1

Streamline Your API Workflow with 2 Essential Postman Configurations

Postman is a powerful tool for API development, but small configuration tweaks can turn it into a productivity powerhouse. Let’s explore two setups that’ll save you from repetitive tasks: auto-switching environments and hands-free token management.

1. Auto-Switch Between Dev and Prod Environments

No more manual URL changes!

The Problem

Switching between localhost (development) and your live production API endpoint? Manually updating the BASE_URL every time is error-prone and tedious.

The Solution

Add a pre-request script to your Postman collection to auto-detect the environment and set the correct BASE_URL.

Setup Steps

1. Create Environment Variables

  • Open Postman → Click the "Environments" tab (left sidebar).
  • Create a new environment (e.g., My Project).
  • Add two variables:
    • ENV: Set value to dev or prod
    • BASE_URL: Add a placeholder value like http://localhost (script will override this)

Variables_setup

2. Add the Pre-Request Script

  • Go to your Collection → Go to Scripts tab → Click the "Pre-request" tab.
  • Paste this code:
// Get environment (default to 'dev' if not set)
const environment = pm.environment.get("ENV") || "dev";

// Set BASE_URL dynamically
if (environment === "dev") {
  pm.environment.set("BASE_URL", "http://localhost:5000");
} else if (environment === "prod") {
  pm.environment.set("BASE_URL", "https://lfsms-backend.onrender.com");
} else {
  throw new Error("Invalid environment. Set ENV to 'dev' or 'prod'.");
}

console.log(`🌐 Environment set to: ${environment}`);
Enter fullscreen mode Exit fullscreen mode

Pre-Script image

How to Use BASE_URL in Your Requests

Once the BASE_URL variable is set, you can reuse it across all requests in your collection. Here’s how:

BASE_URL usage

Why This Works

  • The script dynamically updates BASE_URL based on your ENV variable.
  • Use the same request structure across environments – no more manual edits!

2. Automatically Save JWT Tokens After Login

No more manual token copying!

The Problem

After logging in, you need to manually copy the JWT token from the response and paste it into headers for authenticated requests. This repetitive work slows you down!

The Solution

Add a script to your authentication request to auto-save the token to environment variables.

Setup Steps

1. Add a Script to Your Auth Request

  • Open your login/sign-in request in Postman.
  • Go to the "Script" tab.
  • Paste this code in Post-response Scripts:
if (pm.response.code === 200) {
  const responseData = pm.response.json();

  // Replace 'data.token' with your API's actual token path
  pm.environment.set("TOKEN", responseData.data.token); // Token path
  console.log("🔑 Token saved automatically!");
} else {
  console.log("⚠️ Login failed: Check credentials");
}
Enter fullscreen mode Exit fullscreen mode

If your API's response structure is different (e.g., responseData.access_token or responseData.token), modify the script accordingly

Auth Script

2. Customize for Your API

If your token isn’t in responseData.data.token:

  • Check your API response in Postman’s "Response" panel
  • Adjust the path in the script. Examples:
// If your token is directly in the root:
pm.environment.set("TOKEN", responseData.token);

// If nested under an "auth" object:
pm.environment.set("TOKEN", responseData.auth.token);
Enter fullscreen mode Exit fullscreen mode

3. Use the Token in Other Requests:

  • In authenticated requests, reference {{TOKEN}} in headers

USE_TOKEN

Why This Works

  • The script extracts the token/role from your login response’s JSON structure
  • Works for any API format – just update the JSON path to match your response
  • No more manual copy-pasting!

Let’s Build Better APIs Together!

I’m always curious to see how developers streamline their workflows and solve API challenges. If you tried this setup, ran into issues, or have your own Postman hacks to share, let’s chat!

📬 Find me here:
🐦 Twitter: @salehdotdev

🌐 Portfolio: salehkhatri.tech

📧 Email: salehkhatri29@gmail.com

Follow for more:
I regularly share tips on Postman automation, API development, and backend best practices. Let’s turn tedious tasks into elegant solutions!

Stuck with configurations?
If you’re wrestling with environment variables or scripting quirks, reach out! I’m happy to help troubleshoot or brainstorm ideas.

Remember: Great tools aren’t just about power – they’re about making your dev life simpler. Keep iterating, keep shipping! 🚀

Image of AssemblyAI

Automatic Speech Recognition with AssemblyAI

Experience near-human accuracy, low-latency performance, and advanced Speech AI capabilities with AssemblyAI's Speech-to-Text API. Sign up today and get $50 in API credit. No credit card required.

Try the API

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay