Runscope API Monitoring    Learn More →

Copying Runscope Environments using the Runscope API

By Sam Aybar on .

The Runscope UI provides a lot of flexibility for our customers to monitor and test APIs. You can easily manage the tests you have written, whether that means exporting them, duplicating them, or moving them to a different bucket.

Sometimes, however, our customers want to do something that we don’t have built-in. This is where the benefits of having a robust API come into play. For example, one customer recently expressed interest in being able to export an environment for backup purposes; another customer wanted to copy a shared environment to a different bucket.

Runscope UI, showing an API test editor page with the environment section expanded. Includes two commonly used initial variables, baseUrl and apiKey.

Environments are one of the key elements in creating reusable API tests, especially when it comes to testing local, staging, and production APIs. This blog post outlines two example Python scripts to address these needs of importing/exporting environments. You can find these scripts at Runscope/runscope-api-examples. Feel free to fork/modify them to fit your needs.

Getting Started with Runscope API

The first step in working with the Runscope API is to get a Runscope API Token. The fastest way to do this is to go to the Applications page in your account and select “Create Application”. Since you are creating it for personal use (as opposed to creating an application for third- party use), you can fill in any URL for the website URL and callback URL. Once you have filled in the form, click Create Application. You can grab your Personal Access Token from the bottom of the following page to use in the scripts:

Runscope Application detail page, showing the personal access token a user can find at the bottom of the page, to quickly make requests to the Runscope API.

Configuring the Scripts to Run

Next, we have to get the scripts that will do the heavy lifting for us. You can download the ZIP from the GitHub repository, or do a git clone for:

git clone https://github.com/Runscope/runscope-api-examples.git

The first script -- runscope_export_env.py -- allows you to export a Runscope Environment to a JSON file. The second script -- runscope_import_env.py -- allows you to import a JSON file exported by the first script.

To run the Python scripts, there is a config file (runscope_config.py) that drives both of the scripts:

master_bucket_key = 'BUCKET_KEY_OF_SHARED_ENVIRONMENT_TO_EXPORT'
master_env_id = 'ENVIRONMENT_ID_TO_EXPORT'
runscope_token = 'RUNSCOPE_PERSONAL_ACCESS_TOKEN'
runscope_dest_bucket = 'BUCKET_KEY_TO_WRITE_TO'

This configuration file will define the bucket you want to take the shared environment from, the shared environment you want to copy, the bucket you want to write the environment to, and your Runscope API token, as found above. You can find the Bucket Key in the URL for your bucket (or in the Bucket Settings page), and you can expand an environment to find its ID right next to its name:

3-runscope-bucket-key-env-id.png

Exporting a Runscope Shared Environment

The first script -- runscope_export_env.py -- allows you to export a Runscope Environment to a JSON file. This script leverages the environments endpoint, and it can be run with:

python runscope_export_env.py

This will use the Runscope API token you have specified to authenticate and retrieve the selected environment from the selected bucket using the endpoint:

GET /buckets/<bucket_key>/environments/<environment_id>

And then, it will save the results to a JSON file in the same directory as your script, with the same name as your environment.

You can save this file for future use, modify it if you want to change it before importing to a different bucket, or simply move on to the next step to import it to another bucket.

Importing a Runscope Shared Environment

To import the environment you have exported back to another bucket (specified in the config file), you can just run the second script with:

python runscope_import_env.py 'Your Environment Name.json'

Which will use the endpoint:

POST /buckets/<bucket_key>/environments

That will create a new shared environment in the bucket specified based on the JSON file previously saved. This environment will then be available to tests in this bucket.

Advanced Usage

In addition to copying shared environments between buckets, these scripts can also be used to work with test-specific environments or to overwrite a shared environment. To access these advanced features, check out the README for the command line flags you can use to override the defaults.

With the power of the Runscope API, we make it easy to extend our functionality on your own. If you have any suggestions on how we could improve those scripts, or other things you might want to do that we currently don't have built-in, feel free to open a PR in our api-examples repository, or reach out to our awesome support team!

Categories: code samples, howto, tutorial

Everything is going to be 200 OK®