How to: Import and export your mock API

A mock API’s stubs can be exported in bulk via the admin API. This can be useful for backing up your API to source control, or cloning the contents of one API into another.

To export (or backup) an API’s stubs, simply execute a GET request to the stub mappings admin URL e.g.:

curl -v --output my-backup-file.json \
  -H 'Authorization:Token my-api-token' \
  https://my-api.mocklab.io/__admin/mappings

To import (or restore) a collection of stubs, execute a POST request to the stub import URL e.g.:

curl -v -d @my-backup-file.json \
  -H 'Authorization:Token my-api-token' \
  https://my-target-api.mocklab.io/__admin/mappings/import

Changing import behaviour

By default, if the target API contains existing stubs with IDs matching those in the import, those stubs will be overwritten. Other existing stubs will be ignored.

If you wish to ignore existing stubs rather than overwriting add the following to the export JSON:

"importOptions": {
  "duplicatePolicy": "IGNORE"
}

If you wish to completely replace the contents of an API on import, including deleting any existing stubs that are not present in the import, then add the following to the export JSON:

"importOptions": {
  "deleteAllNotInImport": true
}

Exporting from a WireMock instance

WireMock has the same API as MockLab so exporting works very similarly. The main difference is that there’s no need to supply a security token, so you can simply make a request like this (substituting the correct port number):

curl -v --output my-backup-file.json http://localhost:8080/__admin/mappings

The produced file can then be imported into MockLab exactly as described above.