Creating monitors in Postman - Automation Script Walkthrough
import fetch from 'node-fetch'; const POSTMAN_API_KEY = process.env.POSTMAN_API_KEY; const COLLECTION_UID = process.env.COLLECTION_UID; // e.g. '123abc' const ENVIRONMENT_UID = process.env.ENVIRONMENT_UID; // optional async function createMonitor() { const url = 'https://api.getpostman.com/monitors'; const monitorData = { monitor: { name: 'Daily API Check', collection: { uid: COLLECTION_UID }, environment: ENVIRONMENT_UID ? { uid: ENVIRONMENT_UID } : undefined, schedule: { type: 'daily', interval: 1, time: '09:00' } } }; const response = await fetch(url, { method: 'POST', headers: { 'X-Api-Key': POSTMAN_API_KEY, 'Content-Type': 'application/json' }, body: JSON.stringify(monitorData) }); if (response.status !== 201) { throw new Error(`Failed to create monitor, status code: ${response.status}`); } const data = await response.json(); if (data.monitor.name !== 'Daily API Check') { throw new Error('Monitor name does not match'); } if (!data.monitor.schedule || data.monitor.schedule.type !== 'daily' || data.monitor.schedule.time !== '09:00') { throw new Error('Monitor schedule is incorrect'); } console.log('Monitor created successfully:', data.monitor.uid); // Verify monitor is listed const listResponse = await fetch(url, { method: 'GET', headers: { 'X-Api-Key': POSTMAN_API_KEY } }); if (listResponse.status !== 200) { throw new Error(`Failed to fetch monitors, status code: ${listResponse.status}`); } const listData = await listResponse.json(); const found = listData.monitors.some(m => m.uid === data.monitor.uid); if (!found) { throw new Error('Created monitor not found in monitor list'); } console.log('Monitor verified in list successfully'); } createMonitor().catch(err => { console.error('Test failed:', err.message); process.exit(1); });
This script uses Node.js with the node-fetch library to call the Postman API.
First, it reads environment variables for the API key, collection UID, and optionally environment UID.
It sends a POST request to create a monitor with the specified name, collection, and schedule.
It checks the response status is 201 (created) and verifies the monitor name and schedule in the response.
Then it sends a GET request to list all monitors and confirms the newly created monitor is present.
Errors are thrown if any assertion fails, causing the script to exit with failure.
This approach uses async/await for clear asynchronous code and environment variables for sensitive data.
Now add data-driven testing to create monitors with 3 different schedules: daily at 9 AM, weekly on Monday at 10 AM, and hourly every 2 hours.
Practice
Solution
Step 1: Understand what monitors do
Monitors in Postman are designed to run tests automatically without manual intervention.Step 2: Identify the main use
They run API tests on a schedule to catch issues early.Final Answer:
To run API tests automatically on a schedule -> Option AQuick Check:
Monitors = Automated scheduled tests [OK]
- Thinking monitors are for manual testing
- Confusing monitors with writing test scripts
- Assuming monitors create documentation
Solution
Step 1: Recall monitor prerequisites
Monitors need a saved collection that contains tests to run automatically.Step 2: Check other options
Environment variables help but are not strictly required; a workspace without collections cannot run tests; manual reports are unrelated.Final Answer:
A saved collection with tests -> Option DQuick Check:
Monitor setup = saved collection needed [OK]
- Assuming environment variables are mandatory
- Trying to create monitors without collections
- Confusing manual reports with monitor setup
Solution
Step 1: Understand monitor behavior on failure
If the API is down, the requests in the collection will fail during the monitor run.Step 2: Identify monitor reporting
Monitors report test failures and errors; they do not skip or ignore failures, nor fix issues automatically.Final Answer:
The monitor run will fail and report errors -> Option BQuick Check:
API down = monitor reports failure [OK]
- Thinking monitors skip failed tests
- Believing monitors fix API problems
- Assuming monitor reports success despite failures
Solution
Step 1: Check monitor prerequisites
If the collection linked to the monitor is deleted, the monitor cannot run.Step 2: Evaluate other options
Running every minute is valid; having no tests means no failures but monitor still runs; monitors do not have manual-only mode.Final Answer:
The collection used by the monitor was deleted -> Option AQuick Check:
Deleted collection = monitor won't run [OK]
- Assuming schedule frequency stops runs
- Thinking monitors can be manual-only
- Confusing empty tests with no runs
Solution
Step 1: Prepare the collection
You must have a saved collection that contains the API tests to run.Step 2: Configure the monitor schedule and alerts
Create a monitor scheduled daily at 9 AM UTC and enable email alerts only on test failures.Final Answer:
Save a collection with tests, create a monitor scheduled daily at 9 AM UTC, enable email alerts on failure -> Option CQuick Check:
Collection + daily schedule + alerts on failure = correct setup [OK]
- Scheduling monitor without tests in collection
- Disabling alerts when failures occur
- Creating monitor without a collection
Ready to prove it?
Pass the test and this pattern moves unknown
