Environments
Environments allow you to run the same API tests against different servers (development, staging, production) without modifying requests.
Coming Soon
This feature is currently in development.
Environment Basics
What is an Environment?
An environment is a set of variables that define the context for API requests:
| Environment | Base URL | Purpose |
|---|---|---|
| Development | http://localhost:3000 | Local testing |
| Staging | https://staging-api.example.com | Pre-production |
| Production | https://api.example.com | Live system |
Variable Types
| Type | Scope | Use Case |
|---|---|---|
| Environment | Per environment | URLs, API keys |
| Collection | Per collection | Shared values |
| Global | All collections | Common settings |
| Local | Current session | Temporary values |
Managing Environments
Creating Environments
- Navigate to Environments
- Click New Environment
- Add variables:
- Variable name
- Initial value
- Current value
- Type (text, secret)
Environment Structure
{
"name": "Staging",
"values": [
{
"key": "baseUrl",
"value": "https://staging-api.example.com",
"type": "text"
},
{
"key": "apiKey",
"value": "stg_key_xxxxx",
"type": "secret"
},
{
"key": "timeout",
"value": "5000",
"type": "text"
}
]
}
Using Variables
In Requests
Reference variables with double curly braces:
URL: {{baseUrl}}/api/v1/users
Header: Authorization: Bearer {{accessToken}}
Body: {"email": "{{testEmail}}"}
In Scripts
Access and modify variables programmatically:
// Get variable
const baseUrl = pm.environment.get('baseUrl');
// Set variable
pm.environment.set('accessToken', response.token);
// Clear variable
pm.environment.unset('tempValue');
Variable Resolution
Priority order (highest to lowest):
- Local variables
- Collection variables
- Environment variables
- Global variables
Secret Management
Sensitive Data
Handle secrets securely:
| Practice | Description |
|---|---|
| Type: Secret | Mask values in UI |
| No Export | Exclude from exports |
| Vault Integration | External secret storage |
| Rotation | Regular key rotation |
Best Practices
- Never commit secrets to version control
- Use secret type for sensitive values
- Rotate credentials regularly
- Use different keys per environment
Environment Switching
Quick Switch
Toggle between environments:
- Dropdown in header
- Keyboard shortcut
- Script-based switching
Dynamic Environments
Switch environments programmatically:
// Set active environment based on condition
if (pm.variables.get('region') === 'eu') {
pm.environment.set('baseUrl', 'https://eu-api.example.com');
} else {
pm.environment.set('baseUrl', 'https://us-api.example.com');
}
Common Variables
Standard Variables
| Variable | Example | Purpose |
|---|---|---|
baseUrl | https://api.example.com | API base URL |
apiVersion | v1 | API version |
apiKey | key_xxxxx | API authentication |
accessToken | jwt_token | Bearer token |
timeout | 5000 | Request timeout |
Dynamic Variables
Built-in dynamic values:
| Variable | Description | Example |
|---|---|---|
{{$timestamp}} | Current timestamp | 1699876543 |
{{$randomInt}} | Random integer | 42 |
{{$guid}} | Random UUID | a1b2c3d4-... |
{{$randomEmail}} | Random email | [email protected] |
Environment Templates
Creating Templates
Define reusable environment structures:
{
"template": "Standard API",
"variables": [
{ "key": "baseUrl", "required": true },
{ "key": "apiKey", "required": true, "type": "secret" },
{ "key": "timeout", "default": "5000" }
]
}
Sharing Templates
- Export template structure
- Share with team
- Create environments from templates
CI/CD Integration
Environment Files
Use environment files in pipelines:
# GitHub Actions example
- name: Run API Tests
run: smartapi run collection.json -e staging.json
env:
API_KEY: ${{ secrets.STAGING_API_KEY }}
Environment Variables
Map CI/CD secrets to SmartAPI variables:
| CI/CD Variable | SmartAPI Variable |
|---|---|
$API_KEY | {{apiKey}} |
$BASE_URL | {{baseUrl}} |
$AUTH_TOKEN | {{accessToken}} |
Best Practices
Naming Conventions
- Use camelCase for variable names
- Prefix environment-specific vars
- Document variable purposes
Security
- Never use production secrets locally
- Rotate keys regularly
- Audit environment access
Maintenance
- Review unused variables
- Keep environments in sync
- Document environment purposes