mockapi
Spin up a fully functional mock HTTP server from a YAML or JSON spec in one line. Dynamic response rules, request logging, hot reload, and a CLI tool. Zero dependencies.
Installation
pip install mockapi # JSON specs only
pip install mockapi[yaml] # adds YAML support
Quick Start
# Start a mock server from a YAML spec
mockapi serve spec.yaml
# Or use it in Python tests
from mockapi import MockAPI
with MockAPI("spec.yaml") as api:
response = requests.get(api.url + "/users")
assert response.json() == [{"id": 1, "name": "Alice"}]
Spec Format
YAML (spec.yaml)
routes:
- method: GET
path: /users
status: 200
body:
- id: 1
name: Alice
- id: 2
name: Bob
- method: GET
path: /users/{id}
status: 200
body:
id: 1
name: Alice
rules:
- match:
path_param:
id: "999"
then:
status: 404
body:
error: User not found
- method: POST
path: /users
status: 201
body:
id: 3
name: Created
- method: GET
path: /slow
body: {}
delay: 1.5
- method: GET
path: /broken
error: timeout
JSON (spec.json)
{
"routes": [
{"method": "GET", "path": "/ping", "status": 200, "body": {"ok": true}},
{"method": "POST", "path": "/echo", "status": 201, "body": {"received": true}}
]
}
CLI
# Basic usage
mockapi serve spec.yaml
# Custom port and host
mockapi serve spec.json --port 9000 --host 0.0.0.0
# Disable hot reload
mockapi serve spec.yaml --no-reload
# Quiet mode (no request logs)
mockapi serve spec.yaml --quiet
Output:
=======================================================
mockapi — Mock API Server
URL: http://127.0.0.1:8888
Spec: spec.yaml
Routes: 4
Hot reload: ✅
=======================================================
GET /users → 200
GET /users/{id} → 200
POST /users → 201
GET /slow → 200 [delay 1.5s]
Python API
Context Manager (for tests)
from mockapi import MockAPI
with MockAPI("spec.yaml", port=8080) as api:
# api.url = "http://127.0.0.1:8080"
res = requests.get(api.url + "/users")
assert res.status_code == 200
Programmatic Routes
api = MockAPI(port=8888, verbose=False)
api.add_route("GET", "/ping", body={"ok": True})
api.add_route("POST", "/users", status=201, body={"created": True})
api.add_route("GET", "/slow", body={}, delay=2.0)
api.add_route("GET", "/fail", error="timeout")
api.add_route("GET", "/users/{id}", body={"id": 1})
api.start()
Dynamic Rules
Conditional responses based on path params, query strings, or request body:
api.add_route("GET", "/users/{id}", body={"id": 1, "name": "Alice"}, rules=[
{
"match": {"path_param": {"id": "999"}},
"then": {"status": 404, "body": {"error": "Not found"}}
},
{
"match": {"query": {"format": "minimal"}},
"then": {"status": 200, "body": {"id": 1}}
}
])
Request History & Stats
# All requests
history = api.history
# [{"method": "GET", "path": "/users", "status": 200, "duration_ms": 1.2, ...}]
# Requests for a specific path
api.history_for("/users")
# Summary stats
api.stats()
# {"total": 42, "by_status": {200: 38, 404: 4}, "by_method": {"GET": 40, "POST": 2}}
# Clear history
api.clear_history()
Hot Reload
The spec file is watched for changes automatically. Edit spec.yaml while the server is running and routes update within 1 second — no restart needed.
Error Simulation
error value |
Behaviour |
|---|---|
timeout |
Hangs for 30 seconds |
connection_reset |
Closes connection immediately |
empty |
Returns empty response body |
API Reference
MockAPI
| Method | Description |
|---|---|
__init__(spec, host, port, hot_reload, verbose) |
Create server |
add_route(method, path, status, body, ...) |
Add a route |
start() |
Start background server |
stop() |
Stop server |
reload() |
Manually reload spec from file |
history |
List of all recorded requests |
history_for(path) |
Requests for a specific path |
stats() |
Summary statistics |
clear_history() |
Clear request log |
url |
Base URL string |
Running Tests
pip install pytest pyyaml
pytest tests/ -v
License
MIT © prabhay759
Metadata
Release files for mockapiv2 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| mockapiv2-1.0.0.tar.gz | 13.2 kB | With provenance | Details for mockapiv2-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| mockapiv2-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for mockapiv2-1.0.0-py3-none-any.whl |
Total release size: 24.5 kB
Release files / mockapiv2-1.0.0.tar.gz
| Download URL | mockapiv2-1.0.0.tar.gz |
|---|---|
| Size | 13.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
10d8705153b0a94897c6c829dacd1ed94fc937a3126698aea9df80f2bd613540
|
|
BLAKE2b-256 checksum How to use checksums |
179cac13f3b5b2a5258406114e15ccdedfe4226fa54ba721bdddcd92b144f286
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 3, 2026.
Transparency logRelease files / mockapiv2-1.0.0-py3-none-any.whl
| Download URL | mockapiv2-1.0.0-py3-none-any.whl |
|---|---|
| Size | 11.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
253fa34970fbe70685b92099ec23d5459b100400114d6853bfd8040ae8a16c75
|
|
BLAKE2b-256 checksum How to use checksums |
9556c1c21e23eeb5fcc128c5eeb1768998d9aa8a9f031742cdbab5c0718fafb6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 3, 2026.
Transparency logSecurity for mockapiv2 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for mockapiv2-1.0.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 3, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.