Template functions

Some values change on every send: a request id, a timestamp, a random name. Template functions produce them inline, inside {{ }}, without writing a script.

POST {{baseUrl}}/events
X-Request-Id: {{ uuid() }}
X-Sent-At: {{ isoDate() }}
Authorization: Basic {{ base64Encode("api:secret") }}

::: body json
{
  "id": "{{ uuid() }}",
  "timestamp": {{ now() }},
  "score": {{ randomInt(1, 100) }},
  "nickname": "user-{{ randomString(6) }}"
}

Functions

FunctionArgumentsReturnsExample result
uuid()noneA random UUID, version 43f2b8c1e-7a4d-4e8b-9f61-2c5d0a9b7e14
now()noneMilliseconds since the Unix epoch1791302400000
isoDate()noneThe current UTC time, RFC 33392026-10-06T09:30:00Z
randomInt(min, max)two integersA random integer between min and max, both included. If min is greater than max, they are swapped42
randomString(len)one integerA random string of letters and digits of that length, up to 1,048,576 characters. A longer or negative length failsaZ3kQ9
base64Encode(str)one stringStandard Base64 encodingYXBpOnNlY3JldA==
base64Decode(str)one stringThe decoded text. Fails if the input is not valid Base64 or does not decode to UTF-8 textapi:secret
oauthToken(profile), oauthToken(profile, "id_token")a profile name, and optionally "access_token" (default) or "id_token"The token of that OAuth 2.0 profile for the active environment, obtained or renewed before the pre-request script, as the OAuth 2.0 auth type does. For a token in another header or in the bodyeyJhbGciOi…
urlEncode(str)one stringPercent encoding. Letters, digits and - . _ ~ are kept as isa%20b%26c

Each call is evaluated on its own, so {{ uuid() }} written twice gives two different UUIDs. To reuse one value in several places, generate it once in a pre-request script with vars.set("id", util.uuid()) and write {{id}} wherever you need it.

Arguments

An argument is one of:

  • A string in double quotes: "user:pass". Inside it, \" is a quote and \\ is a backslash. No other escapes exist.
  • An integer: 1, -5.
  • A variable name, without braces or quotes: base64Encode(credentials) uses the value of the variable credentials, looked up in the same layers as {{credentials}}.
GET {{baseUrl}}/search?q={{ urlEncode(searchTerm) }}
Authorization: Basic {{ base64Encode(basicCredentials) }}

Here basicCredentials holds user:password in the environment, and searchTerm may contain spaces or symbols.

Calls cannot be nested and there are no operators: {{ base64Encode(urlEncode("x")) }} and {{ randomInt(1, 10) + 1 }} are not valid. Use a pre-request script for anything more involved.

Errors

An unknown function, the wrong number of arguments, an argument of the wrong type (a string where a number is expected) or an argument that names an unknown variable all behave like an unknown variable: the whole {{ … }} marker is sent exactly as written and the response shows a warning with the reason. A template function never stops a request.

The same functions in scripts

Every function except oauthToken() is also available in scripts as util.<name>(): util.uuid(), util.now(), util.base64Encode("text") and so on. They are the same implementation, so they behave identically and their error messages are word for word the same. In scripts, util.now() and util.randomInt() return numbers, the rest return strings. See the Script API.

Found a mistake? Edit this page on GitHub or open an issue.

↑↓to move ↵to open /or Ctrl Kto search from any page