Docs menu · Response templates
APIMock documentation
Response templates
Response templates turn an endpoint's response body into a safe Handlebars template. Reuse values from the incoming request, generate realistic data, and add simple conditional behavior without running arbitrary JavaScript.
Updated
Quick start
- Open an endpoint and enable Response template.
- Add helpers to the response body.
- Use the sample request panel to render a server-side preview.
- Save the endpoint and set it Live.
{
"id": "{{pathParam 'id'}}",
"name": "{{body 'name' 'Anonymous'}}",
"email": "{{faker 'internet.email'}}",
"requestId": "{{header 'x-request-id' 'not-supplied'}}"
}Templates are opt-in. When the switch is off, APIMock returns the response body exactly as written—even if it contains {{ braces }}.
Request helpers
Each helper takes a literal dot path and an optional fallback. Array values can be addressed by index, such as {{body 'items.0.name'}}. Missing values become an empty string when no fallback is supplied.
| Helper | Example | Reads |
|---|---|---|
| body | {{body 'user.name' 'Anonymous'}} | JSON or form body value |
| queryParam | {{queryParam 'page' '1'}} | Query-string value |
| pathParam | {{pathParam 'id'}} | Named :path parameter |
| header | {{header 'x-request-id'}} | Case-insensitive request header |
| cookie | {{cookie 'session'}} | Cookie value |
Request bodies are parsed for JSON media types and application/x-www-form-urlencoded. Repeated query and form fields are arrays and can be read with field.0.
Generate fake data
Use {{faker 'namespace.generator'}}. APIMock exposes a stable English-language allowlist; generator arguments and dynamic method names are intentionally unsupported.
IDs and values
string.uuid, datatype.boolean, number.int
People
person.firstName, person.lastName, person.fullName, person.jobTitle
Internet
internet.email, internet.username, internet.url, internet.ipv4
Contact and location
phone.number, location.streetAddress, location.city, location.state, location.country, location.zipCode
Business
company.name, commerce.productName, commerce.price, finance.currencyCode
Text
lorem.word, lorem.sentence, lorem.paragraph
Dates
date.past, date.future, date.recent
{
"id": "{{faker 'string.uuid'}}",
"name": "{{faker 'person.fullName'}}",
"product": "{{faker 'commerce.productName'}}",
"createdAt": "{{faker 'date.recent'}}"
}Logic and arrays
Use if, unless, and a single bounded each loop. Comparison helpers are eq, ne, and, or, and not.
{
"active": {{#if (eq (queryParam 'status') 'active')}}true{{else}}false{{/if}},
"items": [
{{#each (body 'items')}}
{"name": "{{this.name}}"}{{#unless @last}},{{/unless}}
{{/each}}
]
}Escaping and JSON
Double braces use standard Handlebars HTML escaping. Triple braces emit raw text. Use the json helper when inserting an object, array, or string as a JSON value.
{
"profile": {{{json (body 'profile')}}}
}Templates can deliberately echo headers or cookies into a response. Avoid returning credentials or personal data. Request-log redaction does not change the response you configured.
Limits and errors
- Template source: 64 KiB
- Request body available to a template: 1 MiB
- Rendered response body: 2 MiB
- Each loop: 100 items, with nested loops disabled
The editor blocks syntax, unknown-helper, and limit errors. A body over the request limit returns 413. Unexpected render failures return 500 with an X-APIMock-Request-Id header for Inspector correlation.
Build your first response template
Create an endpoint, enable templating, and verify it with the server preview.
Create endpoint