Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
"general/api-client/environments-and-variables/collection-and-subcollection-variables",
"general/api-client/environments-and-variables/global-variables",
"general/api-client/environments-and-variables/runtime-variables",
"general/api-client/environments-and-variables/dynamic-variables",
"general/api-client/environments-and-variables/variable-precedence",
"general/api-client/environments-and-variables/using-variables-in-api-requests"
]
Expand Down
1,297 changes: 1,297 additions & 0 deletions general/api-client/environments-and-variables/dynamic-variables.mdx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ Variable precedence in Requestly defines how a variable value is resolved when t

## Precedence order

> Runtime Variables → Environment Variables → SubCollection Variables → Collection Variables → Global Variables
> Runtime Variables → Environment Variables → SubCollection Variables → Collection Variables → Global Variables → Dynamic Variables

<Note>
**Dynamic variables** are built-in variables (like `{{$timestamp}}`, `{{$randomUUID}}`, etc.) that have the lowest precedence. If you define a custom variable with the same name, your custom value will be used.
</Note>

## How it works

Expand All @@ -35,11 +39,17 @@ if (variable_name in runtime_variables) { // Check runtime
return collection_variables[variable_name];
} else if (variable_name in global_variables) { // Check global
return global_variables[variable_name];
} else if (is_dynamic_variable(variable_name)) { // Check dynamic variables
return generate_dynamic_value(variable_name); // e.g., $timestamp, $randomUUID
} else {
return null; // Variable not found in any scope
}
```

<Note>
When using the `$` prefix (e.g., `{{$timestamp}}`), the system skips user-defined variable lookups and directly generates the dynamic value.
</Note>

## Example scenario

Assume the variable `{{base_url}}` is defined in multiple scopes:
Expand All @@ -56,4 +66,36 @@ If you send a request inside a **SubCollection** with an active **Environment**,

`https://runtime.api.com`

If the runtime variable is removed, the value falls back to the **environment variable**, followed by SubCollection, Collection, and finally Global based on availability.
If the runtime variable is removed, the value falls back to the **environment variable**, followed by SubCollection, Collection, and finally Global based on availability.

## Dynamic variables precedence example

Assume you want to use a timestamp in your request:

**Scenario 1: Using `{{timestamp}}`** (without `$` prefix)

| Scope | Value (if defined) |
| ------------- | ---------------------------- |
| Environment | `"2024-01-15"` |
| Dynamic | Current timestamp (e.g., `1613360320`) |

Result: `{{timestamp}}` resolves to `"2024-01-15"` (your custom environment variable)

**Scenario 2: Using `{{$timestamp}}`** (with `$` prefix)

Result: `{{$timestamp}}` **always** resolves to the current Unix timestamp (e.g., `1613360320`), regardless of whether you have a custom `timestamp` variable defined.

**Best Practice:**

```javascript
// In pre-request script
// This uses your custom variable if defined, otherwise falls back to dynamic
const myTimestamp = "{{timestamp}}";

// This always generates a fresh dynamic timestamp
const freshTimestamp = rq.$timestamp();
```

<Tip>
Use the `$` prefix (`{{$variableName}}` or `rq.$variableName()`) when you explicitly want to use a dynamic variable, even if a custom variable with the same name exists.
</Tip>
40 changes: 36 additions & 4 deletions general/api-client/scripts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,44 @@ console.log(`Weather in ${city}: ${temp}°C`);

----

### Variable Scope Hierarchy
### Using Dynamic Variables in Scripts

When variables with the same name exist at multiple levels, the priority is:
Requestly provides dynamic variables, which are built in values that automatically generate common data such as timestamps, UUIDs, and random values. You can access them in scripts using the rq.$variableName() syntax.

**Environment Variables > Collection Variables > Global Variables**
**Quick Example:**
```jsx
const uniqueId = rq.$randomUUID();
const timestamp = rq.$timestamp();
const email = rq.$randomEmail();

console.log("Generated User ID:", uniqueId);
console.log("Request Timestamp:", timestamp);
```

**Common Dynamic Variables:**
- `rq.$randomUUID()` - Generate unique identifiers
- `rq.$timestamp()` - Current Unix timestamp
- `rq.$isoTimestamp()` - ISO 8601 timestamp
- `rq.$randomInt()` - Random integer
- `rq.$randomEmail()` - Random email address
- `rq.$randomFirstName()` - Random first name
- `rq.$randomCompanyName()` - Random company name

**With Arguments:**
```jsx
// Generate random integer between 1-100
const age = rq.$randomInt(1, 100);

// Generate alphanumeric string of length 10
const code = rq.$randomAlphaNumeric(10);

// Generate password with specific length
const password = rq.$randomPassword("20");

// Generate email with custom name
const email = rq.$randomEmail("John", "Doe");
```

This allows you to override global or collection defaults with environment-specific values.
[View complete Dynamic Variables documentation →](/general/api-client/environments-and-variables/dynamic-variables)


Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.