Docs menu · OpenAPI import
APIMock documentation
OpenAPI import
Import a Swagger 2.0, OpenAPI 3.0 or 3.1 spec and every operation becomes a mock endpoint that serves the spec's own examples. Paths shaped like a CRUD collection become live Resources that store what you create. Import the spec again after it changes and APIMock updates the mocks in place.
Updated
Quick start
- On the Endpoints page, open the More actions menu (⋯) and choose Import OpenAPI.
- Paste the spec, upload a
.json,.yamlor.ymlfile, or give its URL. - Select Preview. You see the spec's title and version, the endpoints by method, any Resources, and warnings such as operations with no example.
- Select Import. Everything goes into a folder named after the spec's
info.title.
curl -i https://<your-workspace>.apimock.dev/pets/1
HTTP/2 200
content-type: application/json
{ "id": 1, "name": "Rex", "species": "dog" }Paths keep their shape: /pets/{petId} becomes the route /pets/:petId. $refs are resolved, including ones that point at other documents over HTTPS. A URL import resolves relative $refs against the spec's own address.
Operations become endpoints
Each operation becomes one static Endpoint. APIMock picks the response it serves like this:
| What | How it's chosen |
|---|---|
| Status | 200, then 201, then any other 2xx, then default, then the first one listed. |
| Content type | The first JSON media type, otherwise the first one listed (Swagger 2.0: produces). |
| Body | The media type's example, else its first named example, else the schema's example. No example means an empty body. |
Bodies come only from examples in the spec. APIMock doesn't make up data from the schema, so an operation with no example imports with an empty body and a warning. Add one in the editor, or turn on response templates to generate it.
The operation's other examples aren't lost. Other named examples, and the example of each other status code, become response variants you select with a Prefer header:
Prefer: example=notFound # another named example
Prefer: code=404 # another status's exampleImported endpoints start with no auth, no delay, CORS open to every origin and templates off. HEAD, OPTIONS and TRACE operations are skipped with a warning.
When a path becomes a Resource
A pair of paths becomes a stateful Resource when it looks like a list and its items:
- a collection path, such as
/api/todos, whoseGETreturns{ data: [...], meta }in its schema or example, which is the shape a Resource's list returns; - and an item path below it with one parameter, such as
/api/todos/{id}, that has aGET.
The collection's POST and the item's PATCH, PUT and DELETE join the same Resource, so creates, updates and deletes persist and later reads return them. The last path segment is the Resource's name and has to be lowercase letters, digits and hyphens, starting with a letter. The OpenAPI spec you can export from an APIMock Resource has this shape.
Fields come from the POST body schema, keeping its required list. Without one, APIMock reads the PATCH or PUT body, then the item schema. id, createdAt, updatedAt and readOnly properties are left out. The records in the list example become seed data.
| Schema type | Field type |
|---|---|
| string | string |
| string with format: date-time | datetime |
| integer, number | number |
| boolean | boolean |
| enum with string values | enum |
| object, array, or no type | json |
Anything that isn't a clean match imports as static Endpoints that serve the spec's examples exactly:
| Case | Result |
|---|---|
| The collection GET doesn't return { data, meta } | Every operation becomes a static Endpoint. |
| The path has a parameter before the collection, e.g. /users/{userId}/posts | Static Endpoints. A Resource can't be scoped by a parent id. |
| A Resource with that name already exists in the workspace | Static Endpoints, with a note after the import. |
Request validation
Each imported operation keeps its request body schema. To reject bodies that don't match it, open the endpoint and turn on Validate request body under Request validation. The section only appears on endpoints imported with a request schema, and validation is off until you turn it on.
curl -i -X POST https://<your-workspace>.apimock.dev/pets \
-H "Content-Type: application/json" \
-d '{"name": 42}'
HTTP/2 422
{
"error": "Request body failed validation",
"errors": [
{ "path": "(root)", "message": "must have required property 'species'", "keyword": "required" },
{ "path": "/name", "message": "must be string", "keyword": "type" }
]
}Every problem is listed, not just the first. A body that isn't valid JSON gets a 422 with must be valid JSON. Validation runs after auth and before response variants, and it uses JSON Schema with formats such as email and date-time. If a schema can't be compiled, the endpoint skips validation rather than failing every request.
Re-importing a spec
Importing a spec with the same info.title into the same workspace updates the first import instead of making a copy. Operations are matched by operationId, or by method and path when a spec has none:
- Operations still in the spec are updated.
- New operations are added.
- Operations removed from the spec are deleted, and so are their Resources.
The spec wins for what it describes. An updated endpoint takes the spec's status and body again, and settings changed in the editor go back to the import defaults: delay, headers, auth, templates, and request validation, which turns off. Variants you added yourself are kept; the spec's own variants are replaced. A Resource keeps its access setting and the per-field unique and sensitive flags, but its fields are rebuilt and its records go back to the spec's seed data.
An import never overwrites an endpoint it didn't create. If the workspace already has an endpoint with the same method and route, that operation is skipped and counted in the result.
With AI agents
The MCP server's import_openapi tool does the same import from a coding agent. Pass the spec text as spec, or a url, and it reconciles on re-import the same way.
{
"workspace": "acme",
"url": "https://example.com/openapi.yaml"
}Limits
| Limit | Value |
|---|---|
| Pasted or uploaded spec | 1 MB |
| Spec fetched from a URL | 5 MB, 15 seconds, 5 redirects |
| External $ref documents per import | 25 |
| Variants per endpoint | 20 |
| Body of an imported variant | 64 KiB |
Specs and their $refs are only fetched from public addresses. Extra examples beyond the variant limits are dropped with a warning in the preview.