Understand variable scopes, precedence, and advanced usage patterns in Bruno
Bruno provides a powerful variable system with multiple scopes and precedence levels. Understanding how variables work is essential for building dynamic, maintainable API collections.
// Set environment variablebru.setEnvVar('apiToken', 'abc123');// Set persistent variable (saved to disk)bru.setEnvVar('refreshToken', 'xyz789', { persist: true });// Get environment variableconst token = bru.getEnvVar('apiToken');// Check if variable existsif (bru.hasEnvVar('apiToken')) { console.log('Token is set');}// Delete environment variablebru.deleteEnvVar('apiToken');// Get all environment variablesconst allVars = bru.getAllEnvVars();// Clear all environment variablesbru.deleteAllEnvVars();
Global environment variables are shared across all environments, useful for values that don’t change between dev/staging/production.
// Set global environment variablebru.setGlobalEnvVar('apiVersion', 'v2');bru.setGlobalEnvVar('baseUrl', 'https://api.example.com');// Get global environment variableconst version = bru.getGlobalEnvVar('apiVersion');// Delete global environment variablebru.deleteGlobalEnvVar('apiVersion');// Get all global environment variablesconst allGlobals = bru.getAllGlobalEnvVars();// Clear all global environment variablesbru.deleteAllGlobalEnvVars();
Runtime variables are temporary and exist only during request execution. They have the highest precedence and are perfect for passing data between requests.
// In post-response scriptconst response = res.getBody();bru.setVar('authToken', response.token);bru.setVar('userId', response.user.id);
2
Request 2: Get Profile
// In pre-request scriptconst token = bru.getVar('authToken');req.setHeader('Authorization', `Bearer ${token}`);// In URL// GET /users/{{userId}}/profile
3
Request 3: Update Profile
// Runtime variables available across entire executionconst userId = bru.getVar('userId');const body = req.getBody();body.userId = userId;req.setBody(body);
// Get process environment variableconst nodeEnv = bru.getProcessEnv('NODE_ENV');const home = bru.getProcessEnv('HOME');// Use in interpolation// {{process.env.API_KEY}}
// Extract and store resource IDsconst response = res.getBody();// Store newly created resource IDbru.setVar('createdUserId', response.user.id);bru.setVar('createdOrderId', response.order.id);// Use in subsequent requestsreq.setUrl(`{{baseUrl}}/users/${bru.getVar('createdUserId')}`);
Environment Detection
// Adapt behavior based on environmentconst env = bru.getEnvName();const isProduction = env === 'production';// Different timeouts per environmentconst timeout = isProduction ? 5000 : 30000;req.setTimeout(timeout);// Different loggingif (!isProduction) { console.log('Request details:', req.getUrl(), req.getHeaders());}
// Delete sensitive variables after usebru.deleteVar('password');bru.deleteVar('creditCardNumber');bru.deleteVar('ssn');
4
Document Variable Usage
Add comments explaining variable purposes and expected values:
// OAuth2 access token - expires in 1 hourbru.setEnvVar('accessToken', response.access_token);// User ID for currently authenticated userbru.setVar('currentUserId', response.user_id);