at.environment

The at.environment namespace is how scripts read and write environment variables. Anything you set here is persisted to the active environment and is available in subsequent requests, both as the return value of at.environment.get() and as a {{varName}} placeholder in URLs, headers, bodies, query parameters, and auth fields.

Three Methods

  • at.environment.get(key): returns the value of the variable as a string, or undefined if it does not exist.
  • at.environment.set(key, value): writes a variable to the active environment. The value is coerced to a string. The change is visible to subsequent get() calls in the same script immediately, and is persisted to disk after the script finishes.
  • at.environment.has(key): returns true if the variable exists (either a pending write from this script, or an existing stored value), otherwise false.

Where Environment Variables Are Resolved

Variables stored via at.environment.set()(or defined from the Environment Manager) are referenced with the {{varName}} syntax and are resolved at request-build time in every field that supports variable interpolation:

  • 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: the expected value side of an assertion.

Which Environment at.environment Reads

The environment that a script reads from and writes to depends on where the script is running:

  • Standalone request (Box): the active global environment from the Environment Manager.
  • Project request: the active environment of the project that owns the request.
  • Flow step or Flow Before All / After All: the active environment of the flow group.
  • Load test step or Load Test Before All / After All: the active environment of the load test folder.

Switching the active environment from the top bar changes which variables these scripts see the next time they run.

Pre-Script → Post-Script Bridging

When a pre-script calls at.environment.set("foo", "bar"), the post-script for the same request can immediately read that value back with at.environment.get("foo")even though the write has not yet been persisted to disk. Atrahasis carries pre-script environment changes forward into the post-script as pending overrides, so the two scripts always agree on the state of the environment.

Example: Login Flow with Token Storage

Post-script on a login request:

// Extract tokens from a login response and save them to the environment.
if (at.response.status === 200) {
  const data = at.response.json();
  at.environment.set("auth_token", data.access_token);
  at.environment.set("refresh_token", data.refresh_token);
  at.environment.set("user_id", String(data.user.id));
  console.log("Tokens saved to environment");
}

// Subsequent requests can now use:
//   URL:    https://{{base_url}}/api/users/{{user_id}}
//   Header: Authorization: Bearer {{auth_token}}