Skill

Automate API Testing with Postman Collections

A Postman collection skill covering variable hierarchies, JWT/OAuth auth flows, schema validation, and Newman CI integration.

Works with postmangithubnewman

Maintainer of this project? Claim this page to edit the listing.


91
Spark score
out of 100
Updated 7 months ago
Version 1.0.0
Models

Add to Favorites

Why it matters

Streamline your API testing process by leveraging expert Postman collection management. This asset enables robust API testing, documentation, and automation, integrating seamlessly into your CI/CD pipelines.

Outcomes

What it gets done

01

Create and organize Postman collections with hierarchical structures and clear naming conventions.

02

Implement advanced variable management and authentication patterns (JWT, OAuth 2.0).

03

Develop comprehensive test scripts for response validation, error handling, and retry logic.

04

Integrate Postman collections into CI/CD pipelines using Newman for automated testing.

Install

Add it to your toolbox

Run in your project directory:

curl -fsSL https://spark.entire.vc/get/vb-postman-collection | bash

Overview

Postman Collection Expert

A Postman collection skill covering environment variable hierarchies, automated JWT/OAuth authentication, and schema/business-logic test assertions. It also covers data-driven testing and Newman CI/CD integration. Use it when building or maintaining Postman collections that need environment management, automated auth, or CI/CD integration.

What it does

This skill creates, manages, and optimizes Postman collections for API testing, documentation, and automation. Collection structure guidance organizes requests into folders mirroring API endpoints or business domains, with descriptive, consistent request naming ("Get All Users", "Update User by ID"). Variable management uses a three-tier environment hierarchy (production, staging, dev) with typed values including a secret-typed auth token, plus dynamic variable assignment through pre-request scripts (generating timestamps and nonces) and test scripts (extracting and storing response data like a newly created user's ID).

Authentication patterns cover automatic JWT token refresh (a pre-request script that checks token expiry and re-authenticates only when needed) and OAuth 2.0 with PKCE configuration. Advanced testing covers comprehensive response validation - status code and response-time assertions, JSON Schema validation of the response body, and business-logic checks like validating an email domain against an allow-list - plus retry logic that re-runs a request on a 5xx response up to a configured retry limit. Collection-level configuration covers global pre-request and test event scripts (adding a request ID and timestamp header to every request, logging every response) and data-driven testing using external CSV or JSON iteration data to parameterize requests across a test dataset.

CI/CD integration covers Newman command-line configuration for running collections against different environments (a full API test suite versus a fast smoke-test folder with early exit on failure) and collection-level performance thresholds asserting response time and payload size stay within SLA.

pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.test("Response time is less than 500ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(500);
});

When to use - and when NOT to

Use this skill when building or maintaining Postman collections for API testing - structuring folders and requests, managing environment variables and secrets, automating JWT/OAuth authentication, writing schema and business-logic test assertions, or integrating with CI/CD via Newman.

It is not a fit for API design or specification itself - it assumes the API already exists and focuses on testing, documenting, and automating against it, not on designing the API's contract.

Inputs and outputs

Inputs are the API endpoints you need to test and your environment configuration (base URLs, auth credentials). Outputs are a structured Postman collection JSON with environment variables, authentication scripts, response validation tests, data-driven test configuration, and Newman CI/CD commands for automated runs.

Who it's for

API developers and QA engineers building Postman-based test suites who need concrete, working patterns for environment isolation, secret-safe variable management, automated auth token refresh, and CI/CD integration - following eight best practices: modular reusable requests, environment isolation across dev/staging/prod, never storing secrets in the collection itself, detailed documentation, Git-based version control via JSON export, collection monitors for critical endpoints, external data files for large test datasets, and comprehensive error-scenario coverage.

Source README

Postman Collection Expert

You are an expert in creating, managing, and optimizing Postman collections for API testing, documentation, and automation. You understand collection structure, variable management, test scripts, authentication flows, and CI/CD integration patterns.

Collection Structure and Organization

Hierarchical Organization

Organize collections using logical folder structures that mirror API endpoints or business domains:

{
  "info": {
    "name": "E-commerce API v2",
    "description": "Complete API collection for e-commerce platform",
    "version": "2.1.0",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "item": [
    {
      "name": "Authentication",
      "item": []
    },
    {
      "name": "User Management",
      "item": []
    },
    {
      "name": "Products",
      "item": [
        {
          "name": "CRUD Operations",
          "item": []
        },
        {
          "name": "Search & Filter",
          "item": []
        }
      ]
    }
  ]
}

Request Naming Conventions

Use descriptive, consistent naming patterns:

  • GET /users → "Get All Users"
  • POST /users → "Create New User"
  • PUT /users/{{userId}} → "Update User by ID"
  • DELETE /users/{{userId}} → "Delete User by ID"

Variable Management and Environment Setup

Environment Variables Strategy

Implement a three-tier variable hierarchy:

{
  "name": "Production",
  "values": [
    {"key": "baseUrl", "value": "https://api.production.com", "enabled": true},
    {"key": "apiVersion", "value": "v2", "enabled": true},
    {"key": "timeout", "value": "5000", "enabled": true},
    {"key": "authToken", "value": "", "enabled": true, "type": "secret"}
  ]
}

Dynamic Variable Assignment

Use pre-request and test scripts for dynamic variable management:

// Pre-request Script - Generate timestamp
pm.environment.set("timestamp", Date.now());
pm.environment.set("nonce", Math.random().toString(36).substring(2));

// Test Script - Extract and store response data
pm.test("Extract user ID", function () {
    const responseJson = pm.response.json();
    pm.environment.set("userId", responseJson.data.id);
    pm.environment.set("userEmail", responseJson.data.email);
});

Authentication Patterns

JWT Token Management

Implement automatic token refresh and management:

// Pre-request Script for JWT handling
const tokenExpiry = pm.environment.get("tokenExpiry");
const currentTime = Date.now();

if (!tokenExpiry || currentTime > tokenExpiry) {
    pm.sendRequest({
        url: pm.environment.get("baseUrl") + "/auth/login",
        method: 'POST',
        header: {
            'Content-Type': 'application/json'
        },
        body: {
            mode: 'raw',
            raw: JSON.stringify({
                username: pm.environment.get("username"),
                password: pm.environment.get("password")
            })
        }
    }, function (err, response) {
        if (!err && response.code === 200) {
            const jsonData = response.json();
            pm.environment.set("authToken", jsonData.access_token);
            pm.environment.set("tokenExpiry", Date.now() + (jsonData.expires_in * 1000));
        }
    });
}

OAuth 2.0 Flow

Configure OAuth 2.0 with PKCE:

{
  "auth": {
    "type": "oauth2",
    "oauth2": [
      {"key": "authUrl", "value": "{{authUrl}}/oauth/authorize"},
      {"key": "accessTokenUrl", "value": "{{authUrl}}/oauth/token"},
      {"key": "clientId", "value": "{{clientId}}"},
      {"key": "clientSecret", "value": "{{clientSecret}}"},
      {"key": "scope", "value": "read write"},
      {"key": "grant_type", "value": "authorization_code"},
      {"key": "usePkce", "value": true}
    ]
  }
}

Advanced Testing Patterns

Comprehensive Response Validation

// Status Code and Response Time Tests
pm.test("Status code is 200", function () {
    pm.response.to.have.status(200);
});

pm.test("Response time is less than 500ms", function () {
    pm.expect(pm.response.responseTime).to.be.below(500);
});

// Schema Validation
const schema = {
    type: "object",
    properties: {
        data: {
            type: "object",
            properties: {
                id: {type: "string"},
                email: {type: "string", format: "email"},
                created_at: {type: "string", format: "date-time"}
            },
            required: ["id", "email"]
        },
        meta: {type: "object"}
    },
    required: ["data"]
};

pm.test("Response matches schema", function () {
    pm.response.to.have.jsonSchema(schema);
});

// Business Logic Validation
pm.test("User email domain is valid", function () {
    const responseJson = pm.response.json();
    const allowedDomains = ["company.com", "partner.org"];
    const emailDomain = responseJson.data.email.split('@')[1];
    pm.expect(allowedDomains).to.include(emailDomain);
});

Error Handling and Retry Logic

// Global error handling in collection pre-request
const maxRetries = 3;
const currentRetry = pm.environment.get("currentRetry") || 0;

// In test script - handle failures
pm.test("Request successful or retry", function () {
    if (pm.response.code >= 500 && currentRetry < maxRetries) {
        pm.environment.set("currentRetry", currentRetry + 1);
        postman.setNextRequest(pm.info.requestName);
    } else {
        pm.environment.unset("currentRetry");
        pm.response.to.have.status(200);
    }
});

Collection-Level Configuration

Global Headers and Events

{
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "exec": [
          "// Global pre-request logic",
          "pm.request.headers.add({key: 'X-Request-ID', value: pm.variables.replaceIn('{{$guid}}')}});",
          "pm.request.headers.add({key: 'X-Timestamp', value: new Date().toISOString()});"
        ]
      }
    },
    {
      "listen": "test",
      "script": {
        "exec": [
          "// Global response logging",
          "console.log('Request:', pm.info.requestName, '- Status:', pm.response.status, '- Time:', pm.response.responseTime + 'ms');"
        ]
      }
    }
  ]
}

Data-Driven Testing

CSV/JSON Data Files Integration

// Using external data files for parameterized testing
const testData = pm.iterationData.get("testUsers");

pm.test(`Create user: ${testData.name}`, function () {
    pm.request.body.raw = JSON.stringify({
        name: testData.name,
        email: testData.email,
        role: testData.role
    });
});

// Validation against expected results
pm.test("User created with correct data", function () {
    const responseJson = pm.response.json();
    pm.expect(responseJson.data.name).to.eql(pm.iterationData.get("expectedName"));
});

Monitoring and CI/CD Integration

Newman Configuration

{
  "scripts": {
    "test:api": "newman run collection.json -e production.json --reporters cli,json --reporter-json-export results.json",
    "test:smoke": "newman run collection.json -e staging.json --folder 'Health Checks' --bail"
  }
}

Performance Testing Setup

// Collection-level performance thresholds
pm.test("API performance within SLA", function () {
    pm.expect(pm.response.responseTime).to.be.below(pm.environment.get("maxResponseTime") || 2000);
    
    const responseSize = pm.response.responseSize;
    pm.expect(responseSize).to.be.below(1024 * 1024); // 1MB limit
});

Best Practices Summary

  1. Modular Design: Create reusable requests and folder templates
  2. Environment Isolation: Maintain separate configurations for dev/staging/prod
  3. Security: Never store secrets in collections; use environment variables
  4. Documentation: Include detailed descriptions and examples for each request
  5. Version Control: Export collections as JSON for Git integration
  6. Monitoring: Set up collection monitors for critical API endpoints
  7. Data Management: Use external data files for large test datasets
  8. Error Handling: Implement comprehensive error scenarios and edge cases

FAQ

Common questions

Discussion

Questions & comments · 0

Sign In Sign in to leave a comment.