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.

MethodDescription
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:

URL
https://{{base_url}}/api/users
Headers
Authorization: Bearer {{token}}
Request Body
{"api_key": "{{api_key}}"}
Query Parameters
key: {{api_key}}
Authentication
Bearer token, Basic Auth, API Key fields
Flow Assertions
expected: {{expected_status}}

Environment Context

The environment that tg.environment reads from and writes to depends on the execution context:

ContextReads FromPersists To
Standalone requestActive global environmentActive global environment
Project requestActive project environmentActive project environment
Flow stepActive flow group environmentActive 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}}
Note: Variables set with 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 / MethodTypePre-ScriptPost-Script
tg.request.urlstringread / writeread-only
tg.request.methodstringread-onlyread-only
tg.request.headersobjectread (copy)read (copy)
tg.request.bodystring | undefinedread / writeread-only
tg.request.setHeader(key, value)voidadds/updates headernot available
tg.request.removeHeader(key)voidremoves headernot 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');
Note: Reading 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 / MethodTypeDescription
tg.response.statusnumberHTTP status code (e.g., 200, 404)
tg.response.statusTextstringHTTP status text (e.g., "OK", "Not Found")
tg.response.headersobjectResponse headers (copy)
tg.response.bodystringResponse body as raw string
tg.response.json()anyParse body as JSON. Result is cached — safe to call multiple times.
tg.response.timingobject | undefinedTiming 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');
}
Tip: Always use 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.

MethodDescription
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:

URL
{{base_url}}/users/{{flow.userId}}
Headers
Authorization: Bearer {{flow.token}}
Request Body
{"user": "{{flow.userId}}"}
Assertions (expected value)
expected: {{flow.expectedId}}

Flow Variables vs Environment Variables

tg.environmenttg.flow
Syntax{{varName}}{{flow.varName}}
PersistedYes — saved to disk, survives restartsNo — ephemeral, cleared after iteration
ScopeGlobal, project, or flow group (context-dependent)Single flow iteration only
AvailabilityAll scripts (standalone, project, flow)Flow Runner scripts only
Use caseTokens, config values, persistent stateStep-to-step data passing (IDs, tokens, temp values)

Example: Multi-Step Flow

A 3-step flow: Create User → Get User → Delete User

Step 1: Create User (post-script)
const data = tg.response.json();
tg.flow.set('userId', String(data.id));
tg.flow.set('username', data.username);
console.log('Created user:', data.id);
Step 2: Get User
URL:
{{base_url}}/api/users/{{flow.userId}}
Assertion:
body.username equals {{flow.username}}
Step 3: Delete User
URL:
{{base_url}}/api/users/{{flow.userId}}
Important: 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:

GlobalDescription
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.
Timeout: All scripts have a 30-second execution limit. If a script exceeds this limit, it is terminated and an error is reported.

Variable Resolution Order

When a flow step is executed, variables are resolved in a specific order:

  1. {{varName}} — Environment variables resolved first

    Checks the active environment (live envContext) → falls back to static environment variables

  2. {{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
Unresolved variables: If a variable is not found in any source, the placeholder remains as-is (e.g., {{missing_var}}). This helps you identify missing variables in the response.