Blog
Wild & Free Tools

UUID in Postman — How to Generate UUIDs in Pre-Request Scripts

Last updated: February 2026 5 min read
Quick Answer

Table of Contents

  1. Built-in {{$guid}} Dynamic Variable
  2. Pre-Request Script — Reusable UUID Variable
  3. Idempotency Key Pattern
  4. UUID in Postman Collections and Data Files
  5. Frequently Asked Questions

Postman can generate a UUID for every request automatically — no external tool, no copy-pasting. Use the built-in {{$guid}} dynamic variable for simple cases, or write a pre-request script to store the UUID in a variable and reuse it across multiple requests in the same run. Here is every method with copy-paste code.

Built-in Dynamic Variable — {{$guid}}

The fastest way to add a UUID to any request field in Postman is the built-in dynamic variable {{$guid}}. Type it directly into any body field, header value, or URL parameter — Postman replaces it with a new UUID v4 on every send.

// In a request body (JSON):
{
  "orderId": "{{$guid}}",
  "customerId": "{{$guid}}",
  "status": "pending"
}

// In a header:
Idempotency-Key: {{$guid}}

// In a URL path:
POST https://api.example.com/orders/{{$guid}}/items

Limitation: A new value is generated each time the variable is evaluated. If you reference {{$guid}} twice in the same request, you get two different UUIDs. If you need the same UUID in both the request body and a response assertion, use a pre-request script instead.

Pre-Request Script — Generate and Store a UUID Variable

// In the Pre-request Script tab:

// Method 1: Using Postman's built-in uuid library
const uuid = require('uuid');
const requestId = uuid.v4();
pm.environment.set('requestId', requestId);
// or pm.collectionVariables.set() / pm.variables.set()

// Method 2: Using crypto (Node.js 14.17+)
const requestId = require('crypto').randomUUID();
pm.environment.set('requestId', requestId);

// Method 3: pm.variables.replaceIn
const requestId = pm.variables.replaceIn('{{$guid}}');
pm.environment.set('requestId', requestId);

// Now use {{requestId}} in the request body, headers, URL:
// {
//   "idempotencyKey": "{{requestId}}",
//   "payload": { ... }
// }

// And reference it in the Tests tab for assertions:
// pm.test('Request ID matches', () => {
//   const body = pm.response.json();
//   pm.expect(body.requestId).to.eql(pm.environment.get('requestId'));
// });
Sell Custom Apparel — We Handle Printing & Free Shipping

Using UUID as an Idempotency Key in Postman

Idempotency keys prevent duplicate operations when requests are retried. The pattern: generate a UUID, store it in a variable, send it as a header. If the same key is sent twice, the server returns the original response.

// Pre-request Script:
const idempotencyKey = require('crypto').randomUUID();
pm.environment.set('idempotencyKey', idempotencyKey);

// In the Headers tab:
// Key: Idempotency-Key
// Value: {{idempotencyKey}}

// In the Headers tab (alternative — new UUID every request):
// Key: Idempotency-Key
// Value: {{$guid}}

For testing idempotency behavior: run the request once, copy the idempotencyKey environment value, set it as a fixed variable, and send again — the server should return the cached response rather than processing a new request.

UUID in Collection Runs and Data Files

// Collection Runner: generate a unique UUID per iteration
// Pre-request Script at collection level:
const iterationUUID = require('uuid').v4();
pm.collectionVariables.set('iterationId', iterationUUID);
pm.collectionVariables.set('runTimestamp', new Date().toISOString());

// In request body:
// {
//   "batchId": "{{iterationId}}",
//   "timestamp": "{{runTimestamp}}"
// }

// Data file (CSV) approach:
// If your data file has a uuid column, Postman reads it as {{uuid}}
// To generate UUIDs for a CSV data file, use our UUID generator
// to produce UUIDs, then paste into your CSV test data

When you need to quickly produce a batch of test UUIDs to paste into a Postman data file (CSV or JSON), the WildandFree UUID Generator generates 10 at a time with one click.

Generate Test UUIDs for Postman

Need UUIDs for your Postman data file, test fixtures, or a quick request body? Generate 1 or 10 UUID v4 values instantly — click copy, paste, done.

Open Free UUID Generator

Frequently Asked Questions

What is the difference between {{$guid}} and a pre-request UUID variable?

{{$guid}} generates a new UUID every time Postman evaluates that expression — including if you reference it twice in the same request, you get two different values. A pre-request variable (pm.environment.set("uuid", ...)) generates once and stores the result, so {{uuid}} evaluates to the same value everywhere in the request.

Does Postman have a built-in uuid() function?

Yes via the dynamic variable syntax: {{$guid}} generates a UUID v4 in any field. In scripts, use require("uuid").v4() (Postman includes the uuid package) or require("crypto").randomUUID() for the native Node.js method.

How do I reuse the same UUID in the request body and URL path?

Set it in a pre-request script: pm.environment.set("orderId", require("crypto").randomUUID()). Then reference {{orderId}} in both the URL path and the request body. Both will resolve to the same value because it was set once before the request was built.

Can I generate a UUID without a pre-request script in Postman?

{{$guid}} works in any text field in Postman — headers, body, URL, params — without any scripting. It is the simplest approach when you just need a unique value per request and do not need to reference that same value in multiple places.

David Rosenberg
David Rosenberg Technical Writer

David spent ten years as a software developer before shifting to technical writing covering developer productivity tools.

More articles by David →
Launch Your Own Clothing Brand — No Inventory, No Risk