Scripting API
Tigrister provides the tg scripting API for interacting with requests, responses, environment variables, and flow variables inside Pre-Scripts and Post-Scripts. The tg object is the primary interface for all script automation — from modifying requests on-the-fly to extracting tokens and sharing data across flow steps.
The tg Object
The tg object has four namespaces, each serving a distinct purpose:
tg.environment
Read and write environment variables. Changes are persisted immediately and available in subsequent requests. Variables set here are referenced as {{varName}} in URL, headers, body, params, and auth fields.
tg.flow
Read and write flow-scoped variables for cross-step data sharing. Only available inside Flow Runner execution. Variables set here are referenced as {{flow.varName}} in URL, headers, body, and assertions.
tg.request
Access and modify the HTTP request. In pre-scripts, the request is mutable — you can change the URL, headers, and body. In post-scripts, the request is read-only.
tg.response
Access the HTTP response data. Only available in post-scripts. Provides status, headers, body, timing information, and a cached json() parser.
tg.environment
Read and write environment variables from scripts. Changes made with tg.environment.set() are persisted immediately to the active environment and available in subsequent requests.
| Method | Description |
|---|---|
| tg.environment.get(key) | Returns the value of the variable, or undefined if not found |
| tg.environment.set(key, value) | Sets a variable in the active environment. Persisted immediately. |
| tg.environment.has(key) | Returns true if the variable exists |
Using Environment Variables
Variables stored via tg.environment.set() (or defined in the Environment Manager) are referenced with {{varName}} syntax. They are resolved at send time in:
https://{{base_url}}/api/usersAuthorization: Bearer {{token}}{"api_key": "{{api_key}}"}key: {{api_key}}expected: {{expected_status}}Environment Context
The environment that tg.environment reads from and writes to depends on the execution context:
| Context | Reads From | Persists To |
|---|---|---|
| Standalone request | Active global environment | Active global environment |
| Project request | Active project environment | Active project environment |
| Flow step | Active flow group environment | Active flow group environment |
Example: Login Flow with Token Storage
Post-script on a login request:
// Extract token from login response and save to environment
if (tg.response.status === 200) {
const data = tg.response.json();
tg.environment.set('auth_token', data.access_token);
tg.environment.set('refresh_token', data.refresh_token);
tg.environment.set('user_id', String(data.user.id));
console.log('Tokens saved to environment');
}
// Now subsequent requests can use:
// URL: https://{{base_url}}/api/users/{{user_id}}
// Header: Authorization: Bearer {{auth_token}}tg.environment.set() within a script are immediately visible to tg.environment.get() in the same script. They are also persisted to disk, so they survive app restarts.tg.request
Access and modify the HTTP request. Behavior differs between pre-scripts and post-scripts:
Pre-Script (Mutable)
All properties are readable. url and body are writable. Headers are modified via setHeader() and removeHeader(). Mutations are applied to the actual request before sending.
Post-Script (Read-Only)
All properties are readable but writes are silently ignored. Useful for inspecting the request that was sent.
| Property / Method | Type | Pre-Script | Post-Script |
|---|---|---|---|
| tg.request.url | string | read / write | read-only |
| tg.request.method | string | read-only | read-only |
| tg.request.headers | object | read (copy) | read (copy) |
| tg.request.body | string | undefined | read / write | read-only |
| tg.request.setHeader(key, value) | void | adds/updates header | not available |
| tg.request.removeHeader(key) | void | removes header | not available |
Example: Dynamic Request Modification
Pre-script:
// Add authentication header from environment
const token = tg.environment.get('auth_token');
if (token) {
tg.request.setHeader('Authorization', 'Bearer ' + token);
}
// Add a unique request ID
tg.request.setHeader('X-Request-Id', crypto.randomUUID());
// Modify URL based on environment
const baseUrl = tg.environment.get('base_url');
if (baseUrl) {
tg.request.url = baseUrl + '/api/v2/users';
}
// Remove a debug header before sending
tg.request.removeHeader('X-Debug');tg.request.headers returns a copy of the headers object. To modify headers, always use setHeader() and removeHeader(). Direct assignment to the copy has no effect.tg.response
Access HTTP response data in post-scripts. In pre-scripts, tg.response is an empty object (the response hasn't been received yet).
| Property / Method | Type | Description |
|---|---|---|
| tg.response.status | number | HTTP status code (e.g., 200, 404) |
| tg.response.statusText | string | HTTP status text (e.g., "OK", "Not Found") |
| tg.response.headers | object | Response headers (copy) |
| tg.response.body | string | Response body as raw string |
| tg.response.json() | any | Parse body as JSON. Result is cached — safe to call multiple times. |
| tg.response.timing | object | undefined | Timing breakdown: total, dns, tls, ttfb (all in ms) |
Example: Response Inspection and Token Extraction
Post-script:
// Inspect response
console.log('Status:', tg.response.status, tg.response.statusText);
// Parse JSON body (cached — call multiple times safely)
const data = tg.response.json();
// Save extracted values to environment
if (tg.response.status === 200) {
tg.environment.set('user_id', String(data.id));
tg.environment.set('session_token', data.token);
}
// Check performance
const timing = tg.response.timing;
if (timing && timing.total > 2000) {
console.warn('Slow response:', timing.total, 'ms');
}tg.response.json() instead of JSON.parse(tg.response.body). The result is cached internally, so calling json() multiple times returns the same parsed object without re-parsing.tg.flow
Read and write flow-scoped variables for cross-step data sharing inside Flow Runner. Unlike environment variables, flow variables are ephemeral — they exist only for the current flow iteration and are not persisted to disk.
| Method | Description |
|---|---|
| tg.flow.get(key) | Returns the value of a flow variable, or undefined if not found |
| tg.flow.set(key, value) | Sets a flow variable for the current iteration |
| tg.flow.has(key) | Returns true if the flow variable exists |
Using Flow Variables
Variables stored via tg.flow.set() are referenced with {{flow.varName}} syntax. They are injected into subsequent steps at execution time in:
{{base_url}}/users/{{flow.userId}}Authorization: Bearer {{flow.token}}{"user": "{{flow.userId}}"}expected: {{flow.expectedId}}Flow Variables vs Environment Variables
| tg.environment | tg.flow | |
|---|---|---|
| Syntax | {{varName}} | {{flow.varName}} |
| Persisted | Yes — saved to disk, survives restarts | No — ephemeral, cleared after iteration |
| Scope | Global, project, or flow group (context-dependent) | Single flow iteration only |
| Availability | All scripts (standalone, project, flow) | Flow Runner scripts only |
| Use case | Tokens, config values, persistent state | Step-to-step data passing (IDs, tokens, temp values) |
Example: Multi-Step Flow
A 3-step flow: Create User → Get User → Delete User
const data = tg.response.json();
tg.flow.set('userId', String(data.id));
tg.flow.set('username', data.username);
console.log('Created user:', data.id);{{base_url}}/api/users/{{flow.userId}}body.username equals {{flow.username}}{{base_url}}/api/users/{{flow.userId}}tg.flow is only available inside Flow Runner execution. Calling it from a standalone or project request script throws an error: "tg.flow is only available inside flow execution".Additional Script Globals
In addition to the tg object, the following globals are available in all scripts:
| Global | Description |
|---|---|
| console.log() / info() / warn() / error() | Log messages to the Script Output panel. Total output is capped at 1000 characters. |
| fetch(url, options) | Make HTTP requests from within scripts (async). Uses the browser's native fetch API. |
| setTimeout(fn, ms) | Delay execution within a script. |
Variable Resolution Order
When a flow step is executed, variables are resolved in a specific order:
- {{varName}} — Environment variables resolved first
Checks the active environment (live envContext) → falls back to static environment variables
- {{flow.varName}} — Flow variables resolved second
Checks the flow store (tg.flow.set() values) → falls back to extracted variables from previous steps
Example: Mixed Variables
// URL with both env and flow variables:
// {{base_url}}/api/users/{{flow.userId}}/posts
//
// Resolution:
// {{base_url}} → from tg.environment → "https://api.example.com"
// {{flow.userId}} → from tg.flow → "42"
//
// Result: https://api.example.com/api/users/42/posts{{missing_var}}). This helps you identify missing variables in the response.