The Environment/setGlobalVariablesInEnvironments extension updates Virtuoso environment variables by calling the Virtuoso Environments API. It reads the current variables for an environment, compares them by variable name, and either updates an existing variable or creates a new one.
This is useful when test data such as usernames, passwords, email addresses, reference numbers, or reusable environment-level values need to be prepared automatically before a journey runs.
Parameters:
-
tokenInputrequired, a string containing a valid Virtuoso API bearer token used to authenticate the GET, POST, and PUT API requests; -
environmentIDrequired, a string or number identifying the Virtuoso environment where variables should be created or updated; -
inputArrayObjectrequired, a JSON string representing an array of variable objects. Each object should include at least anameandvalueproperty, for example[{"name":"email","value":"virtuoso@qa.com"}];
Note: The inputArrayObject value must be valid JSON that parses to an array. The extension automatically adds hidden: false and file: false to each variable object before creating or updating it.
NLP usage
Use the extension in a journey by calling Environment/setGlobalVariablesInEnvironments with the execute command. Pass each value to the matching extension input using as inputName.
Note: This extension performs API calls asynchronously. The source code calls processArray(dataArray) but does not explicitly return or await that final promise, so journey timing may depend on the Virtuoso extension runner handling asynchronous execution correctly.
execute "Environment/setGlobalVariablesInEnvironments" using "$setTestDataToken" as tokenInput, "1882" as environmentID, '[{"name":"email","value":"virtuoso@qa.com"},{"name":"loginPassword","value":"Virtuoso"}]' as inputArrayObject returning $responseexecute "Environment/setGlobalVariablesInEnvironments" using "$apiToken" as tokenInput, "$environmentId" as environmentID, "$environmentVariables" as inputArrayObject returning $responseVariables can be prepared first and then passed into the extension call.
store value '[{"name":"email","value":"virtuoso@qa.com"},{"name":"loginPassword","value":"Virtuoso"}]' in $environmentVariables
execute "Environment/setGlobalVariablesInEnvironments" using "$setTestDataToken" as tokenInput, "1882" as environmentID, "$environmentVariables" as inputArrayObject returning $responsestore value "virtuoso@qa.com" in $email
store value "Virtuoso" in $password
store value '[{"name":"email","value":"virtuoso@qa.com"},{"name":"loginPassword","value":"Virtuoso"}]' in $object
execute "Environment/setGlobalVariablesInEnvironments" using "$setTestDataToken" as tokenInput, "1882" as environmentID, "$object" as inputArrayObject returning $responseExample output when the extension completes successfully:
The environment variables are created or updated through the Virtuoso API. API responses are logged to the browser console.This extension requires the following resources:
The extension should be configured as:
- Run asynchronously: No
- Scope: Global
Limitation: This extension is tied to the https://api.virtuoso.qa/api/environments API base URL in the source code, so it will only work for accounts and environments available through that API domain. The token must have permission to read and modify the target environment, and API availability, authentication, rate limits, and backend validation can affect execution. The input must be a JSON array; a single object is not handled unless wrapped in an array. Variables are matched only by name, and the update endpoint uses the existing variable name as the path key, so renamed variables or names containing unsupported URL path characters may fail unless the API accepts them exactly. The code also starts processArray(dataArray) without returning or awaiting it at the top level, so the final returned value may not contain the API response even though the API calls are performed.
Add the extension to your Virtuoso instance
Select the domain that matches your Virtuoso account.
View source
Last updated: 25/05/2026
Resources:
// Note this extension is not a product feature of Virtuoso and is not officially supported
// Extensions use javascript which may or may not be compatible with systems under test (SUTs)
// We welcome you to use this extension or adapt it for your needs
//INSTRUCTIONS:
/*
Example Usage:
Store value ${[ {"name": "email", "value": "virtuoso@qa.com"}, {"name": "loginPassword", "value": "Virtuoso"}]} in $object
or
Store value ${[ {"name": "email", "value": $email}, {"name": "loginPassword", "value": $password}]} in $object
Navigate to "https://app.virtuoso.qa" in new tab
setGlobalVariableInEnviroment($setTestDataToken, "1882", $object)
===================================================================================================================================================
PARAMS: Token, Environment Id & single object/array of objects
Summary:
This JavaScript code interacts with an API to manage environment variables. It fetches existing variables, processes an input array, and either updates or adds variables to the API. The code ensures variable completeness with default values and uses Axios for HTTP requests (GET, POST, PUT). The operations depend on specified API endpoints, authentication token, and environment ID. The commented-out section suggests an alternative approach without checking for existing variables.
*/
const dataArray = JSON.parse(inputArrayObject);
let baseUrl = 'https://api.virtuoso.qa/api/environments';
const token = tokenInput;
if (!token) {
throw new Error('API token is required. Please provide a valid API token.');
}
const environmentId = environmentID;
if (!environmentId) {
throw new Error('Environment ID is required. Please provide a valid environment ID.');
}
const getEnv = `${baseUrl}/${environmentId}`;
const createEnv = `${baseUrl}/${environmentId}/variables?envelope=false`;
async function getDataFromApi() {
try {
const response = await axios.get(getEnv, {
headers: {
'Authorization': `Bearer ${token}`
}
});
return response.data.item.variables;
} catch (error) {
console.error('Error getting data from API:', error.message);
throw error;
}
}
async function postDataToApi(data) {
try {
const response = await axios.post(createEnv, data, {
headers: {
'Authorization': `Bearer ${token}`
}
});
console.log('API Response:', response.data);
return response.data;
} catch (error) {
console.error('Error posting data to API:', error.message);
throw error;
}
}
function completeObjectDefaults(obj) {
return {
...obj,
hidden: false,
file: false
};
}
/*
async function processArray(array) {
const existingVariables = await getDataFromApi();
const completedArray = array.map(completeObjectDefaults);
for (const obj of completedArray) {
const existingVariable = Object.values(existingVariables).find(
variable => variable.name === obj.name //&& variable.value === obj.value
);
if (!existingVariable) {
await postDataToApi(obj);
}
}
}
processArray(dataArray);
*/
async function processArray(array) {
const existingVariables = await getDataFromApi();
const completedArray = array.map(completeObjectDefaults);
for (const obj of completedArray) {
const existingVariable = Object.values(existingVariables).find(
variable => variable.name === obj.name
);
if (existingVariable) {
await updateDataToApi(existingVariable.name, obj);
} else {
await postDataToApi(obj);
}
}
}
async function updateDataToApi(key, data) {
try {
const updateEnv = `${baseUrl}/${environmentId}/variables/${key}`;
const response = await axios.put(updateEnv, data, {
headers: {
'Authorization': `Bearer ${token}`
}
});
console.log('API Response:', response.data);
return response.data;
} catch (error) {
console.error('Error updating data to API:', error.message);
throw error;
}
}
processArray(dataArray);
Comments
0 comments
Please sign in to leave a comment.