# Create or replace a template

`PUT /v1/templates/{slug}`

- Authentication: required (Bearer token)
- Required scope: `api.templates.manage`

Creates the template named by `{slug}`, or replaces it if it exists.
This is the endpoint to push rendered email from a build: it is
idempotent, hash-guarded, and it deploys to SES before it answers.

**It is a full replacement, not a patch.** An omitted field is the new
value, not "leave it alone" — send the whole body every time. (This
differs from `PUT /v1/segments/{id}`, which merges; a template is
rendered whole by whatever produced it.)

**Idempotent.** If the stored content, subject, `default_topic` and
`default_consent_class` already match what you are sending, the
response is `200` with `unchanged: true` and NOTHING is written: no
version snapshot, no SES call. A pipeline that runs on every commit can
push every template every time.

**`format` is required** and `handlebars` is the only accepted value;
anything else is `422 unsupported_format` with the accepted values in
`accepted_formats`. The field exists so that accepting other source
formats later is an added value rather than a breaking change. Render
your source (React Email, MJML, anything) to SES-subset Handlebars
before pushing it.

**Concurrency.** Send `expected_content_hash` (the `content_hash` from
a previous read) to make the write conditional. If the stored hash has
moved on, the write is refused with `409 conflict` carrying the row's
current `content_hash`, so a retry is one re-read away. Omit it and the
last write wins. The hash covers the BODY only — a subject-only edit
does not move it, so two callers changing only the subject cannot
detect each other. That matches the MCP `templates_author` tool
exactly, deliberately: one concurrency rule, not two.

**Deploy.** A successful write pushes the template to the org's SES
account within a bounded budget and reports what happened in
`deploy_state`: `deployed` (live and sendable on return), `failed`
(the transform pipeline blocked it — the content is still saved, and
`deploy_error` says why), or `pending` (the budget ran out, SES errored,
or the org has no AWS connection yet; a background job will finish it).
Replacing an already-deployed template reports `pending` until the new
body lands, even though the template is still sendable with its
previous content — those are two different questions and this field
answers the one about what you just pushed.

**Naming.** `{slug}` is the template's name, and it must be something
SES can hold: letters, numbers, `_` and `-`. A name with a dot in it is
refused (`422`) rather than deployed, because SES rejects it on every
attempt. A `name` in the body is accepted only when it equals the slug;
this route does not rename, since the name is what every broadcast and
every workflow `send` step resolves through. A slug differing from a
stored template only in case updates that template and keeps its stored
spelling.

**`requires[]`** reports what this template still needs: whether its
`default_topic` names a topic that exists, and whether every
`{{asset}}` path resolves. It is a different question from
`deploy_state`, which is only about this row reaching SES.

## Path parameters

- `slug` (string, required) — Template name (e.g. `welcome`).

## Request body

Content type: `application/json`

```json
{
  "name": "string",
  "subject": "string",
  "body": "string",
  "format": "handlebars",
  "default_topic": "string",
  "default_consent_class": "",
  "expected_content_hash": "string"
}
```

## Example request

```bash
curl -X PUT 'https://api.sendops.dev/v1/templates/string' \
  -H "Authorization: Bearer $SENDOPS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"name":"string","subject":"string","body":"string","format":"handlebars","default_topic":"string","default_consent_class":"","expected_content_hash":"string"}'
```

## Responses

### 200 — The replaced template, or the unchanged one

Content type: `application/json`

```json
{
  "slug": "string",
  "name": "string",
  "subject": "string",
  "channel": "string",
  "content_hash": "string",
  "deploy_state": "pending",
  "ses_template_name": "string",
  "deploy_error": "string",
  "deployed_at": "2026-05-17T20:00:00Z",
  "default_topic": "string",
  "default_consent_class": "",
  "variables": [
    "string"
  ],
  "last_modified_at": "2026-05-17T20:00:00Z",
  "requires": [
    {
      "kind": "template",
      "key": "string",
      "status": "ok",
      "detail": "string",
      "fix": "string"
    }
  ],
  "created": true,
  "unchanged": true,
  "validation": {
    "valid": true,
    "variables": [
      "string"
    ]
  }
}
```

### 201 — The created template

Content type: `application/json`

```json
{
  "slug": "string",
  "name": "string",
  "subject": "string",
  "channel": "string",
  "content_hash": "string",
  "deploy_state": "pending",
  "ses_template_name": "string",
  "deploy_error": "string",
  "deployed_at": "2026-05-17T20:00:00Z",
  "default_topic": "string",
  "default_consent_class": "",
  "variables": [
    "string"
  ],
  "last_modified_at": "2026-05-17T20:00:00Z",
  "requires": [
    {
      "kind": "template",
      "key": "string",
      "status": "ok",
      "detail": "string",
      "fix": "string"
    }
  ],
  "created": true,
  "unchanged": true,
  "validation": {
    "valid": true,
    "variables": [
      "string"
    ]
  }
}
```

### 401 — Missing, malformed, or unknown API key

Content type: `application/problem+json`

```json
{
  "type": "https://example.com",
  "title": "string",
  "status": 0,
  "detail": "string",
  "code": "invalid_key",
  "request_id": "string",
  "retry_after": 0,
  "retention_days": 0,
  "scope": "string",
  "resource": "string",
  "errors": [
    {
      "field": "string",
      "reason": "string"
    }
  ],
  "attribute_id": "00000000-0000-0000-0000-000000000000",
  "content_hash": "string",
  "differs": [
    "string"
  ]
}
```

### 403 — Key lacks the required scope or plan limit violated

Content type: `application/problem+json`

```json
{
  "type": "https://example.com",
  "title": "string",
  "status": 0,
  "detail": "string",
  "code": "invalid_key",
  "request_id": "string",
  "retry_after": 0,
  "retention_days": 0,
  "scope": "string",
  "resource": "string",
  "errors": [
    {
      "field": "string",
      "reason": "string"
    }
  ],
  "attribute_id": "00000000-0000-0000-0000-000000000000",
  "content_hash": "string",
  "differs": [
    "string"
  ]
}
```

### 409 — The write was refused by a state rule. Three cases, told apart by the extension members on the Problem: a stale `expected_content_hash` carries the row's current `content_hash`; a delete blocked by a live dependent carries `referenced_by`.

Content type: `application/problem+json`

```json
{
  "type": "https://example.com",
  "title": "string",
  "status": 0,
  "detail": "string",
  "code": "invalid_key",
  "request_id": "string",
  "retry_after": 0,
  "retention_days": 0,
  "scope": "string",
  "resource": "string",
  "errors": [
    {
      "field": "string",
      "reason": "string"
    }
  ],
  "attribute_id": "00000000-0000-0000-0000-000000000000",
  "content_hash": "string",
  "differs": [
    "string"
  ],
  "referenced_by": [
    {
      "kind": "workflow",
      "key": "string",
      "name": "string"
    }
  ]
}
```

### 422 — A query parameter, path value or body field failed validation.

The body is a `validation_failed` Problem. When the refusal is about a
reference — a segment key, template slug, topic or attribute name the
organization does not have — it additionally carries `validation_code`,
`field`, `line`/`column`, `missing`, `candidates` and `next_step`, so a
client can correct the call without a second round of guessing. See
`ValidationProblem`.

Content type: `application/problem+json`

```json
{
  "type": "https://example.com",
  "title": "string",
  "status": 0,
  "detail": "string",
  "code": "invalid_key",
  "request_id": "string",
  "retry_after": 0,
  "retention_days": 0,
  "scope": "string",
  "resource": "string",
  "errors": [
    {
      "field": "string",
      "reason": "string"
    }
  ],
  "attribute_id": "00000000-0000-0000-0000-000000000000",
  "content_hash": "string",
  "differs": [
    "string"
  ],
  "validation_code": "invalid_syntax",
  "field": "string",
  "line": 0,
  "column": 0,
  "missing": [
    {
      "kind": "segment",
      "key": "string"
    }
  ],
  "candidates": {},
  "next_step": "string"
}
```

### 429 — Per-org rate limit exceeded

Content type: `application/problem+json`

```json
{
  "type": "https://example.com",
  "title": "string",
  "status": 0,
  "detail": "string",
  "code": "invalid_key",
  "request_id": "string",
  "retry_after": 0,
  "retention_days": 0,
  "scope": "string",
  "resource": "string",
  "errors": [
    {
      "field": "string",
      "reason": "string"
    }
  ],
  "attribute_id": "00000000-0000-0000-0000-000000000000",
  "content_hash": "string",
  "differs": [
    "string"
  ]
}
```

### 500 — Unexpected server-side failure. The `code` is `internal_error`. The
`request_id` field can be quoted to SendOps support to investigate.

Content type: `application/problem+json`

```json
{
  "type": "https://example.com",
  "title": "string",
  "status": 0,
  "detail": "string",
  "code": "invalid_key",
  "request_id": "string",
  "retry_after": 0,
  "retention_days": 0,
  "scope": "string",
  "resource": "string",
  "errors": [
    {
      "field": "string",
      "reason": "string"
    }
  ],
  "attribute_id": "00000000-0000-0000-0000-000000000000",
  "content_hash": "string",
  "differs": [
    "string"
  ]
}
```
