Mock Rules
Intercept matching requests and return fake responses instead of hitting the real server. Perfect for offline development, testing error states, and simulating API behavior.
Mock rules are static. They can match by URL, method, and request headers, but they do not maintain server-side state or branch on request bodies.
Web UI
Open Mock Rules from the Modify group. The page provides:
- A form to create mock rules with Name, URL Pattern, Match Type (exact/wildcard/regex), Method, Header Conditions, Status Code, Response Headers, Response Body, File Path, Directory Path, Delay, and Priority
- A list of all mock rules with enable/disable toggles
- Edit and delete buttons for each rule
- Active/inactive status indicators
When you create a mock rule for an HTTPS URL, APXY automatically enables SSL interception for that domain.
CLI
Creating mock rules
# Mock an endpoint with a JSON response
apxy mock add \
--name "Mock Users" \
--url "https://api.example.com/api/users" \
--match exact \
--status 200 \
--body '{"users": [{"id": 1, "name": "Test User"}]}'
# Mock with wildcard matching
apxy mock add \
--name "Mock All API" \
--url "https://api.example.com/api/*" \
--match wildcard \
--status 200 \
--body '{"mocked": true}'
# Mock with regex and custom delay
apxy mock add \
--name "Mock User by ID" \
--url "https://api.example.com/api/users/\\d+" \
--match regex \
--status 200 \
--body '{"id": 42, "name": "Mocked"}'
# Simulate a slow API
apxy mock add \
--name "Slow Endpoint" \
--url "https://api.example.com/api/slow" \
--match exact \
--status 200 \
--body '{"data": "delayed"}' \
--delay 2000
# Add provider-style response headers
apxy mock add \
--name "GitHub Rate Limit" \
--url "https://api.github.com/user" \
--match exact \
--headers "Content-Type=application/json,X-RateLimit-Remaining=0,Retry-After=60" \
--status 403 \
--body '{"message":"API rate limit exceeded"}'
# Serve a response from a local file
apxy mock add \
--name "Mock from file" \
--url "/api/config" \
--match exact \
--file-path ./mocks/config.json
# Serve static files from a local directory
apxy mock add \
--name "Mock static assets" \
--url "/static/*" \
--match wildcard \
--dir-path ./publicFile and directory responses
Instead of inline --body, you can serve responses from local files:
| Flag | Description |
|---|---|
--file-path | Serve the response body from a single file |
--dir-path | Serve files from a directory (matched by URL path) |
These are mutually exclusive with --body.
Header conditions
Match requests based on specific header values:
apxy mock add \
--name "Mock mobile API" \
--url "https://api.example.com/api/users" \
--match exact \
--header-conditions '{"X-Platform":"mobile"}' \
--status 200 \
--body '{"mobile": true}'Match types
| Type | Description | Example |
|---|---|---|
exact | URL must match exactly | /api/users matches only /api/users |
wildcard | * matches any characters | /api/* matches /api/users, /api/posts |
regex | Full regular expression | /api/users/\d+ matches /api/users/123 |
Managing rules
# List active mock rules
apxy mock list
# Enable/disable a rule without removing it
apxy mock enable --id <rule-id>
apxy mock disable --id <rule-id>
# Remove a specific rule
apxy mock remove --id <rule-id>
# Clear all rules
apxy mock clearPriority
When multiple rules match a request, the one with the lowest priority number wins:
apxy mock add --name "Catch-all" --url "/api/*" --match wildcard --priority 10 --status 404
apxy mock add --name "Specific" --url "/api/users" --match exact --priority 1 --status 200A request to /api/users will return 200 (priority 1 beats priority 10).
Use Cases
Testing error handling
apxy mock add --name "Server Error" \
--url "/api/payments" \
--status 503 \
--body '{"error": "service unavailable"}'Offline development
apxy mock add --name "stub-users" --url "/api/users" --status 200 --body '{"users":[]}'
apxy mock add --name "stub-auth" --url "/api/auth/token" --status 200 --body '{"token":"fake"}'Simulating slow responses
apxy mock add --name "Slow Payment" \
--url "/api/payments" --delay 3000 \
--status 200 --body '{"status":"ok"}'