Compare an API response with a saved baseline

Sometimes you don't want to write twenty assertions. You want to know: is this response the same as the one we know is right? That's a baseline comparison, sometimes called a snapshot or golden test.

It's useful for read-only endpoints with stable data (a product catalogue, a config endpoint, a pricing table) and for checking that a refactor or a new environment returns the same thing as before.

Step 1: A diff that tells you where

pm.expect(a).to.eql(b) tells you the two objects differ, not where. This helper returns the paths:

JavaScript
function diff(a, b, path = '$') {
  if (Object.is(a, b)) return [];
  if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) {
    return [`${path}: ${JSON.stringify(b)} → ${JSON.stringify(a)}`];
  }
  if (Array.isArray(a) !== Array.isArray(b)) return [`${path}: type changed`];
  const keys = new Set([...Object.keys(a), ...Object.keys(b)]);
  return [...keys].flatMap((k) => diff(a[k], b[k], Array.isArray(a) ? `${path}[${k}]` : `${path}.${k}`));
}

diff(actual, baseline) returns lines like $.plans[1].price: 12 → 15 and $.plans[2].name: undefined → "Enterprise" (a field that's new).

Step 2: Ignore what changes every time

JavaScript
const IGNORE = ['id', 'createdAt', 'updatedAt', 'requestId', 'etag'];

function normalize(value) {
  if (Array.isArray(value)) return value.map(normalize);
  if (value && typeof value === 'object') {
    return Object.fromEntries(
      Object.entries(value)
        .filter(([k]) => !IGNORE.includes(k))
        .map(([k, v]) => [k, normalize(v)]),
    );
  }
  return value;
}

Step 3: Record a baseline, then compare

Put both helpers and this logic in the request's post-response script:

JavaScript
const actual = normalize(pm.response.json());
const saved = pm.environment.get('baseline_pricing');

if (!saved) {
  console.log('No baseline yet. Copy this into the environment variable baseline_pricing:');
  console.log(JSON.stringify(actual));
} else {
  pm.test('pricing matches the baseline', () => {
    pm.expect(diff(actual, JSON.parse(saved)).slice(0, 10)).to.be.empty;
  });
}

The first send prints the normalized response. Paste it into an environment variable named baseline_pricing (a normal variable, not a secret). Every later send compares against it, and a failure lists up to ten paths that changed.

When a change is intended, update the variable. In CI, the same check runs with requestarium run-collection.

Comparing two environments

To check that staging returns the same as production for a read-only endpoint, compare two requests in one run:

JavaScript
// "Pricing (production)" — post-response
pm.environment.set('prodPricing', normalize(pm.response.json()));
JavaScript
// "Pricing (staging)" — post-response, the next request in the folder
const prod = JSON.parse(pm.environment.get('prodPricing'));
pm.test('staging pricing matches production', () => {
  pm.expect(diff(normalize(pm.response.json()), prod).slice(0, 10)).to.be.empty;
});

Give the two requests different base URLs, for example {{prodBaseUrl}} and {{stagingBaseUrl}}, in one environment. Use this only for read-only requests: never send writes to production from a test.

When not to use a baseline

  • Data that legitimately changes (feeds, search results, anything with "today"). You'll update the baseline more often than it catches bugs.
  • Large responses. Diff a page, not the whole catalogue.
  • As your only test. A baseline says "something changed", not "this is correct". Keep a few real assertions for the fields that matter.