Environments
Introduction to Variables and Environments
Before we delve into the concept of environments, it is important to know what a variable is. A variable is a reusable value that one can use in API requests. If you want an easier way to change variable values based on work context, you can make use of the environment and its variables.
In the API world, variables are like the details you pass back and forth between your code and the API. They serve as placeholders for information such as user IDs, authentication tokens, or data you want to send or receive. Variables act as messengers, carrying specific details needed for your code to talk effectively with the API. In Sparrow, you can define variables as key-value pairs. Simply enter the variable name and its value.
You will be able to add variables under two types of environments in the Sparrow application – Global Environment and Local Environment. With environments, you can create different sets of variables for various types of work. For example, environment variables are useful when you might want to use different values while sending API requests to a development server or a production server.
When working with APIs, an environment is like the testing ground for your code and the API to interact. It defines where your code and the API will meet, specifying things like server locations, authentication details, and other settings. Think of it as the virtual space where your code communicates with the API.
Global Environment
The global environment is the environment in which you can set and make variables available regardless of which environment is active.
Add a Global Environment Variable
You can add a global environment variable by following these steps:
- Navigate to the Collections tab in the sidebar. Select Environments from the left panel and choose Global Variables.
-
Enter the variable name and the value associated with the variable name.
-
Once provided, click on the Save button at the top for the variable and the corresponding value to be saved.
-
You can add as many variables and values as you desire. Once you start entering a variable and its value, a blank field is automatically created at the bottom for more entries.
-
You can delete the variable-value pair by clicking the delete icon next to the entry.
- Suppose if the variable list is too long, you can use the search area to find environment variables that match your criteria.
Local Environment
In addition to the global environment variables, you can create local environment variables. You can use global variables anywhere within your workspace, regardless of which environment is selected. However, to use a variable that is defined under an environment, you need to ensure that the environment is selected.
Add Local Environment
- To create a new local environment, click the + Add Environment button.
- A new environment will be created in which you can enter the variable and the values corresponding to the variables.
-
Enter the variable name and the value associated with the variable name.
-
Once the variable and the value are provided, click on Save button available at the top, for the variable and the corresponding value to be saved against the variables.
-
You can add variables and values as you desire. Once you have started entering a variable and its value, a blank field is created at the bottom automatically.
-
You can delete the variable-value pair by clicking the delete icon provided beside the same. The usage of the local environment variable-value is like the global environment, but it will be restricted for that environment only.
Add More Local Environment Options
If you right click on the “+” button present in front of environment, then you will be able to add more local environments
More Local Environment Options
If you right-click on the "three dots" icon next to a local environment, you will be provided with four options:
- Open Environment: Open the environment details if closed.
- Rename: Rename the environment.
- Select Environment: Select the environment.
- Delete: Delete the environment variables and their values.
How to Use Variables Help
If you need help using variables, you can click the link “See how it works” in the global variables section. This will provide a detailed view of variables, environments, and their usage.
Using Variables in API Requests
Once you’ve defined the environment and variable details, you can use them in API requests.
To use variables in the REST API tool, start typing curly brackets and Sparrow will show all the defined variables. Global variables are marked with "G," and local variables are marked with "E."
Variables can be used in URLs, parameters, headers, authorization fields, or anywhere else in a REST API request.
- Suppose if the variable you are intending to be referred is not available in the list of variables under the global environment or the selected environment, then you can just type the variable you would want to add post brackets. You would be provided a prompt to add the variable as the same is not present in the selected/ global environment.
-
Click Add Variable and then provide the details of the value against the variable which you would want to add. Suppose if you want to add it as a global variable then select the toggle switch to add the same as a global variable.
-
Click Add & Apply once you are adding to the respective environment (Toggle off) or the global environment (Toggle on).
The variable and the value would be added to the respective environment in case you click Add & Apply directly and the details would be displayed in the environment tab. If you select the Global Variable toggle, then it would be added as a global variable.
Switching Environments
To switch environments for API testing, select the dropdown from the collections list in the side panel of the REST API tool.