# Templates

A template holds the content of an email that you send many times. Design it once in the dashboard and send it from the API, a campaign or an automation.

## Create a Template

Open Templates and click New Template. Start from Blank Template or a starter such as Welcome, Verify Email, Password Reset or Receipt. You can also import HTML.

The editor saves as you type, and your team can edit at the same time. A template can also set the from address, reply-to, subject and preheader.

## Variables

A variable puts per-email data in the content, such as `{{first_name}}`. Set a fallback in the editor to use when a value is missing. Variables also work in the subject and preheader.

Contact properties use their key as the variable name. Campaign and automation emails also offer `{{unsubscribe_url}}` and `{{mailing_address}}`.

## Publish a Template

Edits stay in the draft until you click Publish, so a half-finished edit never reaches your contacts. API and automation sends use the published version. A template that was never published cannot send through them.

The API works the same way. `PATCH /v1/templates/{template_id}` changes the draft, and `POST /v1/templates/{template_id}/publish` makes the draft the published version. Test sends and previews use the draft. A new campaign copies the published version, or the draft if there is none.

## Version History

Inboxjot saves versions as you edit. Open History in the editor to view them, and restore any version. A restore keeps the current draft in history, and it reaches sends after you publish.

Name a version to keep it. Automatic versions expire with your plan's log retention, and named ones stay until you remove the name. A template or campaign can have 40 named versions.

## Send with a Template

Pass the template id and its variables in the `template` field of `POST /v1/emails`.

```bash cURL
curl -X POST https://api.inboxjot.com/v1/emails \
  -H "Authorization: Bearer $INBOXJOT_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "to": "riti@example.com",
    "template": {
      "id": "tpl_…",
      "variables": {
        "first_name": "Riti",
        "order_total": "$42.00"
      }
    }
  }'
```

```javascript Node.js
import { Inboxjot } from 'inboxjot';

const inboxjot = new Inboxjot();

await inboxjot.emails.send({
  to: 'riti@example.com',
  template: {
    id: 'tpl_…',
    variables: { first_name: 'Riti', order_total: '$42.00' },
  },
});
```

```rust Rust
use inboxjot::{Inboxjot, SendEmail, TemplateRef};
use serde_json::json;

let client = Inboxjot::from_env()?;

client
    .emails()
    .send(&SendEmail {
        to: vec!["riti@example.com".into()],
        template: Some(TemplateRef {
            id: "tpl_…".into(),
            variables: json!({ "first_name": "Riti", "order_total": "$42.00" })
                .as_object()
                .cloned()
                .unwrap_or_default(),
        }),
        ..Default::default()
    })
    .await?;
```

```go Go
client := inboxjot.NewClient("")

email, err := client.Emails.Send(ctx, inboxjot.SendEmailParams{
	To: []string{"riti@example.com"},
	Template: &inboxjot.Template{
		ID:        "tpl_…",
		Variables: map[string]any{"first_name": "Riti", "order_total": "$42.00"},
	},
})
```

```python Python
from inboxjot import Inboxjot

inboxjot = Inboxjot()

inboxjot.emails.send(
    {
        "to": "riti@example.com",
        "template": {
            "id": "tpl_…",
            "variables": {"first_name": "Riti", "order_total": "$42.00"},
        },
    }
)
```

> Fields in the request win over the template. The template content is used only
> when the request has no html and no text.
