apyhub
Back
▣ OUTPUT KIT

Validate & Generate Documents from Templates

What it does

The Document Template API fills DOCX, HTML and Markdown templates with JSON data. Send a template and the values it reads, and get back a finished DOCX, PDF or HTML document. A separate endpoint checks DOCX templates before you use them.

To check a template, send it to the validate endpoint with your data. It tells you if the template is valid. It also lists the variables it reads, keys missing from your data, keys it never uses, and syntax errors with line numbers.

To fill a Word template, send it as template_base64 with data. Set format to json (default), docx or pdf. To fill an HTML or Markdown template, send template with data. Add css, a title and page settings, and set markdown to false for plain HTML. format takes pdf (default), html or json. On both endpoints, download set to true returns the file itself. Otherwise you get a data_uri with the bytes, media_type, warnings and timing.

Teams use the Document Template API to fill Word templates for contracts and proposals. Others generate invoices and customer letters in their own design, or export reports to PDF from HTML templates.

For fixed layouts that need no template, use the Generate Invoice Document from JSON API or the Generate Report Document from JSON API.

▣ ENDPOINT 01 / 03
POST
Validate a DOCX template
https://api.eu.apyhub.com/callable-labs/validate-generate-documents-from-templates/v1/documents/validate

QUICKSTART

GUIDE

Quickstart

Validate a .docx template by uploading the template file and, optionally, a JSON string of template data.

curl -X POST "https://api.eu.apyhub.com/callable-labs/validate-generate-documents-from-templates/v1/documents/validate" \
  -H "apy-token: $APY_TOKEN" \
  -F "template=@/path/to/template.docx" \
  -F 'data={"name":"Ava","company":"ApyHub"}'

What you'll get back

Returns a JSON object with these top-level fields: ok boolean, engine string, unused array of strings, undefined array of strings, variables array of strings, and syntax_errors array of objects. ok is false when the template has syntax errors.

{
  "ok": true,
  "engine": "docxtemplater",
  "unused": [],
  "undefined": [],
  "variables": ["name", "company"],
  "syntax_errors": []
}
TRY ITLIVE · 30 ATOMS
Loading your default key…
The full key is used to call the gateway and stays in this tab — never sent to orbit or saved.
Max 100MB total per request (all files combined). Larger? Use this API's URL-based endpoint instead, if it has one.
Body_validate_template*
The .docx template to inspect
Optional JSON object to check against the template's variables

About this endpoint

What it does

Validates a .docx template and checks it against optional input data. It returns whether the template parsed successfully, plus details about template variables, unused input keys, undefined variables, and any syntax errors.

Request Body

ParameterTypeDescription
templateStringThe .docx template to inspect.
dataStringOptional JSON object to check against the template's variables. Can be null.

Response

Returns a JSON object with ok as a boolean field, engine as a string field, unused, undefined, and variables as string array fields, and syntax_errors as an array field of objects containing line as an integer and message as a string. ok is false when the template has syntax errors.

ParameterTypeDescription
okBooleanfalse when the template has syntax errors.
engineStringTemplate engine name.
unusedString ArrayKeys of data no tag reads.
undefinedString ArrayVariables data does not provide.
variablesString ArrayTop-level names the template reads.
syntax_errorsObject ArraySyntax errors found in the template, as objects with line and message fields.

Notes

The request body is unwrapped from the parser’s internal body envelope, so callers should send template and data as top-level body fields, not nested under a body object.

Max 100MB total per request (all files combined). Larger? Use this API's URL-based endpoint instead, if it has one.

▣ ENDPOINT 02 / 03
POST
Fill a DOCX template
https://api.eu.apyhub.com/callable-labs/validate-generate-documents-from-templates/v1/documents/render

QUICKSTART

GUIDE

Quickstart

Render a DOCX template from base64 and return the rendered result in the default JSON format.

curl -X POST "https://api.eu.apyhub.com/callable-labs/validate-generate-documents-from-templates/v1/documents/render" \
  -H "apy-token: $APY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "template_base64": "UEsDBBQAAAAIA...",
    "data": {
      "customer": { "name": "Ada Lovelace" },
      "items": [
        { "name": "Widget", "qty": 2, "price": 9.5 }
      ]
    }
  }'

What you'll get back

Returns a JSON object with rendering metadata such as bytes (integer), cache ("hit" or "miss"), pages (integer or null), engine (string), format ("docx", "pdf", or "html"), data_uri (string), warnings (array of strings), queued_ms (integer), render_ms (integer), media_type (string), and engine_version (string).

{
  "bytes": 12345,
  "cache": "miss",
  "pages": null,
  "engine": "libreoffice",
  "format": "docx",
  "data_uri": "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,...",
  "warnings": [],
  "queued_ms": 12,
  "render_ms": 45,
  "media_type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  "engine_version": "7.6.0"
}
TRY ITLIVE · 250 ATOMS
Loading your default key…
The full key is used to call the gateway and stays in this tab — never sent to orbit or saved.
DocxJsonRequest*
JSON alternative to the multipart form: the template as base64.
The .docx template, base64-encoded (≤ 5 MiB decoded).
Data
The values the template reads, as a JSON object (≤ 256 KiB, ≤ 20 levels deep). Every variable the template references must be present (`data_error` otherwise). For DOCX templates a value shaped `{"$image": "<base64 PNG/JPEG>", "width_mm": 40}` is inserted as a picture.
customer

About this endpoint

What it does

Fills a DOCX template using the values you provide in the request body. The endpoint can return the rendered file as DOCX or PDF, or as a JSON response containing a base64 data URI and metadata.

Request Body

ParameterTypeDescription
dataObjectThe values the template reads, as a JSON object. Every variable the template references must be present or the response will contain data_error. For DOCX templates, a value shaped {"$image": "<base64 PNG/JPEG>", "width_mm": 40} is inserted as a picture. Supports up to 256 KiB and up to 20 levels deep.
formatENUMOutput format. Allowed values: json (default), docx, pdf.
docx returns the filled document.
pdf converts the document with LibreOffice.
json returns the DOCX as a base64 data URI plus metadata.
downloadBooleanWhen true, sends Content-Disposition: attachment so browsers save the file. Default: false.
template_base64StringThe .docx template, base64-encoded. Maximum decoded size: 5 MiB.

Response

Returns a JSON object with metadata about the rendered output. The object includes bytes (integer), cache (string), pages (integer, nullable), engine (string), format (string), data_uri (string), warnings (array of strings), queued_ms (integer), render_ms (integer), media_type (string), and engine_version (string).

ParameterTypeDescription
bytesIntegerSize of the rendered output in bytes.
cacheENUMCache result. Allowed values: hit, miss.
pagesIntegerPDF page count. null for DOCX/HTML.
engineStringRendering engine used.
formatENUMOutput format. Allowed values: docx, pdf, html.
data_uriStringdata:<media_type>;base64,… representation of the rendered output.
warningsObject ArrayWarnings produced during rendering. The schema defines this as an array of strings.
queued_msIntegerTime spent queued, in milliseconds.
render_msIntegerTime spent rendering, in milliseconds.
media_typeStringMedia type of the rendered output.
engine_versionStringVersion of the rendering engine.
▣ ENDPOINT 03 / 03
POST
Render an HTML or Markdown template
https://api.eu.apyhub.com/callable-labs/validate-generate-documents-from-templates/v1/documents/html

QUICKSTART

GUIDE

Quickstart

Render a simple HTML template with the minimum required input.

curl -X POST "https://api.eu.apyhub.com/callable-labs/validate-generate-documents-from-templates/v1/documents/html" \
  -H "apy-token: $APY_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "template": "<h1>Hello {{ name }}</h1>",
    "data": {
      "name": "Ada"
    }
  }'

What you'll get back

Returns a JSON object with rendering metadata. Top-level fields include bytes (integer), cache (hit or miss), pages (integer or null), engine (string), format (docx, pdf, or html), data_uri (string), warnings (array of strings), queued_ms (integer), render_ms (integer), media_type (string), and engine_version (string).

{
  "bytes": 1234,
  "cache": "miss",
  "pages": null,
  "engine": "render-html-template",
  "format": "html",
  "data_uri": "data:text/html;base64,PGgxPkhlbGxvIEFkYTwvaDE+",
  "warnings": [],
  "queued_ms": 12,
  "render_ms": 34,
  "media_type": "text/html",
  "engine_version": "1.0.0"
}
TRY ITLIVE · 150 ATOMS
Loading your default key…
The full key is used to call the gateway and stays in this tab — never sent to orbit or saved.
HtmlRequest*
HTML or Markdown template + data → HTML or PDF.
Jinja2 template in HTML or Markdown (≤ 256 KiB). Values are HTML-escaped; use `{{ value | safe }}` to insert markup on purpose. A fragment is wrapped in a document shell with consistent fonts; a full `<html>` document is printed as-is.
Extra stylesheet appended to the document shell. External URLs are never fetched.
Data
The values the template reads, as a JSON object (≤ 256 KiB, ≤ 20 levels deep). Every variable the template references must be present (`data_error` otherwise). For DOCX templates a value shaped `{"$image": "<base64 PNG/JPEG>", "width_mm": 40}` is inserted as a picture.
PageOptions
Paper size and margins for PDF output.
Paper size.

About this endpoint

What it does

Renders a Jinja2 template from template using the values in data, with optional styling and page settings. Depending on format, it returns either the rendered HTML as a data URI with metadata, or the rendered file bytes for HTML/PDF output.

Request Body

ParameterTypeDescription
cssStringExtra stylesheet appended to the document shell. External URLs are never fetched.
dataObjectThe values the template reads, as a JSON object (≤ 256 KiB, ≤ 20 levels deep). Every variable the template references must be present (data_error otherwise). For DOCX templates a value shaped {"$image":"<base64 PNG/JPEG>","width_mm":40} is inserted as a picture.
pageObjectPaper size and margins for PDF output. Contains size, margin_mm, and orientation.
page.sizeENUMPaper size. Allowed values: A4, Letter, Legal, A3, A5. Default: A4.
page.margin_mmNumberUniform page margin in millimetres. Default: 18. Minimum: 0. Maximum: 50.
page.orientationENUMDefault: portrait. Allowed values: portrait, landscape.
titleString<title> of the document shell.
formatENUMDefault: json. Allowed values: json, html, pdf. html or pdf return the bytes; json returns the HTML as a data URI plus metadata.
downloadBooleanDefault: false. Send Content-Disposition: attachment so browsers save the file.
markdownBooleanDefault: false. Treat the rendered template as Markdown (CommonMark + tables) and convert it to HTML.
templateStringJinja2 template in HTML or Markdown (≤ 256 KiB). Values are HTML-escaped; use `{{ value

Response

Returns a JSON object with metadata about the rendered output. The top-level fields are bytes integer, cache string, pages integer or null, engine string, format string, data_uri string, warnings array of strings, queued_ms integer, render_ms integer, media_type string, and engine_version string.

ParameterTypeDescription
bytesInteger
cacheENUMAllowed values: hit, miss.
pagesIntegerPDF page count (null for DOCX/HTML).
engineString
formatENUMAllowed values: docx, pdf, html.
data_uriStringdata:<media_type>;base64,…
warningsString Array
queued_msInteger
render_msInteger
media_typeString
engine_versionString

Notes

format controls the response shape: json returns a data URI plus metadata, while html and pdf return bytes. If you render a fragment rather than a full <html> document, it is wrapped in a document shell with a pinned font stack; external stylesheet URLs are never fetched.

▣ COMMON ERRORS

Errors any endpoint can return

400bad_request

Required parameter missing or malformed body.

401unauthorized

API key missing, revoked, or not authorized for this service.

429rate_limited

Your plan's per-second rate exceeded. Retry with exponential backoff.

503upstream_busy

Backend temporarily unavailable. Try again in a few seconds.