Managing multiple environments - dev, staging, and production, is a daily reality for modern developers. Manually updating URLs, tokens, or headers between these setups can quickly become a headache. That’s where Requestly’s Variables and Environments come to the rescue, making your workflow seamless and your requests error-free.
In this article, you’ll learn how to create and manage environments in Requestly, define and use variables, and switch setups with just a click plus, practical examples and tips for real-world usage.
Why Variables and Environments Matter
Imagine you’re building and testing APIs. Your base URL and tokens keep changing between dev, testing, and production. Editing every request or script each time isn’t just tedious, it’s risky. A single forgotten change can break deployments or leak data. Requestly solves this through a powerful variables and environments system: define once, use everywhere, and switch instantly.
Getting Started: What Are Variables and Environments in Requestly?
Environments are named sets of key-value pairs (like base URLs, API keys, tokens) tailored for different stages (Dev, Staging, Production).
Variables are reusable values (like {{BASE_URL}} or {{AUTH_TOKEN}}) used inside requests, headers, or scripts. When you switch environments, the variables are automatically resolved to the correct values, letting you reuse the same code or request templates everywhere.
Step-by-Step: Setting Up Environments in Requestly
- Open Requestly: 
 Log into your Requestly app or web dashboard. Head to the "Environments" section usually found in the sidebar.
- 
Create Your Environments: 
 For each setup (like Dev, Staging, Production), create a new environment. Click “Add Environment,” name it (e.g., “Development”), and start adding variables, as example shown in the image.Repeat for “Staging” and “Production” with their respective values. 
- 
Switching Environments: 
 With environments set up, you can switch between them during development with a single click. All your requests will automatically pull the correct variable values. No more manual edits!This image shows the variable value set for dev environment. And here just by switching the environment, the value for variable updated automatically. 
Using Variables in Requests
Syntax:
Reference variables using double curly braces, e.g., {{BASE_URL}}.
Example:
Suppose your GET request URL is  {{BASE_URL}}/users/me
You don’t need to hardcode the domain, just switch environments, and the request will target the correct API endpoint.
You can also use variables in headers, bodies, or scripts:
- 
Headers:
Authorization: Bearer {{AUTH_TOKEN}}
- JSON Body:
  { "env": "{{CUSTOM_HEADER}}" }
Advanced Use: Variables in Pre-Request & Post-Response Scripts
Requestly allows running scripts before sending a request or after receiving a response. You can use variables here for advanced automation.
Pre-request script example:
// Set a timestamp variable before request
rq.environment.set('TIMESTAMP', new Date().toISOString());
Use in URL/Body:
Add {{TIMESTAMP}} in your request URL or body, it evaluates to the current ISO timestamp every time.
Post-response script example:
Automatically update a token after login:
// Save JWT from login response
const data = JSON.parse(response.body);
rq.environment.set('AUTH_TOKEN', data.token);
Now, future requests will use the freshly updated token!
Switching Environments Without Hassle
To move from dev to production, just select the new environment. All references to {{BASE_URL}}, {{AUTH_TOKEN}}, etc., update instantly, no more tedious manual search and replace in every request or script.
This ensures:
- Fewer human errors
- More consistent testing
- Safer code migration between stages
Real-World Example: Multi-Environment API Collection
Suppose you have a collection of endpoints for user authentication.
Define variables:
- BASE_URL
- LOGIN_ROUTE
- AUTH_TOKEN
Environment: “Dev”
- 
BASE_URL=https://dev-api.myapp.com
- 
LOGIN_ROUTE=/login
Request:
POST {{BASE_URL}}{{LOGIN_ROUTE}}
Authorization: Bearer {{AUTH_TOKEN}}
Switch to “Production,” and your request now hits the live endpoint automatically.
Pro Tips for Managing Requestly Variables
- Group variables logically: Keep all API-related variables together for quick editing.
- Always avoid hard-coding sensitive values instead store them as environment variables.
Final Thoughts
Requestly’s Variables and Environments strip away the repetitive pain of environment management. Once set up, your requests become portable, safer, and easier to maintain, making you more productive.
If you haven’t already, experiment hands-on with your own setups. Try switching environments, automate token handling in scripts, and share your best tips with the community!
If you found this guide useful, share it on Twitter, LinkedIn, Dev.to, or your favorite developer communities.
This was my experience with Requestly- learning about variables and scripts. Do you have any helpful tips around Requestly? Comment or connect!
Ready to streamline your dev workflow? Start using Variables and Environments in Requestly today!
 




 
    
Top comments (0)