Templates from Markdown or HTML by API

Beta

This feature is in beta. For now it is available only to some users. We will release it gradually to more accounts once testing ends. If you want to use it, ask your JAAK contact for access.

Description

A text with tags can become a signature template. There are two routes:

RoutePurpose
POST /api/v1/templates/document-jobsDocument engine. Turns the text into a PDF and returns the area of each field. It does not create a template. Use it to check the result before you save it.
POST /api/v1/templates/sourceCreate template. Saves the text as a tagged template. Returns the template ID.

Both routes accept the same kind of text:

source_typeText format
markdownMarkdown.
editorHTML. This is the format of the platform editor.

The request body cannot be larger than 6 MiB. The content field cannot be larger than 5 MB.

For the tags ({{key}}, {{key|fecha}} and {{firma:ROLE}}), see Templates with tags (mail merge).

Use the company token in the Authorization: Bearer <token> header.

A. Document engine

The engine works in two steps: it receives a job and then you read the job result.

1. Send a job

POST /api/v1/templates/document-jobs

FieldRequiredDescription
nameYesJob name. It cannot be empty.
source_typeYesmarkdown or editor.
contentYesThe source text. It cannot be empty.
curl -X POST https://<host-api>/api/v1/templates/document-jobs \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Demo contract",
    "source_type": "markdown",
    "content": "# Demo contract\n\nClient: {{cliente.nombre}}\n\n{{firma:EL CLIENTE}}"
  }'

202 response:

{ "job_id": "job_demo_001" }

2. Read the job

GET /api/v1/templates/document-jobs/{job_id}

curl https://<host-api>/api/v1/templates/document-jobs/job_demo_001 \
  -H "Authorization: Bearer $TOKEN"

200 response:

FieldDescription
job_idJob ID.
statusqueued, running, done or failed.
reasonOnly in failed. Text in Spanish that explains the failure.
reason_codeOnly in failed. Stable code. Example: marker_not_found.
fieldsOnly in done. One entry per key, in the order of first appearance.
warningsNon-fatal notices in Spanish. Example: the text has no tags.
pdfOnly in done. The rendered PDF, in base64.

Each fields entry has key and areas. Each area is a rectangle:

{
  "job_id": "job_demo_001",
  "status": "done",
  "fields": [
    {
      "key": "cliente.nombre",
      "areas": [{ "x": 72.0, "y": 180.0, "w": 240.0, "h": 18.0, "page": 1 }]
    }
  ],
  "warnings": [],
  "pdf": "JVBERi0xLjQK..."
}
  • The coordinates are in PDF points.
  • The origin (0, 0) is at the top-left corner.
  • page starts at 1.

Read the job until status is done or failed. A failed job has no pdf.

B. Create a template from the text

POST /api/v1/templates/source

The body is JSON. The content field is the plain Markdown or HTML text. Do not encode it in base64.

FieldRequiredDescription
nameYesTemplate name.
source_typeYesmarkdown or editor.
contentYesThe source text with tags.
folder_nameNoFolder where the template is stored.
external_idNoIdentifier from your system.
signature_typeNoSignature type of the template. See Signature types.
role_signature_typesNoMap from role to signature type. Each value is a fixed type, at_send or empty. See Signature type per signer.
data_keysNoChanges to the label, type or required flag of the keys.
initialsNoInitials rule: enabled, roles and skip_pages.

Each data_keys item has:

FieldValues
keyA key that exists in the text.
labelVisible label.
typetext, multiline or date.
requiredtrue or false.

data_keys entries whose key is not in the text are rejected.

curl -X POST https://<host-api>/api/v1/templates/source \
  -H "Authorization: Bearer $TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Demo service contract",
    "source_type": "markdown",
    "content": "# Service contract\n\nBetween {{empresa.razon_social}} and {{cliente.nombre}}.\n\n{{firma:EL PRESTADOR}}\n\n{{firma:EL CLIENTE}}",
    "folder_name": "Demo contracts",
    "signature_type": "simple",
    "role_signature_types": {
      "EL PRESTADOR": "efirma_sat",
      "EL CLIENTE": "at_send"
    },
    "data_keys": [
      { "key": "empresa.razon_social", "label": "Company name", "type": "text", "required": true }
    ]
  }'

201 response: the template object, with id, slug and name. Save the id to send the template.

Errors

ResponseRouteCause
400 name es obligatorioJobname is missing.
400 source_type debe ser markdown o editorJobsource_type has another value.
400 content es obligatorioJobcontent is empty.
400 solicitud inválidaJobThe engine rejects the request.
400 cuerpo de la solicitud inválidoTemplateThe JSON body is not valid.
404 trabajo no encontradoJobThe job_id does not exist.
413BothThe body is larger than 6 MiB, or the content is larger than 5 MB.
503 servicio de generación de documentos no disponibleJobThe engine does not respond. Retry.
500 error interno del servidorJobUnexpected error. Contact JAAK.

Other template creation failures return the HTTP code of the template service error, with its text.

The error messages in the table come from the API and are in Spanish.

Next steps