Skip to main content

Overview

This endpoint demonstrates KrakenD’s static response (mocking) capability. It returns a predefined JSON response without calling any backend service, useful for testing, development, and API prototyping. HTTP Method: GET
Endpoint: /mocked-response

What It Demonstrates

  • Static Responses: Returns hardcoded data without backend calls
  • API Mocking: Simulates API responses during development
  • Testing Support: Provides predictable responses for testing
  • Backend Decoupling: Allows frontend development before backend completion

Request Example

curl http://localhost:8080/mocked-response

Expected Response

Status: 200 OK
{
  "an_integer": 123,
  "an_array": [
    "arr1",
    "arr2"
  ],
  "an_object": {
    "obj": "obj1"
  }
}
This response is always the same and is returned instantly without any backend calls.

Backend Configuration

Although a backend is configured, it is never called:
  • Host: http://unexistent_backend (non-existent host)
  • URL Pattern: /
  • Purpose: Placeholder (required by configuration but unused due to static response)

KrakenD Configuration

{
  "@comment": "Feature: Mocked response",
  "endpoint": "/mocked-response",
  "method": "GET",
  "output_encoding": "json",
  "backend": [
    {
      "host": ["http://unexistent_backend"],
      "url_pattern": "/"
    }
  ],
  "extra_config": {
    "proxy": {
      "static": {
        "data": {
          "an_integer": 123,
          "an_array": [
            "arr1",
            "arr2"
          ],
          "an_object": {
            "obj": "obj1"
          }
        },
        "strategy": "always"
      }
    }
  }
}

Key Configuration Options

proxy.static

Configures static response behavior:
{
  "proxy": {
    "static": {
      "data": { ... },
      "strategy": "always"
    }
  }
}

Static Data (data)

{
  "data": {
    "an_integer": 123,
    "an_array": ["arr1", "arr2"],
    "an_object": {
      "obj": "obj1"
    }
  }
}
Defines the exact JSON response to return. Supports:
  • Primitives (strings, numbers, booleans, null)
  • Arrays
  • Nested objects
  • Any valid JSON structure

Strategy (strategy)

"strategy": "always"
Options:
  • always - Always return the static response, never call the backend
  • success - Return static response on backend success
  • complete - Return static response only when backend completes
  • errored - Return static response when backend fails (fallback)
Behavior with always:
  • Backend is never called
  • Response is instant (no network latency)
  • Backend host can be non-existent
  • Perfect for pure mocking

Output Encoding

"output_encoding": "json"
Specifies the response format. For static responses, this should always be json.

Use Cases

1. API Development

Define API contracts before backend implementation:
{
  "endpoint": "/users/{id}",
  "extra_config": {
    "proxy": {
      "static": {
        "data": {
          "id": 1,
          "name": "John Doe",
          "email": "[email protected]"
        },
        "strategy": "always"
      }
    }
  }
}

2. Testing

Provide predictable responses for automated tests:
# Test always returns the same data
for i in {1..10}; do
  curl http://localhost:8080/mocked-response | jq '.an_integer'
done
# Output: 123 (10 times)

3. Frontend Development

Allow frontend teams to work independently:
  • No backend required
  • Consistent test data
  • Fast iteration cycles

4. API Documentation

Provide live examples in documentation:
Try this example:
`curl http://api.example.com/mocked-response`

5. Feature Flags

Temporarily return static data during migrations:
{
  "strategy": "always",
  "data": {
    "feature_enabled": false,
    "message": "Feature under maintenance"
  }
}

6. Fallback Responses

Return static data when backends fail:
{
  "strategy": "errored",
  "data": {
    "status": "degraded",
    "message": "Using cached data"
  }
}

Strategy Comparison

StrategyWhen Static Response is Returned
alwaysAlways (backend never called)
successWhen backend call succeeds
completeWhen backend call completes (success or failure)
erroredWhen backend call fails

Performance

Response Time

Mocked endpoint:
  • Response time: < 1ms
  • No network overhead
  • No backend processing
Regular endpoint:
  • Response time: 50-500ms (typical)
  • Network latency
  • Backend processing time

Load Testing

Mocked endpoints can handle extremely high throughput:
# Load test with Apache Bench
ab -n 10000 -c 100 http://localhost:8080/mocked-response

# Typical results:
# Requests per second: 50,000+
# 99th percentile: < 2ms

Advanced Patterns

Dynamic-Like Responses

While the response is static, you can make it appear dynamic:
{
  "data": {
    "timestamp": "2026-03-03T12:00:00Z",
    "cache_hint": "no-cache",
    "data": [
      {"id": 1, "value": "Sample 1"},
      {"id": 2, "value": "Sample 2"}
    ]
  }
}
Note: The timestamp will not actually update. For truly dynamic timestamps, use a backend.

Conditional Mocking

Combine with request matching for conditional mocking:
{
  "endpoint": "/api/data",
  "extra_config": {
    "proxy": {
      "static": {
        "data": {"mode": "test"},
        "strategy": "always"
      }
    }
  }
}

Large Responses

You can mock large datasets:
{
  "data": {
    "results": [
      {"id": 1, "name": "Item 1"},
      {"id": 2, "name": "Item 2"},
      // ... hundreds of items
    ],
    "total": 1000,
    "page": 1
  }
}

Limitations

Static Data Only

  • Cannot include dynamic values (timestamps, random numbers, etc.)
  • Same response for every request
  • Cannot vary based on request parameters

No Backend Validation

  • Request validation must be handled elsewhere
  • Cannot verify request payloads
  • No real business logic execution

Configuration Size

Very large static responses can bloat the configuration file. Consider using external files for large datasets.

Transitioning to Real Backend

When the backend is ready, transition is simple: Before (Mocked):
{
  "endpoint": "/api/users",
  "backend": [{"host": ["http://placeholder"], "url_pattern": "/"}],
  "extra_config": {
    "proxy": {
      "static": {
        "data": { ... },
        "strategy": "always"
      }
    }
  }
}
After (Real Backend):
{
  "endpoint": "/api/users",
  "backend": [
    {
      "host": ["http://real_backend:8080"],
      "url_pattern": "/users"
    }
  ]
}
Simply remove the proxy.static configuration and update the backend host.

Testing the Endpoint

Basic Test

curl http://localhost:8080/mocked-response

Verify Response Structure

curl http://localhost:8080/mocked-response | jq '.
{
  "an_integer": 123,
  "an_array": ["arr1", "arr2"],
  "an_object": {"obj": "obj1"}
}

Check Response Time

time curl http://localhost:8080/mocked-response
# Should be < 10ms

Verify No Backend Calls

The backend host (http://unexistent_backend) doesn’t exist, proving no backend is called:
ping unexistent_backend
# ping: cannot resolve unexistent_backend

Best Practices

  1. Use for Development: Enable mocking during development, disable in production
  2. Document Clearly: Mark mocked endpoints in your API documentation
  3. Match Real Structure: Make mocked responses identical to real backend responses
  4. Version Control: Keep static data in version control
  5. Transition Plan: Plan how to transition from mocked to real backends
  6. Use Realistic Data: Use realistic sample data for better testing

Build docs developers (and LLMs) love