Skip to content

Proxy Recording

mockd can act as a MITM (Man-in-the-Middle) proxy to record real API traffic. Recorded requests and responses are automatically saved as mocks that can be replayed later.

Proxy recording is useful for:

  • Capturing production API behavior without manual mock creation
  • Recording integration test fixtures from real services
  • Creating realistic mocks from actual API responses
  • Debugging API interactions by inspecting traffic

Start the proxy targeting an upstream API:

Terminal window
mockd proxy --target https://api.example.com --record

Point your application to the proxy:

Terminal window
# Application makes request through proxy
curl http://localhost:4280/api/users
# Proxied to https://api.example.com/api/users

The request and response are recorded. View recordings:

Terminal window
mockd recordings list

Record all traffic passing through:

Terminal window
mockd proxy --target https://api.example.com --record

Recordings are saved to ./recordings/ by default.

Replay recorded responses without contacting upstream:

Terminal window
mockd proxy --playback --recordings ./recordings

Record new requests, replay known ones:

Terminal window
mockd proxy --target https://api.example.com --record --playback

Priority:

  1. Check recordings for matching request
  2. If found, replay recorded response
  3. If not found, proxy to upstream and record
{
"proxy": {
"target": "https://api.example.com",
"record": true,
"recordingsDir": "./recordings"
}
}

Start with config:

Terminal window
mockd proxy --config proxy-config.json
{
"proxy": {
"target": "https://api.example.com",
"port": 4280,
"record": true,
"playback": true,
"recordingsDir": "./recordings",
"recordFormat": "json",
"stripHeaders": [
"Authorization",
"Cookie"
],
"rewriteHost": true
}
}
FieldDescriptionDefault
targetUpstream API URLRequired
portProxy port4280
recordEnable recordingfalse
playbackEnable playbackfalse
recordingsDirWhere to save recordings./recordings
recordFormatOutput format (json, yaml)json
stripHeadersHeaders to remove from recordings[]
rewriteHostRewrite Host header to targettrue

Each recorded request creates a file:

recordings/
├── GET_api_users_abc123.json
├── POST_api_users_def456.json
└── GET_api_users_1_ghi789.json

Recording content:

{
"recorded_at": "2024-01-15T10:30:00Z",
"request": {
"method": "GET",
"path": "/api/users",
"headers": {
"Accept": "application/json"
},
"query": {}
},
"response": {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": {
"users": [
{"id": 1, "name": "Alice"}
]
}
}
}

mockd generates CA certificates automatically:

Terminal window
mockd proxy --target https://api.example.com
# Generates ./certs/mockd-ca.crt

Install the CA certificate in your system or browser to avoid TLS warnings.

Use your own certificates:

Terminal window
mockd proxy --target https://api.example.com \
--ca-cert ./my-ca.crt \
--ca-key ./my-ca.key
{
"proxy": {
"target": "https://api.example.com",
"tls": {
"caCertFile": "./certs/ca.crt",
"caKeyFile": "./certs/ca.key",
"certDir": "./certs/generated"
}
}
}
{
"proxy": {
"target": "https://api.example.com",
"record": true,
"recordFilter": {
"includePaths": [
"/api/users.*",
"/api/posts.*"
],
"excludePaths": [
"/api/health",
"/api/metrics"
]
}
}
}
{
"proxy": {
"recordFilter": {
"methods": ["GET", "POST"]
}
}
}

Record only successful responses:

{
"proxy": {
"recordFilter": {
"statusCodes": [200, 201, 204]
}
}
}

Remove sensitive headers from recordings:

{
"proxy": {
"stripHeaders": [
"Authorization",
"Cookie",
"X-API-Key",
"X-Auth-Token"
]
}
}

Mask sensitive data in recorded bodies:

{
"proxy": {
"redact": {
"bodyFields": [
"password",
"secret",
"$.user.ssn"
],
"replacement": "[REDACTED]"
}
}
}

Convert recordings to a mock configuration:

Terminal window
mockd recordings convert --input ./recordings --output mocks.json

Options:

Terminal window
# Convert with response templates
mockd recordings convert --input ./recordings --output mocks.json --templatize
# Merge with existing mocks
mockd recordings convert --input ./recordings --output mocks.json --merge
Terminal window
# Basic proxy with recording
mockd proxy --target https://api.example.com --record
# Playback only
mockd proxy --playback --recordings ./recordings
# Custom port
mockd proxy --target https://api.example.com --port 4290
# With config file
mockd proxy --config proxy.json
Terminal window
# List recordings
mockd recordings list
# List with filter
mockd recordings list --path "/api/users.*"
# Show recording details
mockd recordings show GET_api_users_abc123.json
# Delete recordings
mockd recordings delete --older-than 7d
# Convert to mocks
mockd recordings convert --input ./recordings --output mocks.json
Terminal window
# Start proxy recording
mockd proxy --target https://production-api.example.com --record
# Run your application tests through the proxy
API_URL=http://localhost:4280 npm test
Terminal window
mockd recordings list
# Output:
# GET_api_users_abc123.json (200, 1.2kb)
# POST_api_users_def456.json (201, 0.3kb)
# GET_api_users_1_ghi789.json (200, 0.5kb)
Terminal window
mockd recordings convert --input ./recordings --output mocks.json
Terminal window
mockd start --config mocks.json
# Tests now run against recorded responses
FeatureProxy ModeMock Server
Upstream dependencyRequiredNot needed
Real responsesYesNo (mocked)
RecordingBuilt-inN/A
Offline operationPlayback onlyYes
Best forCapturing real behaviorDevelopment/testing