# Templates from code

Designers build templates in Relevate; your code sends them with the values that
change, like an order number or a sign-in link.

## Send a template [#send-a-template]

```json
{
  "to": "jane@example.com",
  "template": {
    "id": "order-shipped",
    "variables": { "order": { "id": "A-2", "total": 42 } }
  }
}
```

- `id`: the template's alias (like `order-shipped`) or ID. Copy either from the
  template's **⋯ → Developers** menu.
- `variables`: values for the template's declared variables. Dots nest:
  `{"order": {"total": 42}}` fills `{{order.total}}`.
- `version`: optional, to pin a published version.

The sender, type and purpose come from the template. Only the published version is
sent. Every missing, unknown or mistyped value is named in one `422
invalid_variables`, before anything is sent.

## Manage templates [#manage-templates]

| | |
|---|---|
| `GET /email/templates` | Published templates with their variables |
| `GET /email/templates/{id}` | One template; `?draft=true` for the draft |
| `POST /email/templates` | Create a draft from HTML |
| `PATCH /email/templates/{id}` | Change the draft |
| `POST /email/templates/{id}/publish` | Publish, with the same checks as the dashboard |
| `POST /email/templates/{id}/duplicate` | Copy into a new draft |
| `POST /email/templates/{id}/render` | See the finished email for given values, without sending |
| `DELETE /email/templates/{id}` | Delete a draft, or archive a published template |

Variables are `{name, type, required, fallback, example, description}`; `key` and
`fallback_value` are accepted too.
