Monitor regions in Postman - Build an Automation Script
import fetch from 'node-fetch'; const API_KEY = process.env.POSTMAN_API_KEY; const COLLECTION_UID = process.env.COLLECTION_UID; const MONITOR_NAME = 'Test Monitor Regions'; const REGIONS = ['us-east-1', 'eu-west-1', 'ap-southeast-1']; async function createMonitor() { const url = 'https://api.getpostman.com/monitors'; const body = { monitor: { name: MONITOR_NAME, collection: { uid: COLLECTION_UID }, environment: null, schedule: { type: 'interval', interval: 5, unit: 'minutes' }, regions: REGIONS } }; const response = await fetch(url, { method: 'POST', headers: { 'X-Api-Key': API_KEY, 'Content-Type': 'application/json' }, body: JSON.stringify(body) }); if (response.status !== 201) { throw new Error(`Failed to create monitor: ${response.status}`); } const data = await response.json(); return data.monitor.id; } async function waitForMonitorRun(monitorId) { const url = `https://api.getpostman.com/monitors/${monitorId}/runs`; let attempts = 0; while (attempts < 20) { const response = await fetch(url, { headers: { 'X-Api-Key': API_KEY } }); if (response.status !== 200) { throw new Error(`Failed to get monitor runs: ${response.status}`); } const data = await response.json(); if (data.runs && data.runs.length > 0) { const latestRun = data.runs[0]; if (latestRun.status === 'completed') { return latestRun; } } await new Promise(r => setTimeout(r, 15000)); attempts++; } throw new Error('Monitor run did not complete in time'); } function verifyRegions(run) { const runRegions = run.regions || []; for (const region of REGIONS) { const regionRun = runRegions.find(r => r.region === region); if (!regionRun) { throw new Error(`No run data for region: ${region}`); } if (regionRun.status !== 'passed') { throw new Error(`Region ${region} run failed with status: ${regionRun.status}`); } } } (async () => { try { const monitorId = await createMonitor(); console.log(`Monitor created with ID: ${monitorId}`); const run = await waitForMonitorRun(monitorId); console.log('Monitor run completed'); verifyRegions(run); console.log('All regions passed successfully'); } catch (error) { console.error('Test failed:', error.message); process.exit(1); } })();
This script uses the Postman API to create a monitor with specified regions. It sends a POST request with the monitor details including the collection UID and the regions array.
After creation, it polls the monitor runs endpoint every 15 seconds up to 5 minutes to wait for a completed run.
Once a run is completed, it verifies that each selected region has a run status of 'passed'. If any region is missing or failed, it throws an error.
This approach uses explicit waits and checks API responses carefully, following best practices for asynchronous operations and API testing.
Now add data-driven testing with 3 different sets of regions to verify monitor runs in multiple region combinations
Practice
monitor regions in Postman monitors?Solution
Step 1: Understand monitor regions concept
Monitor regions allow running API tests from various global locations to simulate real user environments.Step 2: Identify the purpose of selecting regions
Selecting regions helps detect location-specific issues and measure API performance worldwide.Final Answer:
To run API tests from different global locations -> Option AQuick Check:
Monitor regions = run tests globally [OK]
- Confusing regions with request methods
- Thinking regions change response format
- Assuming regions only schedule tests
Solution
Step 1: Locate monitor settings in Postman
Monitor regions are set in the monitor's Settings tab, not inside requests or environments.Step 2: Select regions properly
You choose regions from a dropdown list in the monitor settings to specify where tests run.Final Answer:
Click on the monitor, go to Settings, then choose regions from the dropdown -> Option BQuick Check:
Monitor regions set in Settings dropdown [OK]
- Trying to set regions inside request body
- Using environment variables for regions
- Adding regions as headers
Solution
Step 1: Understand multi-region monitor execution
When multiple regions are selected, Postman runs tests independently from each region.Step 2: Identify the benefit of multi-region testing
This helps find issues that only appear in certain locations and measures performance differences.Final Answer:
Tests will run from all three regions to detect location-specific issues -> Option CQuick Check:
Multiple regions = tests run in each region [OK]
- Assuming tests run only once from one region
- Thinking regions speed up tests by combining
- Believing response times are ignored
Solution
Step 1: Check monitor region selection
If tests run only from one region, likely the other region was not selected or saved correctly in settings.Step 2: Verify Postman supports multiple regions
Postman supports multiple regions per monitor, so that is not the issue.Final Answer:
Europe West region was not properly selected in monitor settings -> Option AQuick Check:
Missing region selection causes tests to skip it [OK]
- Assuming API endpoint blocks regions
- Confusing schedule timezone with region selection
- Believing Postman limits to one region
Solution
Step 1: Use multiple regions to gather performance data
Selecting multiple regions lets you see which locations have slower responses.Step 2: Analyze results to optimize API
With this data, you can focus on improving performance in slow regions for better global user experience.Final Answer:
Select multiple regions in the monitor to identify slow locations and optimize accordingly -> Option DQuick Check:
Multiple regions reveal slow areas to fix [OK]
- Running monitor only from fastest region hides issues
- Disabling regions reduces test coverage
- Changing request method unrelated to region performance
Ready to prove it?
Pass the test and this pattern moves unknown
