Mockers is a CLI + HTTP server that lets you spin up fake APIs from files in seconds.
If you need a quick backend for frontend work, contract testing, demos, QA, or local integration tests — this thing is exactly for that. No heavy setup, no DB, no headache.
You have requests coming in. You want deterministic responses. You want them fast.
Mockers maps URL + HTTP method to files, returns file contents as responses, and gives you extra controls like:
- custom status codes,
- response delay,
- custom headers,
- admin API,
- request forwarding to a real origin,
- optional disk write-through caching.
So it can work as both:
- a pure mock server, and
- a "mock-first, proxy-if-missing" server.
Build from source:
cargo build --releaseBinary will be in target/release/mockers.
Run server with defaults:
mockers serveDefaults:
- host:
0.0.0.0 - port:
8080 - https port:
8443 - mocks dir:
./mocks
If ./mocks does not exist, Mockers creates it.
mockers <COMMAND>Available commands:
serve— run HTTP servercreate— create a file-based mock + config entrylist— list all file-based mocksinfo— show full info for a mock (status, headers, delay, mime, body)delete— delete a specific mock (and clean config entry)enable— enable a disabled mockdisable— disable a mock
There are also aliases (run, start, ls, rm, etc.), check --help.
mockers serve [OPTIONS]| Flag | Default | Description |
|---|---|---|
--host |
0.0.0.0 |
Interface to bind to |
--port, -p |
8080 |
Port to listen on |
--https-port |
8443 |
HTTPS port to listen on |
--mocks, -m |
mocks |
Directory with mock files |
--cors, -c |
false |
Adds CORS headers (Access-Control-Allow-Origin: *) |
--preflight |
unset | Auto-handle browser OPTIONS preflight requests |
--delay-ms, -d |
0 |
Global response delay in ms |
--origin, -o |
unset | Fallback upstream server when mock is missing |
--admin-base-url, -a |
unset | Enables admin API + Swagger under given absolute base path |
--log-request, -l |
info |
Request logging level: info, debug, trace |
mockers serve can run with HTTPS enabled.
To turn it on, put a TLS certificate and private key into your mocks directory and name them cert.pem and key.pem. No magic, just files.
Mockers uses a separate port for HTTPS, so HTTP and HTTPS do not have to fight over the same one. Configure it with the httpsPort field in the global config file or with the --https-port flag.
If cert.pem, key.pem, and an HTTPS port are present, Mockers will start serving HTTPS.
For local development, you can generate certificates with any tool you prefer. Mockers does not care. If you want the easy route, use mkcert. It can create a local root CA, add it to your system trust store, and issue a certificate for 127.0.0.1 or a local domain.
If --preflight is enabled, Mockers can answer browser preflight requests automatically:
mirror— mirrors requested origin/method/headers back to the browser.permissive— basically "allow everything" mode (*, all methods, etc.).
Good for local dev when CORS fights you.
info: method + path/query.debug: method + path/query + headers.trace: method + path/query + headers + body.
Mock file naming convention is:
<route_path>.<http_method_lowercase>
Examples:
user.get -> GET /user
login.post -> POST /login
config.put -> PUT /config
Nested routes are just nested folders/files:
mocks/
├─ users/
│ ├─ list.get
│ └─ create.post
└─ auth/
└─ login.post
This gives you:
GET /users/listPOST /users/createPOST /auth/login
When your project already has a populated mocks/ directory, you can read it as a contract map.
Example:
mocks/
├─ auth/
│ ├─ login.post
│ ├─ refresh.post
│ └─ config.json
├─ users/
│ ├─ me.get
│ ├─ me.patch
│ └─ config.json
└─ health.get
How to interpret this:
auth/login.postmeansPOST /auth/loginauth/refresh.postmeansPOST /auth/refreshusers/me.getmeansGET /users/meusers/me.patchmeansPATCH /users/mehealth.getmeansGET /health
Important details:
- File extension is always the HTTP method (
.get,.post,.patch, ...). - Path segments come from folders + filename stem.
config.jsonis local to its folder and config keys must match mock filenames in that same folder.- Body is returned exactly from file content, while
Content-Typeis auto-detected from bytes (JSON/CSS/text/binary, etc.).
In any mock directory, you can add a config.json file.
Keys are mock file names in the same directory (like user.get).
Example:
{
"user.get": {
"delayMs": 1200,
"statusCode": 201,
"headers": {
"X-Server": "Mockers"
},
"cacheMode": "overwrite",
"disabled": false
}
}| Field | Type | What it does |
|---|---|---|
delayMs |
number | Per-mock delay in ms |
statusCode |
number | Per-mock HTTP status |
headers |
object string->string | Extra response headers |
cacheMode |
overwrite | nocache |
Controls write-through behavior when proxying to origin |
disabled |
boolean | If true, file mock is ignored |
For a matching mock file:
- status defaults to
200 - delay defaults to global
--delay-msvalue (or0) - headers default to empty
- cache mode defaults to
nocache - disabled defaults to
false
If a config key is missing, defaults are used.
For every incoming request, Mockers roughly does this:
- Build mock filename from request path + method.
- Try file-based mock.
- If file mock is missing (or disabled):
- if
--originis set -> proxy request to origin, - else -> return
404.
- if
- If proxied and
cacheMode=overwrite, write response to disk asynchronously (mock file + config.json).
So you can warm up mocks from a real backend automatically.
mockers create [OPTIONS] <ROUTE>Creates:
- a mock file (
<route>.<method>), - and updates/creates
config.jsonin the same directory.
| Flag | Default | Description |
|---|---|---|
--method |
GET |
HTTP method |
--status-code, -s |
200 |
Response status in config |
--delay-ms, -d |
0 |
Delay in config |
--header |
none | Add response header (Key: Value) |
--contents, -c |
{"hello":"world"} |
Mock body content |
--cache-mode |
nocache |
overwrite or nocache |
--mocks, -m |
mocks |
Mocks directory |
--disabled |
false |
Create mock as disabled |
mockers create /users/profile --method GET --status-code 200 --header "X-Env: local" --contents '{"id":1}'mockers list [OPTIONS]Shows all detected mock files with valid HTTP method extensions.
Example:
mockers list --mocks ./mocksmockers info [OPTIONS] <NAME>Searches by partial name/path (case-insensitive), then prints:
- normalized mock path,
- response status,
- configured headers,
- delay,
- detected mime type,
- body (
--show-bodyonly).
If multiple mocks match, it will ask you to be more specific by listing candidates.
mockers delete [OPTIONS] <NAME>Deletes one matching mock file.
Also removes that entry from config.json in the same directory.
If config becomes empty, config.json is deleted too.
If your query matches multiple mocks, it prints candidates and does nothing.
mockers enable [OPTIONS] <NAME>
mockers disable [OPTIONS] <NAME>These commands toggle the disabled flag in config.json for one matching mock.
disable=> mock is ignored at serve time.enable=> mock becomes active again.
If multiple matches are found, it asks for a more specific query.
Enable admin surface with:
mockers serve --admin-base-url /__adminThen you'll get:
- REST API under
/__admin/api/v2/... - Swagger UI under
/__admin/swagger
The admin API works directly with file-based mocks on disk.
Creating a mock creates the mock file and updates config.json.
Deleting a mock removes the mock file and cleans up its config entry.
Listing mocks returns only route path + HTTP method pairs.
POST /api/v2/mocks— get all mocks (path+methodonly)POST /api/v2/mocks/create— create a new file-based mockPOST /api/v2/mocks/config— get mock config by path+methodPOST /api/v2/mocks/delete— delete a file-based mock by path+method
Swagger also documents request/response schemas and known admin error codes.
Two separate things exist:
--corsadds regularAccess-Control-Allow-Origin: *to regular responses.--preflighthandles OPTIONS preflight negotiation automatically.
Use both if you want easiest browser interop in local env.
mockers serve --mocks ./mocksmockers serve --mocks ./mocks --origin https://example-api.devOptional: set cacheMode: "overwrite" for selected mocks to save upstream responses to disk.
mockers serve --cors --preflight permissivemockers serve --admin-base-url /__adminOpen: http://localhost:8080/__admin/swagger
- Admin base URL must be an absolute path (like
/__admin). - Mockers only treats files with valid HTTP method extensions as mocks.
- If a path in
--mockspoints to a file/symlink instead of dir, command fails. - Relative
--mockspaths are resolved from current working directory. - Response body mime is auto-detected from content.
JSON schema for config.json:
docker build -t mockers-dockers .This builds a production image for mockers.
The mocks directory must be mounted from the host into /app/mocks inside the container.
docker run --rm \
-p 8080:8080 \ # if mockers serves HTTP
-p 8443:8443 \ # if mockers serves HTTPS
-v "/path/to/host/mocks/directory:/app/mocks" \
mockers-dockers serveThis will:
- expose the server on port
8080(or8443when HTTPS enabled, see HTTPS support) - mount the local
/path/to/host/mocks/directorydirectory into the container as/app/mocks - start
mockers serve
The application always listens on port 8080 or 8443 inside the container.
To expose it on a different host port, change the Docker port mapping:
docker run --rm \
-p 9090:8080 \
-v "/path/to/host/mocks/directory:/app/mocks" \
mockers-dockers serveIn this example:
- host port:
9090 - container port:
8080
The container always expects mocks at:
/app/mocksA local directory must be mounted there when the container is started.
Read-only mount is recommended if mockers only needs to read mock files:
-v "/path/to/host/mocks/directory:/app/mocks:ro"If write access is required, remove :ro:
-v "/path/to/host/mocks/directory:/app/mocks"docker build -t mockers-dockers .
docker run --rm \
-p 8080:8080 \
-v "/path/to/host/mocks/directory:/app/mocks:ro"
mockers-dockers serve \
--cors \
--delay-ms 150 \
--log-request debugmockers can generate shell completion scripts for supported shells.
To enable completions, add the following line to your shell startup file:
source <(mockers completion -s SHELL)Replace SHELL with one of the supported shells:
bashzshfishelvishpowershell
Add this line to ~/.bashrc:
source <(mockers completion -s bash)Add this line to ~/.zshrc:
source <(mockers completion -s zsh)Add this line to your Fish config file, usually ~/.config/fish/config.fish:
source (mockers completion -s fish | psub)Add this line to your Elvish config file, usually ~/.config/elvish/rc.elv:
eval (mockers completion -s elvish | slurp)Add the generated script to your PowerShell profile:
mockers completion -s powershell | Out-String | Invoke-ExpressionYou can place this command in your PowerShell profile file so that completions are loaded automatically in every session.
Huge thanks to @Caik, whose Go version sparked the original idea. Also thanks to bloodvez and silentroach.
MIT License.