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 todev
orprod
-
BASE_URL
: Add a placeholder value likehttp://localhost
(script will override this)
-
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}`);
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:
- When creating a request, instead of typing the full URL (e.g.,
http://localhost:5000/api/login
), use{{BASE_URL}}
as the base. - Example:
{{BASE_URL}}/api/v1/auth/login
-
This dynamically resolves to:
Why This Works
- The script dynamically updates
BASE_URL
based on yourENV
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");
}
If your API's response structure is different (e.g., responseData.access_token
or responseData.token
), modify the script accordingly
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);
3. Use the Token in Other Requests:
- In authenticated requests, reference
{{TOKEN}}
in headers
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! 🚀
Top comments (0)