Dynamic Mocking

Uses: Insomnia

Dynamic mocking extends Insomnia’s existing mock server feature. For each request, dynamic mocking renders Liquid templates so responses can include:

  • Data from the request (headers, query parameters, path, or body).
  • Randomly generated fake data.

How to use the Liquid template

Use the Liquid template language in the Insomnia app:

  1. Open or create a Mock server.
  2. Create a new route.
  3. Go to the Mock Body tab.
  4. Define a dynamic response body.

Response headers and status codes remain configured per route, which ensures consistency while the response body updates dynamically.

Differences from traditional mocks

Traditional mocks return static, predefined payloads, while dynamic mocks generate context-aware, variable outputs:

Mock server type

Response behavior

Example

Static Returns the response body exactly as written. The Mock Body is "name": "George". Every request to the route returns George.
Dynamic (random) Each request evaluates faker expressions in the Mock Body. The Mock Body contains {"name": ""}. Each request returns a different randomly generated name.
Dynamic (context-aware) Expressions in the Mock Body read data from the incoming Collection request. The Mock Body contains {"echoed_id": ""}. A request from a Collection to <server-url/mock?id=42 returns {"echoed_id": "42"}.
Mixed Combines fixed fields with evaluated expressions. The Mock Body contains faker.randomFullName and a fixed "role": "admin" field. Each request returns a different name with the role set to admin.

For adding random values, Insomnia provides faker variables that you can insert anywhere in the response body.

Use dynamic mocking to:

  • Serve request-aware responses: Create a mock route that shapes the response body based on the incoming request. For example, echoing identifiers or switching fields based on a query parameter or request body. Only available when running the request from a Collection, where you can copy the mock route URL and input query parameters to be echoed.
  • Insert random data with faker variables: Use faker variables to generate values like names, emails, timestamps, and UUIDs.

Dynamic capabilities

Use case

Description

Use request data Access details from the incoming request and reflect them in the mock response. For example, echo a query parameter or include a field from the request body. This makes the mock behave more like a live API.
Apply conditional logic Use simple Liquid conditions to vary the response based on the request.
Only a limited subset of Liquid tags is supported for safety.
Generate fake data Insert random but realistic data, such as names, emails, or timestamps using faker variables.
Combine request and fake data Mix request data with generated values for realistic scenarios. For example, include the requester’s ID with random profile data.

Template syntax

Dynamic mocking uses Liquid as its templating language. Mock response bodies are rendered as Liquid templates at request time, with two built-in variables available:

  • req — exposes data from the incoming request, including headers, query parameters, path segments, and body.
  • faker — exposes a set of data generation functions that produce random but realistic values such as names, emails, and timestamps.

To keep templates safe and predictable, only a limited subset of Liquid tags is supported for logic control. For filters, only the default filter is supported.

Use data from requests

You can access values from incoming requests and include them in your mock responses.

Format to output a variable:

{{ req.headers['Content-Type'] }}
{{ req.body.name }}

Format to define a default value:

{{ req.body.name | default: "George" }}

Available variables

  • req.headers.* — Reference a specific request header
  • req.queryParams.* — Reference query parameters in the URL
  • req.pathSegments[*] — Reference specific segments of the URL path
  • req.body — Returns the full request body (only when the body is not a binary stream)
  • req.body.* — When the content type is application/json, application/x-www-form-urlencoded, or multipart/form-data, Insomnia parses the body and exposes each field

Generate random data

Use faker variables to generate random but realistic data in mock responses.

For a complete list of available faker variables, check the faker variables list.

Format to output random data:

{{ faker.<variable-name> }}

For example:

{{ faker.randomFullName }}

Liquid logic control

Only the following Liquid tags are supported:

Tag

Description

Reference

assign Creates or updates a variable within the template scope. Liquid Assign
if Conditionally renders a block of content when a statement evaluates as true. Liquid If
unless Renders a block when a statement evaluates as false; acts as the inverse of if. Liquid Unless
raw Prevents Liquid from interpreting enclosed content. Use this to escape template syntax within mock responses. Liquid Raw

For additional implementation details and syntax behavior, go to the LiquidJS documentation.

The test options for a mock route in the Insomnia app

Basic test options

Option

Description

When to use

Send now Immediately send a request to the mock server and display the response in the Response pane. Use this option for quick validation that your mock routes, dynamic templates, or environment variables resolve correctly. Ideal during early setup or after modifying a mock definition.
Generate client code Produce a client-side code snippet that reproduces the current mock request in multiple languages. For example, curl, JavaScript fetch, or Python requests. Use this when you want to share or automate the mock request outside Insomnia. For example, add it to test scripts, CI jobs, or SDK examples.

Advanced test options

Option

Description

When to use

Send after delay Schedules the request to execute after a specified delay in milliseconds. The request is queued and sent automatically once the delay expires. Use this option to simulate network latency, rate-limited APIs, or background task timing. It helps validate client-side behavior when responses are delayed.
Repeat on interval Sends the same request repeatedly on a fixed interval until stopped. Each cycle executes independently, allowing you to observe variable or time-sensitive responses from the dynamic mock. Use this to test dynamic responses, such as mocks that use Faker data, timestamp generation, or conditional logic. It’s also useful for load, polling, or long-running integration tests.

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!