Skip to main content

User variables Pro / Enterprise

User variables are a small, project-scoped key/value store that lets you carry state between operations, scripts, collections and templating.

A typical scenario is a multi-step workflow: you create an entity, then update it, then delete it. Instead of copying the new entity's ID around manually, you store it once in a user variable and reference it from the other operations:

// In the script of the "create" operation
kreya.rest.onCallCompleted(call => {
kreya.variables.set('entity_id', call.response.content.id);
});
In the request body / URL / header of the update and delete operations
{{ vars.entity_id }}

Other common use cases are extracting a login token from a response and reusing it in authentication or subsequent requests, and sharing data between the operations of a collection run.

Setting and reading user variables

User variables are read and written from scripts via the kreya.variables object. It is available in both operation scripts and Kreya scripts.

// Store a value (any JSON-serializable value is allowed: string, number, boolean, object, array)
kreya.variables.set('user', { id: 42, name: 'Jane' });

// Read it back (returns undefined if the variable does not exist)
const user = kreya.variables.get('user');

// Check for existence
if (kreya.variables.has('user')) {
/* ... */
}

// List all variable names
const allKeys = kreya.variables.keys();

// Delete a single variable (passing undefined to set() does the same)
kreya.variables.delete('user');
kreya.variables.set('user', undefined);

// Delete all variables
kreya.variables.clear();
Valid keys

Variable keys (and the keys of nested objects) must start with a letter and may then only contain letters, digits and underscores (matching the regular expression ^[A-Za-z]\w*$).

Referencing user variables in templating

Anywhere templating is supported, user variables are exposed through the vars object. Given the value set above, you can reference it like any other templating value:

Hello {{ vars.user.name }} (id {{ vars.user.id }})

This makes user variables a convenient bridge between scripting and templating: a script computes or extracts a value, and templating consumes it in a request, header, endpoint, etc.

Scope and persistence

  • Scope: user variables are stored per Kreya project. Different projects do not share variables, and they are independent of the active environment.
  • Persistence: variables are kept for the whole session and are saved when Kreya exits, so they survive restarts. They are stored in Kreya's local application data, not inside the project files. They are therefore local to your machine and are not shared via version control or project storage.
  • Use environment data instead when a value is configuration that should be defined up front or shared with your team; use user variables for transient, runtime state.