Postman JavaScript Snippets You’ll Actually Reuse (and Why)
Small pre-request & test scripts make collections reliable in CI and faster to debug.
1) Variables & logs (chain requests fast)
Why: pass data between calls; see what ran where.
pm.environment.set('token','...');
pm.collectionVariables.set('runid', _Date.now());
console.log('run:', pm.collectionVariables.get('runid'));
2) Dynamic data (no collisions in tests)
When: creating users/orders many times.
pm.collectionVariables.set('ts', Date.now());
pm.collectionVariables.set('uuid', pm.variables.replaceIn('{{$guid}}'));
pm.collectionVariables.set('randEmail', pm.variables.replaceIn('{{$randomEmail}}'));
3) Consistent headers (trace & auth)
Why: searchable logs + stable auth.
pm.request.headers.upsert({key:'X-Correlation-Id',value:pm.variables.replaceIn('{{$guid}}')});
pm.request.headers.upsert({key:'Authorization',value:Bearer ${pm.collectionVariables.get('accesstoken')}});
See the full list on our LinkedIn page!