Variables
Variables in JetClient scripts provide a way to store and manipulate data dynamically. Use them to modify request parameters, store temporary values, or persist values across multiple requests.
Accessing Variables
Use the jc.variables
object to access variables from any available scope:
// Get a variable
jc.variables.get("myVar")
// Check if variable exists
if (jc.variables.has("myVar")) {
console.log("Variable exists")
}
// Replace variable placeholders
let text = "Hello {{name}}!"
let replacedText = jc.variables.replaceIn(text)
Variable Types
Runtime Variables
Runtime variables exist only during script execution:
jc.variables.set("tempVar", "temporary value")
Global Variables
Global variables persist across all environments:
// Set global variable
jc.globals.set("globalVar", "persistent value")
// Remove global variable
jc.globals.unset("globalVar")
// Clear all global variables
jc.globals.clear()
Environment Variables
Environment variables are tied to the active environment. If multiple environment groups exist (e.g., ‘User’ in addition to ‘Default’), specifying the group ensures the variable is set in the active environment of that group:
// Set environment variable in the Default group
jc.environment.set("apiKey", "123456")
// Set environment variable in a specific group (e.g., 'User')
jc.environment.set("apiKey", "123456", "User")
// Remove environment variable from Default group
jc.environment.unset("apiKey")
// Remove environment variable from User group
jc.environment.unset("apiKey", "User")
// Clear environment variables in Default group
jc.environment.clear()
// Clear environment variables in User group
jc.environment.clear("User")
Folder Variables
Folder variables persist within a specific folder:
// Set folder variable
jc.folderVariables.set("folderVar", "folder value")
// Set environment-specific folder variable in Default group
jc.folderVariables.setEnv("folderEnvVar", "env value")
// Set environment-specific folder variable in User group
jc.folderVariables.setEnv("folderEnvVar", "env value", "User")
// Remove folder variable
jc.folderVariables.unset("folderVar")
// Remove folder variable from Default group
jc.folderVariables.unsetEnv("folderEnvVar")
// Remove folder variable from User group
jc.folderVariables.unsetEnv("folderEnvVar", "User")
// Clear all folder variables
jc.folderVariables.clear()
Collection Variables
Collection variables apply to the top-level folder (collection) and behave the same as folder variables. This is essentially an alias for folder variables at the highest level:
jc.collectionVariables.set("collectionVar", "collection value")
Script Variables
Script variables behave like folder variables but are specific to standalone scripts. These scripts can have their own variable sets, just like folders:
jc.scriptVariables.set("scriptVar", "script value")
Dynamic Values
JetClient supports built-in functions and expressions for generating dynamic values:
// Using dynamic functions
console.log(jc.variables.get("$uuid"))
// Using expressions
console.log(jc.variables.get("$randomFirstName + ' ' + $randomLastName"))
console.log(jc.variables.replaceIn("text {{$randomInt(1, 10) * 10}} text"))
For more details see Variables and Environments, Inline Expressions and Functions documentation.