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.
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:
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. Fill in the appropriate values and save this file before running the scripts.
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:
This will use the Runscope API token you have specified to authenticate and retrieve the selected environment from the selected bucket using the endpoint:
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:
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.
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!