Environment Variables in API Testing: One Test for Every Server
Environment variables in API testing let one test run on every server: what goes where, which value wins, how to pass a token on, and where secrets stay.
Environment variables in API testing are names that stand for the values that change between servers, such as an address or a password. The test keeps the names, each environment gives the values, and one test runs everywhere.
Without them, teams copy. There is a test for the test server, a copy for staging, and another one on a colleague's computer. Someone adds a check to one of them. Months later, a bug goes through staging, because that copy never got the check.
What are environment variables in API testing?
Definition
In API testing, an environment variable is a name written in the test in place of a value that depends on where the test runs.
- The name is the same everywhere, such as baseUrl.
- Its value is one address on your computer and another on staging.
Change the value, and the same test talks to another server.
The words have a second meaning.Every computer can hold environment variables of its own, which its programs read.Pipelines use them to hand a password to a run.In this article, they are one of the places a value can come from.
One step of one test
GET {{baseUrl}}/orders
Your computer
baseUrl
http://localhost:8080/
The test server
baseUrl
https://test.example.com/
Staging, in the pipeline
baseUrl
https://staging.example.com/
Different test environments, and one test too many
Many teams have several servers: a developer's own, a shared test server, staging, sometimes one for demonstrations. They run the same app with different settings. The steps of a test are the same on all of them, and so are its checks.
Software teams met this problem long ago.The well-known guide to building online services, The Twelve-Factor App, asks them to keep their settings apart from the code.
"An app's config is everything that is likely to vary between deploys (staging, production, developer environments, etc)."
A test deserves the same care. Its steps and its checks are what the team decided. An address and a password are settings. Keep them apart, and there is one test to read, review and correct.
What goes where: a place for each value
Two questions find the right place. Is the value a secret? Does it exist before the run, or does the run create it?
| Value | Its name in the test | Where to keep it |
|---|---|---|
| The server's address | {{baseUrl}} | In one file per environment, and in the test itself for your usual server |
| A test user's name | {{user}} | In the same places as the address |
| A password or an API key | {{loginPassword}} | Under This computer on your desk, and as a secret parameter in the pipeline |
| A token | {{apiToken}} | Nowhere: the sign-in step saves a fresh one at each run |
| An order's id | {{orderId}} | Nowhere: the step that creates the order saves it |
| A reference that must be unique | {{$uuid}} | Nowhere: a new one is made at every use |
When your API signs callers in with OAuth 2.0, the token is fetched for you.See how to test an API with OAuth 2.0.
Which value wins? The five places, in order
GUI Test Studio automates end-to-end tests of any app without code, and its Call API step sends requests to an API. When a step names a variable, the run looks in five places, always in this order. The first value found is used.
- 1Saved by an earlier stepNothingNo value here
- 2The file given to the runhttps://staging.example.com/apiUsed
- 3The environment variable GTS_VAR_baseUrlNothingNot asked
- 4This computer's variableshttp://localhost:8080/apiNot asked
- 5The test's own variableshttps://test.example.com/apiNot asked
The order is what makes it useful.
- The test carries a value that works for everyone, so a new colleague can run it at once.
- You can replace that value on your own computer, and nobody else is affected.
- A pipeline replaces both, with the file it gives to the run.
- And what a step saved during the run comes before them all, because it is the newest.
Variables in the URL and in the request body
In a step, a variable is its name between double braces.It can be a part of the address, as in {{baseUrl}}/orders/{{orderId}}.It can also be a value of the body, a header, the authorization, or the value a check expects.
Values made new at every use
Some data must be new at each run, or the second run fails with "this email already exists". These built-in names make a new value at every use.
| Name | What it gives | Good for |
|---|---|---|
{{$uuid}} | A unique identifier | An order's reference |
{{$randomText}} | Random letters | The first part of an email address |
{{$randomInt}} | A random whole number | A customer number |
{{$timestamp}} | The present moment, as a number | A name that says when it was made |
{{$today}} | Today's date | An order's date |
How to pass a token from one API to another
This is called chaining. One call gives a value, and the next call needs it. Copying it by hand works once, until the token ends.
1Sign in
POST {{baseUrl}}/
login Uses
userloginPassword
Saves
apiToken
2Create an order
POST {{baseUrl}}/
orders Uses
apiToken
Saves
orderId
3Read it back
GET {{baseUrl}}/
orders/ {{orderId}} Uses
apiTokenorderId
Saves
Nothing: it only checks
- 1Send the sign-in request and click the token in the real answer.
- 2Click "Save as Variable" and name it apiToken.
- 3In the next step, give {{apiToken}} as the Bearer token.
- 4Click "Run the Steps Before This One", so that Send has a real token to use.
A value is saved only when its call passed, so a failed sign-in never hands on a wrong token.Ids work the same way: API testing without code follows one order from its creation to its removal.In GraphQL, the saved id goes into the query's own variables, as GraphQL testing shows.
Variable scope: how long does a value live?
The scope of a variable is where its value can be used, and for how long. It follows from where the value comes from.
| Where the value comes from | Who can use it | How long it lasts |
|---|---|---|
| Saved by a step | The later steps of the same run | Until that run of the test ends |
| The file given to the run | Every test of that command | That command |
| GTS_VAR_ followed by its name | Every run started where it is set, such as a pipeline job | As long as it is set |
| This computer's variables | The tests you run on this computer, with your Windows account | Until you change it |
| The test's own variables | That test, on every computer | As long as the test holds it |
| A built-in name, such as {{$uuid}} | The place where it is written | One use |
The first row is there on purpose. A test cannot use a value another test saved. Each test prepares what it needs, so it passes or fails on its own, in any order.
How to run one API test on several environments, step by step
- 1
List what changes
Write down every value that differs between your servers: the address, the test user, the password, a key.
- 2
Name the ordinary values
Open Settings, then Variables..., and add
baseUrlanduserunder This test, with the values of your usual test server. - 3
Keep the secrets on your computer
In the same window, add
loginPasswordunder This computer.It never enters the test file. - 4
Use the names in the steps
In each Call API step, replace the server's address with its name, such as
{{baseUrl}}/orders.Do the same for the user and the password. - 5
Save what a step creates
Send each request, click the value a later step needs, such as the order's id, then Save as Variable.
- 6
Write one file per environment
Put one value per line in
staging.env, such asbaseUrl = https://staging.example.com/api.Leave every secret out of it. - 7
Run the test with a file
Run
gtr-cli run tests --variables staging.env, with each secret set as an environment variable whose name starts withGTS_VAR_.
Secrets in tests: the name in the file, the value elsewhere
The same Twelve-Factor page has a question worth asking about your tests.It asks if the code could be made public at any moment, "without compromising any credentials".Could you send your tests to a supplier today without giving away a password?
With names in place of secrets, the answer is yes.
- On your computer, the value is under "This computer", encrypted for your Windows account.
- In the pipeline, it is a secret parameter, such as GTS_VAR_loginPassword.
- In the report, the console, the files and the bug report, it reads (hidden).
- A name such as apiToken, password or clientSecret is hidden by itself, and for any other name you tick "Secret".
Secrets are one part of what a test can reveal.The people and the orders on the screen are the other.See data privacy in test automation.
What else reads a variable, and what does not
A variable is not only for API calls. An end-to-end test often prepares its data through the API, then works in the app. The same names link the two.
| Step | Does it read variables? | Example |
|---|---|---|
| Open Web App | Yes, in its address | {{appUrl}}/orders opens the app of the right environment |
| Text Assert | Yes, in its expected text | Order {{orderId}} must be on the screen |
| Run JavaScript | Yes, in its script | Open the order {{orderId}} in the page |
| Run Python Script, or a program Run App runs until it ends | It reads the values earlier steps saved | orderId is the environment variable GTS_VAR_orderId |
| Type Text | No, it types its own words | The words you recorded or wrote in the step |
A Call API creates an order and saves its number as orderId.Open Web App opens {{appUrl}}/orders.A Text Assert expects Order {{orderId}}.It looks for that very order, not for any order in the list.
API testing vs UI testing says when to use each one and why one test often needs both.Functional and end-to-end testing places such a test among the others.
Variables or baseline profiles?
GUI Test Studio has another way to run one test under several conditions. The two do different jobs, and a run can use both.
| Question | Variables | Baseline profiles |
|---|---|---|
| What changes | The values the steps use | How the app looks, and what a step does in that variation |
| Examples | Test server, staging, another test user | Dark mode, French, a 4K monitor |
| How a run chooses | --variables staging.env | --baseline-profile "Dark mode" |
| The words a Type Text types | Not read from a variable | Can differ from one profile to another |
In short, a server is a variable, and a look is a profile.The guide to baseline profiles has the settings, and dark mode testing a full example.
Multiple test environments in your pipeline
Each environment gets one line in the pipeline. The tests are the same files, and only the file of values changes.
baseUrl = https://staging.example.com/api
appUrl = https://staging.example.com
user = qa-testergtr-cli run tests --variables test.env --junit results\test.xml
gtr-cli run tests --variables staging.env --junit results\staging.xmlA forgotten value is found before it costs you a run. GUI Test Runner-Recorder looks at the variables your Call API and Open Web App steps use. Before the run, it names the ones that have no value. You can give them one, or run anyway.
gtr-cli does not run a test with a missing value, unless you add --ignore-missing-target.Run tests in CI/CD has every option, and this TeamCity example works in any pipeline tool.
API test automation best practices for several environments
Do
- Use the same names everywhere, and change only their values.
- Give each test a value that works for your usual server, so it runs on a new computer at once.
- Sign in during the run, and save a fresh token each time.
- Create the data a test needs at its start, and make its names unique.
- Review the files of values with your team, as you review the tests.
Avoid
- Keeping one copy of a test per server.
- Writing a password or a key in a test, or in a file of values.
- Pasting a token that will end tomorrow.
- Relying on an order another test created.
- Choosing a server by editing the steps.
Your team decides what the test must check, and reads it on the timeline.An AI assistant can add a step that uses your names.It can be Claude, ChatGPT with Codex, or a local AI model, one we provide or one you bring: see Create a test from a prompt.The GTS Engine runs the saved test the same way on every server.
For the checks themselves, take the tables of API test cases you can copy.They work on every server when the address is a name.
Key takeaways
- 1One test, many servers: the test keeps the names, and each environment gives the values.
- 2Five places, one order: an earlier step, the run's file, the pipeline's environment variable, your computer, the test itself.
- 3A run creates its own values: save the token and the order's id in one step, and use them in the next.
- 4Secrets stay outside: the file holds their names, and every result reads (hidden).
- 5A server is a variable, a look is a profile: use each for its own job.
Questions
What are environment variables in API testing?
They are names that stand for the values that change from one environment to another, such as the server's address or a password. The test holds the names, and each environment gives the values. So one test runs everywhere.
How do I run the same API test on several environments?
Replace what changes with variables, write one small file of values per environment, and give a file to each run. With GUI Test Studio's command line, that is gtr-cli run tests --variables staging.env.
How do I pass a token from one API call to the next?
Save it from the answer of the first call under a name, such as apiToken, and write that name between double braces in the next call. In GUI Test Studio, click the token in the real answer, then Save as Variable.
Which value is used when a variable is set in several places?
In GUI Test Studio, the first one found in this order: saved by an earlier step, the file given to the run, an environment variable named GTS_VAR_ followed by the name, this computer's variables, the test's own variables.
Where should passwords and API keys for tests be kept?
Outside the test file. On your computer, keep them under Variables, This computer, where they are encrypted for your Windows account. In a pipeline, keep them as secret parameters. The test holds only their names.
Can I use a variable in the URL and in the request body?
Yes, in both. A variable can be a part of the address, such as its first part or an order's id. In a body, it can be any value. A Call API step also takes variables in its headers, its authorization and the values its checks expect.
How long does a saved variable last?
Until the end of that run of the test. The next test, and the next run of the same test, start without it. So each test creates or asks for what it needs, and no test depends on the one before it.
Can I run API tests against production?
Never run a test that creates, changes or deletes data on real production data. If you test production at all, use a separate test that only reads, with an account made for that job.


