apimock-rs (API Mock)

Build a working REST API in seconds β without a backend.
Frontend blocked by an unfinished backend ?
Need stable API responses for UI tests or offline development ?
Drop JSON files into a folder and your API immediately exists.
Mock APIs easily π β just JSON and go
If youβre building or testing APIs, this tool makes mocking painless. Itβs super fast, efficient, and flexible when you need it to be. All you have to do to start up is just use folders and JSON without any config set.
- βοΈ Zero-config start.
- π¬οΈ Fast to boot, light on memory.
- πͺ File-based and rule-based matching. Scripting supported.
Why apimock-rs ?
- The backend is not ready yet.
- You need stable API responses for UI testing.
- You want offline development.
- CI tests require a predictable API.
- Your mock data is becoming large.
Handles real project scale
As your project grows, your mock API grows, too. Large mock datasets often cause problems:
- Slow startup
- High memory usage
- Crashes during UI testing
- Unstable CI runs
apimock-rs does not preload responses. Each response file is read only when a request arrives using non-blocking I/O. This keeps:
- Startup nearly instant
- Memory usage minimal
- Stable behavior under repeated requests
as validated with k6 load testing. You can run UI development and automated tests continuously without worrying about server instability.
π Documentation
For more details, π§ check out the docs.
Quick start
Easy to start with npm package.
# install into your app project
# and go
# just use folders and JSON
# response
# --> {"hello":"world"}
You may also check it out with browser to visit http://localhost:3001/api/v1/hello .
You now have a running REST endpoint.
Vite project integration
An example of scripts section in package.json is as below.
concurrently is used to run the Vite and API mock servers simultaneously, while cross-env enables terminal output coloring. Before starting, ensure you run:
Edit package.json:
"scripts":
Run:
npx apimock variation
| command | result |
|---|---|
npx apimock |
Run with all default parameters. |
npx apimock -p 4000 |
Run with custom port. |
npx apimock -d tests/apimock-dyn-route |
Run with custom root dir on server response. |
npx apimock -c apimock.toml |
Run with config file giving rich features. Running npx apimock --init beforehand is required. |
Configuration reference
Everything below describes the TOML config files. Run npx apimock --init (or
apimock --init) once to scaffold apimock.toml and apimock-rule-set.toml
into the current directory, then tweak to taste.
apimock.toml β top-level file
[]
= "127.0.0.1" # interface to bind
= 3001 # plaintext HTTP port
[] # optional β enables HTTPS
= "./cert.pem"
= "./key.pem"
# port = 3002 # optional β if omitted, HTTPS replaces HTTP on the port above
[]
= { = true, = true }
[]
= "first_match" # only value supported today
= ["apimock-rule-set.toml"] # paths, resolved relative to this file
= [] # Rhai scripts, resolved relative to this file
= "." # folder served when no rule matches
| Section / field | Required | Default | Notes |
|---|---|---|---|
listener.ip_address |
no | 127.0.0.1 |
Bind address. 0.0.0.0 to accept from other machines. |
listener.port |
no | 3001 |
Plaintext HTTP port. |
listener.tls.cert / .key |
yes if [listener.tls] set |
β | PEM files. Any key format rustls-pki-types accepts (PKCS#8, PKCS#1, SEC1). |
listener.tls.port |
no | same as listener.port |
When omitted, the single port serves HTTPS only β no plaintext listener is started. |
log.verbose.header |
no | false |
Log each request's headers. |
log.verbose.body |
no | false |
Log each request's URL query and JSON body (pretty-printed). |
service.strategy |
no | first_match |
Currently the only strategy β the first rule that matches wins. |
service.rule_sets |
no | [] |
See Rule-set schema below. Paths relative to this file. |
service.middlewares |
no | [] |
Rhai script paths, relative to this file. |
service.fallback_respond_dir |
no | . |
Folder served when nothing in rule_sets or middlewares handled the request. |
Rule-set schema β apimock-rule-set.toml
[] # optional
= "/api/v1/" # prepended to every rule's url_path
= "apimock-rule-sets/@respond-dir" # prepended to every rule's file_path
[] # optional
= 0 # applied to every rule in this set unless overridden
[[]]
= "complicated"
= "POST"
= { = "Bearer eyJhb", = "starts_with" }
= { = "user1" } # op defaults to "equal"
= { = "42" }
= { = "Strictly authorized !" }
[[]]
= { = "/public/", = "starts_with" }
= { = "public/index.json", = "records" }
[[]]
= ""
= { = 403 }
when.request β match conditions (AND-ed together)
| Field | Accepts | Purpose |
|---|---|---|
url_path |
string or { value, op } |
URL path match. String form is shorthand for op = "equal". |
method |
"GET" / "POST" / "PUT" / "DELETE" |
HTTP method. Omit to match any method. |
headers |
{ header-name = { value, op } } map |
Every listed header must match. Header names are matched case-insensitively per HTTP. |
body.json |
{ json-path = { value, op } } map |
JSON body match β see JSONPath form below. |
respond β response shape
Exactly one of file_path / text / status must be present. (text may be combined with status to set the response code.)
| Field | Type | Purpose |
|---|---|---|
file_path |
string | File under prefix.respond_dir (or the working dir if no prefix). Extension decides content type; .json / .json5 / .csv served as JSON, other text types served verbatim with an inferred Content-Type, binary types (images, audio, video, .pdf, .zip) served with the corresponding binary Content-Type. |
text |
string | Return this string verbatim. Content type text/plain; charset=utf-8. |
status |
integer (100β999) | HTTP status code. With text, sets the status; alone, returns an empty body with that status. Cannot be combined with file_path. |
headers |
{ name = value } map |
Extra response headers. |
delay_response_milliseconds |
integer | Sleep this long before responding β useful for simulating slow backends in UI tests. |
csv_records_key |
string | Only meaningful when file_path points at a .csv. The JSON response wraps the rows as { "<key>": [...] } using this value; defaults to "records". |
op β comparison operators
Every { value, op } structure accepts these op values. If op is omitted it defaults to equal.
op |
Meaning | Example |
|---|---|---|
equal (default) |
Exact string match | { value = "user1" } matches user1 only |
not_equal |
Anything but an exact match | { value = "guest", op = "not_equal" } matches everything except guest |
starts_with |
String starts with value | { value = "Bearer ", op = "starts_with" } matches any Bearer token |
contains |
Value appears anywhere inside the target | { value = "admin", op = "contains" } matches superadmin, admin-1, etc. |
wild_card |
Glob match β see below | { value = "/api/v*/users/?", op = "wild_card" } |
Wildcard / glob syntax (wild_card)
The glob matcher supports exactly two metacharacters, matching the common case without the complexity of a full regex engine:
| Token | Matches |
|---|---|
? |
Exactly one character |
* |
Zero or more characters |
Examples:
| Pattern | Matches | Doesn't match |
|---|---|---|
hello |
hello |
hell, hello! |
file*.txt |
file.txt, file123.txt, files/doc.txt |
file.csv |
file?.txt |
file1.txt, fileX.txt |
file.txt, file12.txt |
a*b?c |
axyzbxc, ab1c |
abc |
*test* |
my_test_file, test |
tes |
γγγ«γ‘?δΈη |
γγγ«γ‘γ―δΈη |
Unicode is respected per code point. |
This is NOT a regex. Sequences such as ., [β¦], (β¦), +, ^, $ are matched literally.
JSONPath form (body.json and csv_records_key)
The JSONPath support is deliberately minimal β only the "object key" and "array index" forms, joined by .. This covers every real-world body-match need without exposing a full jsonpath engine that users would then have to learn.
| Input | Meaning |
|---|---|
user |
Top-level key user |
user.id |
user β id (nested object) |
items.0 |
First element of array items |
orders.2.total |
Array index 2 of orders, then key total |
a.b.c |
Three-level nested object lookup |
If any segment is missing or has the wrong shape (e.g. a numeric segment against an object), the rule simply does not match β no error, because "missing" is a useful matchable condition.
prefix
| Field | Effect |
|---|---|
url_path |
Prepended to every rule's when.request.url_path before matching. Leading/trailing slashes are normalised. The contains operator deliberately ignores the prefix β it matches against the raw url_path value β because "contains" usually means "substring anywhere in the full request path", and re-applying a prefix there would be surprising. |
respond_dir |
Prepended to every rule's respond.file_path. Resolved relative to the rule-set file's directory, not the working dir β so configs travel cleanly between machines. |
default
| Field | Effect |
|---|---|
delay_response_milliseconds |
Default delay for rules in this set that don't specify their own. |
Open-source, with care
This project is lovingly built and maintained by volunteers.
We hope it helps streamline your API development.
Please understand that the project has its own direction β while we welcome feedback, it might not fit every edge case π±
Acknowledgements
Depends on tokio / hyper / toml / serde / serde_json / json5 / console / rhai / thiserror / anyhow. In addition, mdbook (as to workflows).