> ## Documentation Index
> Fetch the complete documentation index at: https://docs.requestly.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Global Variables (rq.globals)

> Complete reference for the rq.globals object in Requestly scripts to manage global variables accessible across all collections and environments.

The `rq.globals` object provides methods to manage global variables during script execution. Global variables work similarly to environment variables, but they are available to all collections and requests across all environments. This makes them ideal for storing truly global configuration or state that needs to be shared everywhere.

## Methods

### `rq.globals.set(key, value)`

Sets a global variable with the given key and value. If the variable already exists, it will be updated with the new value.

**Parameters:**

* `key` (string): The name of the global variable
* `value` (any): The value to store (will be converted to string)

**Example:**

```jsx theme={null}
rq.globals.set("appVersion", "1.0.0");
```

### `rq.globals.get(key)`

Retrieves the value of the specified global variable.

**Parameters:**

* `key` (string): The name of the global variable to retrieve

**Returns:** The value of the global variable, or `undefined` if it doesn't exist.

**Example:**

```jsx theme={null}
const version = rq.globals.get("appVersion");
console.log("App Version:", version);
```

### `rq.globals.unset(key)`

Removes the specified global variable.

**Parameters:**

* `key` (string): The name of the global variable to remove

**Example:**

```jsx theme={null}
rq.globals.unset("appVersion");
```

## Common Use Cases

### Store Application Version

Keep track of the application or API version being tested:

```jsx theme={null}
// In setup script
rq.globals.set("appVersion", "1.0.0");
rq.globals.set("apiVersion", "v2");

// In any request
const version = rq.globals.get("appVersion");
console.log("Testing with app version:", version);
```

### Track Global State

Maintain state that needs to be shared across all collections:

```jsx theme={null}
// Track total requests made
const totalRequests = parseInt(rq.globals.get("totalRequests") || "0");
rq.globals.set("totalRequests", totalRequests + 1);
console.log("Total requests made:", totalRequests + 1);
```

### Store User Preferences

Keep user preferences that apply globally:

```jsx theme={null}
rq.globals.set("preferredLanguage", "en");
rq.globals.set("dateFormat", "YYYY-MM-DD");
rq.globals.set("timezone", "UTC");
```

### Global Timestamp

Set a global timestamp that all requests can reference:

```jsx theme={null}
// Set once at the beginning of a test run
rq.globals.set("testRunStartTime", Date.now());

// Reference in any request
const startTime = rq.globals.get("testRunStartTime");
const elapsed = Date.now() - parseInt(startTime);
console.log("Time since test start:", elapsed, "ms");
```

### Feature Flags

Implement global feature flags:

```jsx theme={null}
// Enable/disable features globally
rq.globals.set("enableLogging", "true");
rq.globals.set("enableDebugMode", "false");
rq.globals.set("useNewEndpoint", "true");

// Check flags in any request
const debugMode = rq.globals.get("enableDebugMode") === "true";
if (debugMode) {
    console.log("Debug mode is enabled");
}
```

### Store Common Constants

Define constants that are used across all collections:

```jsx theme={null}
rq.globals.set("MAX_PAGE_SIZE", "100");
rq.globals.set("DEFAULT_PAGE_SIZE", "20");
rq.globals.set("API_KEY_HEADER", "X-API-Key");
```

## Variable Scope Hierarchy

Understanding the hierarchy of variables in Requestly:

1. **Global Variables** (`rq.globals`) - Highest level, available everywhere
2. **Collection Variables** (`rq.collectionVariables`) - Available to all requests in a collection
3. **Environment Variables** (`rq.environment`) - Available to all collections in a specific environment

When a variable with the same name exists at multiple levels, the most specific scope takes precedence:

```
Environment Variables > Collection Variables > Global Variables
```

## Best Practices

1. **Use Sparingly**: Reserve global variables for truly global data. Prefer collection or environment variables when possible.

2. **Naming Convention**: Use clear, uppercase names for global constants
   ```jsx theme={null}
   rq.globals.set("MAX_RETRY_ATTEMPTS", "5");
   rq.globals.set("DEFAULT_TIMEOUT_MS", "30000");
   ```

3. **Document Purpose**: Comment your global variable usage
   ```jsx theme={null}
   // Global feature flag for new API version
   rq.globals.set("useV2Api", "true");
   ```

4. **Check Existence**: Always verify a variable exists before using it
   ```jsx theme={null}
   const value = rq.globals.get("myGlobalVar");
   if (!value) {
       console.error("Global variable 'myGlobalVar' not set");
   }
   ```

5. **Initialize Early**: Set global variables in a dedicated setup script or request

6. **Clean Up**: Remove globals that are no longer needed
   ```jsx theme={null}
   rq.globals.unset("temporaryFlag");
   ```

7. **Type Conversion**: Remember values are stored as strings
   ```jsx theme={null}
   const maxRetries = parseInt(rq.globals.get("maxRetries") || "3");
   const enableFeature = rq.globals.get("enableFeature") === "true";
   ```

## Related Documentation

* [Pre-request & Post-response Scripts](/api-client/scripts)
* [rq.request Object](/api-client/rq-api-reference/rq-request)
* [rq.response Object](/api-client/rq-api-reference/rq-response)
* [rq.environment Object](/api-client/rq-api-reference/rq-environment)
* [rq.collectionVariables Object](/api-client/rq-api-reference/rq-collection-variables)
* [rq.vault Object](/api-client/rq-api-reference/rq-vault)
* [rq.test Object](/api-client/rq-api-reference/rq-test)
* [rq.expect Object](/api-client/rq-api-reference/rq-expect)
