Skip to content

A mock server from your spec, or from your own code

Publish a mock from any commit of your repository and call it at its own URL on APIGit Cloud. Spec-based mocks answer from OpenAPI documents and GraphQL schemas with no scripts. Code-based mocks run your Express.js scripts when responses need logic and state.

Spec-based mocks are on every plan, including Free. Nothing to host.

petstore-sandbox.mockserver.apigit.com
$ curl https://petstore-sandbox.mockserver.apigit.com/api/v3/pet/1 \    -H 'api_key: anything'{"id":10,"name":"doggie","category":{"id":1,"name":"Dogs"}, "photoUrls":["string"],"tags":[{"id":0,"name":"string"}],"status":"available"} $ curl -i https://petstore-sandbox.mockserver.apigit.com/api/v3/pet/1 \    -H 'api_key: anything' -H 'Prefer: code=404'HTTP/2 404content-type: application/json

Two types, one way to publish

Start spec-based for a working mock with no code. Publish a code-based mock when responses depend on earlier requests. A mock server is one type or the other, and one repository can publish several of each, even from the same commit.

PropertySpec-basedCode-based
SourceOpenAPI documents and complete GraphQL schemasYour scripts in apigit-mock/
You writeNothing beyond the documentJavaScript, on Express.js
ResponsesOpenAPI examples or generated bodies; GraphQL values shaped by the schemaWhatever your code returns
Request validationOpenAPI checks you can switch; GraphQL queries checked against the schemaYour own code
Stays in step with the specAlways: the document is the mockUp to you: regenerate or edit the scripts
State between requestsNoYes, in the state object
Several APIs on one mockYes, each document under its own base pathOne Express app
PlansFree, Team, EnterpriseTeam, Enterprise

Spec-based: the document is the mock

Pick the documents, deploy, and the mock is live. There is no generated code to maintain, so the mock cannot drift from the spec.

Answers from your OpenAPI document

Each request is matched to an operation. The answer is your example, or a body generated from the schema when there is none. The same document and the same request always give the same answer, so tests stay stable.

  • OpenAPI 3.0 and 3.1 in YAML or JSON, and Swagger 2.0
  • Split documents: local $ref files are collected with the document
  • Cross-origin requests from a browser work
app.apigit.com/acme/petstore-platform
Publish dialog of a spec-based mock server, with the documents to serve and a base path for each

Validation, and the response you ask for

The mock checks parameters, the request body and the presence of the credentials the document requires, and says exactly what is wrong. A Prefer header asks for a declared status, a named example or random realistic values, so your client's error handling gets tested too.

  • Switch each check per document, or per request
  • Errors are application/problem+json (RFC 9457), easy to tell from your API's own responses
  • The Prefer conventions of Prism, so tests written for it carry over
a request the document does not allow
$ curl https://petstore-sandbox.mockserver.apigit.com/api/v3/pet/abc \    -H 'api_key: anything'{  "type": "https://apigit.com/mock/problems/invalid-request",  "title": "The request does not match the API description",  "status": 422,  "detail": "The request does not match getPetById: path petId: must be integer",  "errors": [    { "in": "path", "name": "petId", "message": "must be integer" }  ]}

Several APIs on one mock server

Mount each document under its own base path, taken from its servers URL or set by you. Documents never share routes, so two APIs can both declare /pets. Add or remove documents, edit base paths and switch validation on the Setting page, without a republish.

  • The longest matching base path wins
  • One document on Free, up to 10 on Team and Enterprise
  • GET /apigit-mockserver-routes lists every document and operation
one mock server, three documents
https://petstore-sandbox.mockserver.apigit.com
├── /pets/v3    openapi/petstore.yaml
├── /orders/v2  openapi/orders.yaml
└── /graphql    schema.graphql
app.apigit.com/acme/petstore-platform/publish/mock/petstore-sandbox/setting
Documents and Base Paths on a mock server's Setting page, with a base path and validation switches for each document

GraphQL, from a schema file

Select a complete SDL schema as one more document of the mock. It answers queries and mutations at /graphql, validates each query against the schema and returns values in the shape you selected. Repeating a request gives the same values.

  • Introspection is supported
  • An argument that matches a returned field is echoed
  • Shares access control, quota and logs with the OpenAPI documents of the mock
catalog-sandbox.mockserver.apigit.com/graphql
$ curl https://catalog-sandbox.mockserver.apigit.com/graphql \    -H 'Content-Type: application/json' \    --data '{"query":"query Product($id: ID!) { product(id: $id) { id name price } }","variables":{"id":"42"},"operationName":"Product"}'{  "data": {    "product": {      "id": "42",      "name": "Example",      "price": 1.5    }  }}

Code-based: real logic in Express.js

When a response depends on earlier requests, write it. APIGit creates an Express.js 4 app, loads apigit-mock/main.js from the published commit and serves it at the mock's URL. Team and Enterprise plans.

Routes, middleware and state

mock is the Express app, so mock.get(), mock.post() and mock.use() work as documented by Express. JSON, URL-encoded and text bodies and cookies are parsed before your code runs. What you store in state is kept between requests.

  • Multi-step scenarios: a checkout, an OAuth flow, a computed price
  • Generate the routes from an API document, then edit them
  • console.log output appears beside the request in the logs
apigit-mock/main.js
mock.get('/users/:name', (req, res) => {
  res.json({ name: req.params.name, createdAt: new Date().toISOString() });
});

mock.post('/users', (req, res) => {
  state.users = state.users || [];
  state.users.push(req.body);
  res.status(201).json({ count: state.users.length });
});

State

Plain JSON, up to 2 MB, kept when the mock is overwritten or follows a branch. Inspect it in the State view.

Libraries

Faker 9.9.0, lodash, jsonwebtoken, ajv, moment, uuid, validator and more are pre-installed. Load them with require.

Modules

Split handlers, models and helpers into files under apigit-mock/ and require them from main.js.

Sandbox

Each mock runs in its own isolated environment, with no network access and no cloud credentials.

Pin it to a commit, or follow a branch

A mock server always serves one commit. You decide how that commit changes, and who can call the mock.

An update strategy for each mock

Pin a mock to keep a release or a partner sandbox stable. Or follow a branch on a paid plan: a push, a merged pull request, a file saved in the editor or a mirror sync republishes it. Publish from the application, or from a terminal or CI with apigit mock publish.

  • The URL, the access token and the state stay across updates
  • If an update cannot be published, the mock keeps serving its last good commit
  • Publish from a branch, from any commit in the history, or while editing
app.apigit.com/acme/petstore-platform/publish/mock/petstore-sandbox/setting
Setting page of a mock server with its update strategy, documents and access control

Access control and request logs

A mock is public, or requires an access token sent in the APIGIT-Token header or as a query parameter. The Logs view shows every request with its method, path, status and time, and the response. Credentials are removed from the logs.

  • Change the access setting or regenerate the token at any time
  • Republishing keeps the token
  • Read the log from a terminal with apigit mock logs
app.apigit.com/acme/petstore-platform/publish/mock/petstore-sandbox/logs
Mock server access logs in APIGit

Try it on your machine first

The apigit CLI runs both types locally, offline and without a login, on the engines of the hosted mock servers. The mock listens on 127.0.0.1:4010 and follows your edits without a restart, so you or your coding agent can try a change before it is committed.

  • Spec-based: the same routing, validation, examples and Prefer header
  • Code-based: the same Express app, mock, state and library versions
  • Local state starts empty, and there is no token, quota, log or sandbox
~/petstore-platform
$ npm install -g @apigit/cli$ apigit mock serve openapi.yaml$ apigit mock serve schema.graphql$ apigit mock serve apigit-mock

An OpenAPI file, a GraphQL schema at /graphql (CLI 0.7.0 or later), and the folder of a code-based mock.

What each plan includes

Mock server counts and monthly calls belong to the account, not to a seat.

FeatureFreeTeamEnterprise
Spec-based mocksYesYesYes
Code-based mocksNoYesYes
Follow a branchNoYesYes
Documents per mock server11010
Mock servers1UnlimitedUnlimited
Calls a month, for the account1,00020,000100,000
Choose the mock server's nameNoNoYes

Paid plans can add call packs: $20 per 50,000 calls a month. AsyncAPI documents are not mock inputs. See pricing

Give your team an API to build against today

Create a free workspace and publish a mock server from an OpenAPI document or a GraphQL schema. No scripts to write.

Free for individuals. No credit card.

$ npm install -g @apigit/cli$ apigit auth login$ apigit doc publish