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:
npm install --global @mockhub/climockhub --versionTo pin the version a repository uses, install it there instead, and run it as npx mockhub:
npm install --save-dev --save-exact @mockhub/clinpx mockhub --help-
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.
-
In a terminal, give the CLI the token:
Terminal window export MOCKHUB_TOKEN=mh_mt_… -
In the repository, connect the project:
Terminal window mockhub initThe CLI writes
mockhub.yamland pulls the project’s configuration into it. It printsWrote …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;--projectanswers without a terminal, and--envchooses another target name. -
Check that the file and the project agree:
Terminal window mockhub diffIt prints
No changes. The project matches the file. -
Edit
mockhub.yaml, for example to add a mock:mocks:- name: GET /ordersrequest: { method: GET, urlPath: /orders }responses:- key: okname: OKresponse:statusCode: 200headers: { Content-Type: application/json }body: '{"items":[]}'responseSelection: { mode: default, defaultResponseKey: ok }Name the body’s type in
headers: a JSON body withoutContent-Typereaches the caller astext/plain. -
Run
mockhub diffagain to see what pushing would change, then apply it:Terminal window mockhub pushIf the change deletes anything, the CLI names each deletion and asks before applying.
Result
Section titled “Result”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.
How the file works
Section titled “How the file works”-
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,chaosProfilesorwebhookskey, such as onemockhub init --no-pullwrites, holds only its connection.pushrefuses to delete anything from it, even with--yes, anddiffwarns. Runmockhub pullto fill it, or writemocks: []to delete the project’s mocks on purpose. Ifinitcannot pull, it leaves no file behind. -
pullstarts 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,workspaceandseed. -
Push applies the plan you saw.
pushpreviews 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_TOKENor--token.
Several projects from one file
Section titled “Several projects from one file”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: 3openapi: nullenvironments: 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.
Values from the environment
Section titled “Values from the environment”${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.
Several responses in one mock
Section titled “Several responses in one mock”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.
Make a response the default
Section titled “Make a response the default”mockhub mock default changes which response answers without editing the file:
mockhub mock default "POST /orders" invalid-emailIt 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.
Projects with an OpenAPI contract
Section titled “Projects with an OpenAPI contract”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: 3openapi: document: | openapi: 3.1.0 info: { title: Orders, version: 1.0.0 } … importOptions: { … }-
Leave
openapiaspullwrote it. The CLI sendsopenapi.documentand each mock’sopenapiGenerationexactly as written, and the project checks them. To change the contract, usemockhub openapi importor Update a specification, thenpull. -
openapi: nullremoves the contract, its revisions and every mock’s link to it.pushcounts 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}.pulladds the dollar to everything the project sends, so a mock whose body contains${env:TOKEN}pushes back unchanged:mocks:- name: GET /configrequest: { method: GET, urlPath: /config }responses:- key: okname: OKresponse:statusCode: 200headers: { 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.
diffandpushsend 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:
pullwrites the current version.
Limits
Section titled “Limits”- The CLI talks to
https://api.mockhub.iounlessapiUrlorMOCKHUB_API_URLnames another address. It refuses plain HTTP and follows no redirects. - There is no sign-in command. Every command needs a token in
MOCKHUB_TOKENor--token. - A personal token belongs to no workspace, so
mockhub initwith one asks which workspace, or takes--workspace. - Each request gives up after 30 seconds;
MOCKHUB_TIMEOUT_MSchanges 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.