0
0
Postmantesting~15 mins

Creating monitors in Postman - Automation Script Walkthrough

Choose your learning style9 modes available
Create a Postman Monitor for API Collection
Preconditions (2)
Step 1: Open Postman application or web version
Step 2: Navigate to the Collections tab
Step 3: Select the desired collection to monitor
Step 4: Click on the three dots (options) next to the collection name
Step 5: Choose 'Monitor' from the dropdown menu
Step 6: Click 'Create a monitor' button
Step 7: Enter a monitor name, e.g., 'Daily API Check'
Step 8: Set the environment if needed
Step 9: Set the schedule to run every day at 9 AM
Step 10: Click 'Create Monitor' to save
✅ Expected Result: A new monitor is created for the selected collection, scheduled to run daily at 9 AM, and visible under the Monitors tab
Automation Requirements - Postman API with Newman and Node.js
Assertions Needed:
Verify the monitor creation API returns status 201
Verify the monitor name matches the input
Verify the schedule is set correctly
Verify the monitor is listed when fetching monitors
Best Practices:
Use environment variables for collection and environment IDs
Use explicit API calls to create and verify monitors
Handle API authentication securely with Postman API key
Use async/await for API calls
Validate responses with proper assertions
Automated Solution
Postman
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.

Common Mistakes - 4 Pitfalls
Hardcoding API keys directly in the script
Not checking HTTP response status codes before parsing JSON
Ignoring asynchronous nature and not using async/await or proper promise handling
Not verifying that the created monitor appears in the monitor list
Bonus Challenge

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.

Show Hint