# Template renderer API

> Render Mustache or Liquid templates with JSON data: loops, conditions, partials, Vietnamese filters (VND, amount in words, dd/mm/yyyy) and mail-merge rows.

- Endpoint: `POST https://apibox.io.vn/v1/template-render`
- Category: Text
- HTML docs: https://apibox.io.vn/docs/template-render/ (Vietnamese: https://apibox.io.vn/vi/docs/template-render/)
- Online tool: https://apibox.io.vn/tools/template-render/
- Authentication: `Authorization: Bearer <API key>` or `X-API-Key: <API key>` (free key: https://apibox.io.vn/dashboard/#signup)

Send a `template` and `data` and get the rendered text. Two engines run as interpreters (no code generation, safe on a serverless runtime): Mustache (logic-less, `{{name}}`, `{{#items}}…{{/items}}`, `{{^empty}}`, partials `{{> header}}`) and Liquid (LiquidJS: `{% if %}`, `{% for %}`, `{% assign %}`, `{% render "part" %}`, 40+ standard filters). `engine=auto` picks Liquid when the template uses `{% %}` tags or `| filters`.

Vietnamese filters work in both engines: `vnd` (1.500.000 đ), `number_vi`, `vnd_words` / `so_thanh_chu` (Một triệu năm trăm nghìn đồng), `date_vn` / `ngay_vn` (20/11/2026), `date_vn_long` / `ngay_thang_nam` (ngày 20 tháng 11 năm 2026), `remove_accents` / `bo_dau`, `upcase_vi`, `slug_vi`. In Liquid they are filters (`{{ tong | vnd }}`); in Mustache they are sections (`{{#vnd}}{{tong}}{{/vnd}}`).

`output=html` escapes values for HTML; the default `text` does not. `rows` renders the template once per object (up to 1,000, for SMS / Zalo / e-mail mail-merge). `partials` holds named templates for includes — no files or URLs are ever read. `action=variables` lists the variables a template uses. Limits: 200,000-character templates, 2 MB output, about 2 seconds of rendering; data is read through own properties only.

## Parameters

| Name | In | Type | Required | Default | Description |
|---|---|---|---|---|---|
| `template` | body | string | yes |  | The template (or a text/plain body). |
| `data` | body | object | no |  | Data for one render (JSON object). |
| `rows` | body | array | no |  | Batch: render once per object (max 1,000), returns `outputs`. |
| `engine` | body | string | no | `auto` | `mustache`, `liquid` or `auto`. |
| `partials` | body | object | no |  | Named templates for `{{> name}}` / `{% render "name" %}` (max 50). |
| `output` | body | string | no | `text` | `text` (no escaping) or `html` (values HTML-escaped). |
| `strict` | body | boolean | no | `false` | Liquid: error on undefined variables. |
| `delimiters` | body | string | no |  | Mustache: custom tags, e.g. `<% %>`. |
| `action` | body | string | no |  | `variables`: list the variables only. |
| `format` | body | string | no | `json` | `json` or `raw` (the text itself as text/plain or text/html). |

## Example request

```bash
curl -X POST "https://apibox.io.vn/v1/template-render" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"engine":"liquid","template":"Kính gửi {{ khach.ten }},\nĐơn {{ don.ma }} gồm {% for sp in don.items %}{{ sp.ten }} x{{ sp.sl }}{% unless forloop.last %}, {% endunless %}{% endfor %}.\nTổng: {{ don.tong | vnd }} ({{ don.tong | so_thanh_chu }}), hạn thanh toán {{ don.han | ngay_vn }}.","data":{"khach":{"ten":"Anh Minh"},"don":{"ma":"DH-1024","items":[{"ten":"Áo thun","sl":2},{"ten":"Mũ","sl":1}],"tong":450000,"han":"2026-11-20"}}}'
```

## Example response

```json
{
  "engine": "liquid",
  "output": "text",
  "result": "Kính gửi Anh Minh,\nĐơn DH-1024 gồm Áo thun x2, Mũ x1.\nTổng: 450.000 đ (Bốn trăm năm mươi nghìn đồng), hạn thanh toán 20/11/2026.",
  "length": 128,
  "variables": [
    {
      "name": "khach",
      "kind": "variable"
    },
    {
      "name": "don",
      "kind": "variable"
    }
  ]
}
```

## Use cases

- Compose dynamic e-mails, SMS or Zalo messages from Zapier, Make or n8n
- Mail-merge plain-text notices for a list of customers
- Render HTML snippets for invoices or receipts with VND amounts in words
- Check which variables a template expects before wiring data

## FAQ

### Mustache or Liquid?

Mustache is simpler and logic-less; Liquid adds if / for / assign and filters. Use engine=auto to let the API choose from the syntax.

### Is Handlebars supported?

Handlebars templates that only use Mustache syntax render with engine=mustache. Handlebars helpers are not supported because the Handlebars compiler generates code at run time.

### What happens with an unknown variable?

It renders as empty. With engine=liquid and strict=true an undefined variable returns a missing_variable error instead.
