If you need to set up a new environment with the same environment variables as an existing environment, you can use the mabl API. Compared to copying variables manually in the mabl app, using the mabl API is faster and reduces the risk of human error.
Before you start
Gather the following information before starting this task:
- “Command Line Interface” API key - workspace owners can create and access API keys in the mabl app: Settings > APIs.
- Workspace ID - found in Settings > Workspace in the mabl app
The examples in this article use cURL, but you can use whichever method you’re most comfortable with. Just make sure to set the authentication method to Basic Auth and pass in the required mabl API key according to your tool’s setup.
Get environment IDs
Send a request to the query environments endpoint to get the following IDs:
- Source environment ID - the ID of the environment you want to copy environment variables from
- Destination environment ID - the ID of the environment you want to copy environment variables to
To use the following cURL command, replace the query parameter {workspace_id}
with your workspace ID and replace {api_key}
with your API key.
curl --request GET \ --url 'https://api.mabl.com/environments?workspace_id={workspace_id}' \ --user 'key:{api_key}'
The query environments endpoint returns an array of objects with information about the environments in the workspace. Environment IDs end in -e
. Copy the environment IDs for the source environment and the destination environment
Get environment variables from the source environment
Send a request to the get environment endpoint to get environment details about the source environment. To include environment variables in the response, set the query parameter decrypt
to true
.
To use the following cURL command, replace {source_env_id}
with the ID of the environment you want to copy variables from and update {api_key}
to your actual API key.
curl --request GET \ --url 'https://api.mabl.com/environments/{source_env_id}?decrypt=true' \ --user 'key:{api_key}'
Here is an example of a successful response:
{ "workspace_id": "{workspace_id}", "created_time": 1637783279217, "variables": { "TEST1234": "abcdefg", "SAMPLE_ENV_VAR": "example1", "ANOTHER_VAR": "sample1234" }, "use_link_agent": false, "last_updated_time": 1729866085345, "link_bypass_mabl_proxy": false, "name": "prod", "page_load_wait": "NORMAL", "last_updated_by_id": "{user_id}", "id": "{environment_id}", "created_by_id": "{user_id}" }
Copy the "variables"
object from the response:
"variables": { "TEST1234": "abcdefg", "SAMPLE_ENV_VAR": "example1", "ANOTHER_VAR": "sample1234" }
Update the destination environment
Send a request to the update environment endpoint for the destination environment, and add the environment variables to the body of the request.
When you use the update environment endpoint to update environment variables, you override any existing variables for that environment.
To use the following cURL command, replace the following values:
-
{destination_env_id}
- use the ID of the environment you want to copy variables to -
{api_key}
- use your actual API key - In the
--data
flag, replace the example with the variables object that you copied from the response of the previous request. If you are using a different tool, add the variables object as a JSON request body.
curl --request PATCH \ --url https://api.mabl.com/environments/{destination_env_id} \ --header 'accept: application/json' \ --user 'key:{api_key}' \ --header 'content-type: application/json' \ --data ' { "variables": { "TEST1234": "abcdefg", "SAMPLE_ENV_VAR": "example1", "ANOTHER_VAR": "sample5678" } } '
If you are copying over a lot of variables, you can store the object in a JSON file and send it with the request. In the following example, the --data
flag sends the information in the variables.json
file.
curl --request PATCH \ --url https://api.mabl.com/environments/{destination_env_id} \ --header 'accept: application/json' \ --user 'key:{api_key}' \ --header 'content-type: application/json' \ --data @variables.json
To confirm that the environment variables copied over to the destination environment, send another request to the get environment endpoint to get environment details about the destination environment. Don’t forget to set the query parameter decrypt
to true
.
To use the following cURL command, replace {destination_env_id}
with the ID of the updated environment and replace {api_key}
with your actual API key.
curl --request GET \ --url 'https://api.mabl.com/environments/{destination_env_id}?decrypt=true' \ --user 'key:{api_key}'