# Markdown templates and fields

A Markdown or HTML document is text, not a fixed page. Signers on phones read it as a responsive
page, the signed copy is printed to PDF, and you change wording without moving any fields. This
guide covers the syntax; [Extensions](https://docs.signapi.dev/extensions) gives the overview.

## A first document

```json
{
  "key": "nda",
  "title": "Mutual NDA",
  "format": "markdown",
  "content": "# Mutual NDA\n\nThis agreement is between Example Ltd and {{client_name}}.\n\nPhone `{\"line_type\": \"text-input\", \"signer_field_id\": \"phone\"}`",
  "data": { "client_name": "Acme Inc." }
}
```

With no signature place in the text, Sign API adds a signature block at the end with a signature,
name and date for every signer. Give a document exactly one of `content` (up to 1 MB), `url` (an
upload or an allowed host) or `template_id`.

## Placeholders

`{{key}}` is replaced from the document's `data` before anyone signs. Keys match
`^[a-z][a-z0-9_]*$`. A value can be:

| Value | Inserted as |
|---|---|
| `"Acme Inc."` | Plain text, escaped |
| `{"markdown": "## Terms\n- Net 30"}` | Formatted Markdown |
| `{"html": "<p>…</p>"}` | HTML, cleaned like an HTML document (HTML documents only) |
| `{"template_id": "tpl_…"}` | That template's content, up to 20 levels deep |

A placeholder with no value is refused with
[`template_data_missing`](https://docs.signapi.dev/errors/template_data_missing), and templates that insert each other in
a loop with [`template_cycle`](https://docs.signapi.dev/errors/template_cycle).

## Line configs

A JSON object in backticks at the very end of a line changes that line. For formatting:

```markdown
# Services agreement `{"alignment": "center"}`
## Scope `{"header_counter": "yes"}`
```

`alignment` is `left`, `center`, `right` or `justify`; `header_counter: "yes"` numbers headings
automatically (1, 1.1, 1.2, 2…).

## Signer fields

With `line_type`, the line becomes a field, and the text before the config is its label:

```markdown
Full name `{"line_type": "text-input", "signer_field_id": "full_name", "input_required": "yes"}`
Start date `{"line_type": "date", "signer_field_id": "start_date"}`
Device `{"line_type": "select-input", "signer_field_id": "device", "select_values": "Laptop\nDesktop"}`
I accept the terms `{"line_type": "checkbox", "signer_field_id": "accept"}`
Monthly `{"line_type": "radiobutton", "signer_field_id": "plan_monthly"}`
Yearly `{"line_type": "radiobutton", "signer_field_id": "plan_yearly"}`
Copy of ID `{"line_type": "file-upload", "signer_field_id": "id_copy"}`
```

| `line_type` | Becomes the place | Notes |
|---|---|---|
| `text-input` | `text_input` | `placeholder_text` is the hint; `masked: "yes"` hides the value from other signers and in the PDF. |
| `text-area` | `multiline_text_input` | 4 lines. |
| `date` | `text_input` | A date picker; the value is `YYYY-MM-DD`. |
| `select-input` | `dropdown` | `select_values` lists the options, one per line. |
| `checkbox` | `checkbox` | |
| `radiobutton` | `radio_group` | Consecutive lines form one group; each line's `signer_field_id` is an option. |
| `file-upload` | `file_upload` | The signer attaches a PDF, PNG or JPEG (up to 10 MB), added to the signed PDF. |

Other keys:

| Key | Meaning |
|---|---|
| `signer_field_id` | The place `key`, and the name in `captures` unless `capture_as` is set. |
| `input_required` | `yes` or `no` (default `no`). |
| `input_type` | Who fills it in: `only_first_signer`, `only_second_signer`, `only_last_signer` or `every_signer` (one place per signer, keyed `<id>__<recipient_key>`). Signers are counted in recipient order. |
| `recipient_key` | Assign the field to one recipient by key instead of `input_type`. |
| `default_value` | The preselected option of a `select-input` or radio group. |
| `capture_as` | The name in `captures`. |
| `format`, `format_message` | Check a `text-input`, as on a [`text_input` place](https://docs.signapi.dev/places). |

Fields can't sit inside tables, headings or links, and a document may hold up to 200; a bad
config is refused with [`field_config_invalid`](https://docs.signapi.dev/errors/field_config_invalid), naming the line.
Templates written for eSignatures.com's Extended Markdown work unchanged; `recipient_key`,
`capture_as` and `format` are Sign API additions.

## Your own signature block

To place signatures yourself, write `[[place_key]]` in the text and define the place in the
document's `places`, as for a PDF:

```json
{
  "format": "markdown",
  "content": "Signed for the client:\n\n[[client_sig]]",
  "places": [{ "key": "client_sig", "type": "signature", "recipient_key": "client" }]
}
```

The automatic signature block is left out once the document has its own signature place.

## HTML documents

With `format: "html"`, fields are elements whose attributes are the same keys in kebab case:

```html
<p>Phone <lumin-field type="text-input" signer-field-id="phone" required></lumin-field></p>
```

An empty `<lumin-field>` takes the text before it as its label. If your HTML tool drops unknown
elements, put `data-lumin-field='{"line_type": "text-input", …}'` on a block element instead.
HTML is cleaned before use: scripts, forms, frames, external styles and fonts are removed, and
styles that would hide or move text are dropped, so signers always see every word. Images must be
`https` or `data:` PNG, JPEG, GIF or WebP. HTML with nothing usable left is refused with
[`html_rejected`](https://docs.signapi.dev/errors/html_rejected).

## Store templates

[Create a template](https://docs.signapi.dev/api/CreateTemplate) stores a source with a `title` and `format`, and returns
the placeholders and fields it found. Use it in a document with `template_id` and fill it with
`data`. Templates need no recipients: `input_type` is resolved when an envelope uses one.

- [Edit template content](https://docs.signapi.dev/api/EditTemplateContent) applies find-and-replace edits; send
  `dry_run: true` first to see how many times each `find` matched.
- [Duplicate a template](https://docs.signapi.dev/api/DuplicateTemplate) copies it, with `data` filling some placeholders
  in the copy.
- [Preview a template](https://docs.signapi.dev/api/PreviewTemplate) renders it to PDF.

Templates belong to one mode: a test key sees test templates, a live key live ones.
