Skip to content

Keep a project's configuration in a repository

Before you begin

Plan
Included in Free, Pro and Team. How plans and limits work
Role
Viewer or higher to pull and compare, Editor or higher to push mocks and scenarios and Admin or higher to push settings, chaos profiles or webhooks.
You need
  • Node.js 22.19 or newer
  • A machine token, or a personal token

The MockHub CLI, @mockhub/cli, keeps a project’s configuration document in a mockhub.yaml file next to your code, so changes to mocks are reviewed in pull requests like any other change. It is the same document as Edit a project’s configuration in the browser, written as YAML.

Install it once, globally:

Terminal window
npm install --global @mockhub/cli
mockhub --version

To pin the version a repository uses, install it there instead, and run it as npx mockhub:

Terminal window
npm install --save-dev --save-exact @mockhub/cli
npx mockhub --help
  1. Create a machine token for the project, with Sync a project from a repository under What is it for?. Copy it when it is shown.

  2. In a terminal, give the CLI the token:

    Terminal window
    export MOCKHUB_TOKEN=mh_mt_…
  3. In the repository, connect the project:

    Terminal window
    mockhub init

    The CLI writes mockhub.yaml and pulls the project’s configuration into it. It prints Wrote …mockhub.yaml with environment "orders-api" (orders-api).: the target is named after the project’s subdomain. When the token reaches several projects, it asks which one; --project answers without a terminal, and --env chooses another target name.

  4. Check that the file and the project agree:

    Terminal window
    mockhub diff

    It prints No changes. The project matches the file.

  5. Edit mockhub.yaml, for example to add a mock:

    mocks:
    - name: GET /orders
    request: { method: GET, urlPath: /orders }
    responses:
    - key: ok
    name: OK
    response:
    statusCode: 200
    headers: { Content-Type: application/json }
    body: '{"items":[]}'
    responseSelection: { mode: default, defaultResponseKey: ok }

    Name the body’s type in headers: a JSON body without Content-Type reaches the caller as text/plain.

  6. Run mockhub diff again to see what pushing would change, then apply it:

    Terminal window
    mockhub push

    If the change deletes anything, the CLI names each deletion and asks before applying.

push prints what it created, changed and deleted, and the project’s address answers with the new mocks from the next request. Commit mockhub.yaml; the token stays out of it.

  • The file is the whole truth. A mock, scenario, chaos profile or webhook the file does not mention is deleted by push, and a setting it leaves out goes back to its default. Names are identity, so renaming a mock deletes it and creates a new one. Configuration document describes every field.

  • A file that lists nothing deletes nothing. A file without any mocks, scenarios, chaosProfiles or webhooks key, such as one mockhub init --no-pull writes, holds only its connection. push refuses to delete anything from it, even with --yes, and diff warns. Run mockhub pull to fill it, or write mocks: [] to delete the project’s mocks on purpose. If init cannot pull, it leaves no file behind.

  • pull starts from the project. It replaces the file’s configuration with what the project serves now, and keeps the keys only the CLI reads: apiUrl, environments, workspace and seed.

  • Push applies the plan you saw. push previews first, then applies exactly that preview. If someone changes the project in between, nothing is applied, and the CLI says so:

    The project changed while the plan was being reviewed; nothing was applied. Re-run `mockhub diff` to see the new plan, then push again.
  • Secrets stay out. Webhook signing secrets and access keys are never in the file, and the token comes from MOCKHUB_TOKEN or --token.

A file can drive several projects, such as staging and production, each a target under environments. A target’s settings are merged over the top-level ones:

version: 3
openapi: null
environments:
staging:
project: 0198f6a2-0000-7000-8000-000000000001
production:
project: 0198f6a2-0000-7000-8000-000000000002
settings:
logUnmatchedRequests: false
mocks:
- name: GET /orders
request: { method: GET, urlPath: /orders }
responses:
- key: ok
name: OK
response:
statusCode: 200
headers: { Content-Type: application/json }
body: '{"items":[]}'
responseSelection: { mode: default, defaultResponseKey: ok }

Name the target on every command, as mockhub diff --env staging. With one target, --env can be left out. Run mockhub init in a directory that already has mockhub.yaml to add another target; it keeps the rest of the file and its comments, and pulls nothing.

${env:NAME} in any string value is replaced with that environment variable when the CLI runs, so a value that differs between machines, or should not be committed, stays out of the file:

settings:
gatewayMode: proxy-fallback
upstreamUrl: ${env:UPSTREAM_URL}

A missing variable stops the command before anything is sent: Missing environment variable(s) for ${env:...} interpolation: UPSTREAM_URL. pull writes the project’s actual values into the configuration it pulls; placeholders survive only in apiUrl and environments.

A mock lists its named responses and a responseSelection. key is a response’s identity: it stays the same when the response is renamed or edited, and a default and a sequence’s steps name it. pull writes keys an import derived as they are, starting oa-; keep them, and choose your own for responses you add, without that prefix. A sequence names keys in order:

mocks:
- name: POST /orders
request: { method: POST, urlPath: /orders }
responses:
- key: unavailable
name: Unavailable
response: { statusCode: 503 }
- key: created
name: Order created
response: { statusCode: 201, headers: { Content-Type: application/json }, body: '{"id":1}' }
responseSelection: { mode: sequence, steps: [unavailable, unavailable, created], onEnd: stick }

The selector the app shows for a response, the value of X-MockHub-Response, is derived from the mock’s identifier and never in the file, so the same file applied to another project works there.

mockhub mock default changes which response answers without editing the file:

Terminal window
mockhub mock default "POST /orders" invalid-email

It takes the mock by name or identifier and the response by key, reads the mock, and sends only the new responseSelection over the version it read. An edit made meanwhile is refused (the command exits with 2) instead of overwritten. It changes no response, no other mock and not the mock’s activation, and it prints the X-MockHub-Response value that asks for that response on one request. The command does not wait for the change to reach the project’s address, so a request sent a moment after may still get the old default. pull brings the file in line. A mock that answers with a sequence has no default.

pull always writes version: 3, with openapi: null for a project without a contract. A project with an OpenAPI contract has it under openapi, and each imported mock carries openapiOperation and openapiGeneration, which also lists where each response came from:

version: 3
openapi:
document: |
openapi: 3.1.0
info: { title: Orders, version: 1.0.0 }
…
importOptions: { … }
  • Leave openapi as pull wrote it. The CLI sends openapi.document and each mock’s openapiGeneration exactly as written, and the project checks them. To change the contract, use mockhub openapi import or Update a specification, then pull.

  • openapi: null removes the contract, its revisions and every mock’s link to it. push counts it as a deletion and asks first.

  • A literal ${env:NAME} needs one more dollar. In the file, $${env:NAME} is sent as the text ${env:NAME}, and $$${env:NAME} as $${env:NAME}. pull adds the dollar to everything the project sends, so a mock whose body contains ${env:TOKEN} pushes back unchanged:

    mocks:
    - name: GET /config
    request: { method: GET, urlPath: /config }
    responses:
    - key: ok
    name: OK
    response:
    statusCode: 200
    headers: { Content-Type: application/json }
    body: '{"token": "$${env:TOKEN}"}' # served as {"token": "${env:TOKEN}"}
    responseSelection: { mode: default, defaultResponseKey: ok }

    ${env:NAME} with one dollar is still replaced from the environment.

  • A file of an older version is refused, whatever the project holds. diff and push send it, and print the project’s answer:

    The server rejected the document:
    - version: Config version 2 is no longer read: a mock now lists named "responses" and a "responseSelection". Run `mockhub pull` to write version 3, and edit that file.

    There is no conversion: pull writes the current version.

  • The CLI talks to https://api.mockhub.io unless apiUrl or MOCKHUB_API_URL names another address. It refuses plain HTTP and follows no redirects.
  • There is no sign-in command. Every command needs a token in MOCKHUB_TOKEN or --token.
  • A personal token belongs to no workspace, so mockhub init with one asks which workspace, or takes --workspace.
  • Each request gives up after 30 seconds; MOCKHUB_TIMEOUT_MS changes that.
  • The file leaves out what the configuration document leaves out: the project’s name and address, alert rules, request logs, version history, which chaos profile is active and where each scenario stands.

Report a problem with this page