> ## Documentation Index
> Fetch the complete documentation index at: https://docs.unosend.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Email Templates

> Learn how to create and use reusable email templates with dynamic variables for personalized email content.

## Why Use Templates?

![Templates](https://mintcdn.com/unosend/K3usqosZ4eI74Aej/images/templates.png?fit=max\&auto=format\&n=K3usqosZ4eI74Aej\&q=85\&s=81113d951013b06e07ac8575b8f90af7)

* **Consistency** - Maintain brand consistency across all emails
* **Efficiency** - Write once, use many times
* **Personalization** - Dynamic variables for each recipient
* **Easy Updates** - Change template once, affects all future emails

## Creating a Template

Create a template with dynamic variables using double curly braces:

```bash cURL theme={null}
curl -X POST https://api.unosend.co/templates \
  -H "Authorization: Bearer un_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Welcome Email",
    "subject": "Welcome to {{company_name}}, {{first_name}}!",
    "html": "<!DOCTYPE html><html><body><h1>Welcome, {{first_name}}!</h1><p>Thanks for joining {{company_name}}.</p><a href=\"{{dashboard_url}}\">Go to Dashboard</a></body></html>",
    "text": "Welcome, {{first_name}}! Thanks for joining {{company_name}}. Visit: {{dashboard_url}}"
  }'
```

```javascript JavaScript theme={null}
await fetch('https://api.unosend.co/templates', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer un_your_api_key',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    name: 'Welcome Email',
    subject: 'Welcome to {{company_name}}, {{first_name}}!',
    html: '<!DOCTYPE html><html><body><h1>Welcome, {{first_name}}!</h1><p>Thanks for joining {{company_name}}.</p><a href="{{dashboard_url}}">Go to Dashboard</a></body></html>',
    text: 'Welcome, {{first_name}}! Thanks for joining {{company_name}}. Visit: {{dashboard_url}}'
  })
});
```

```python Python theme={null}
import requests

requests.post('https://api.unosend.co/templates',
  headers={'Authorization': 'Bearer un_your_api_key'},
  json={
    'name': 'Welcome Email',
    'subject': 'Welcome to {{company_name}}, {{first_name}}!',
    'html': '<!DOCTYPE html><html><body><h1>Welcome, {{first_name}}!</h1><p>Thanks for joining {{company_name}}.</p><a href="{{dashboard_url}}">Go to Dashboard</a></body></html>',
    'text': 'Welcome, {{first_name}}! Thanks for joining {{company_name}}. Visit: {{dashboard_url}}'
  }
)
```

### Response

```json response.json theme={null}
{
  "id": "tpl_xxxxxxxxxxxxxxxx",
  "name": "Welcome Email",
  "subject": "Welcome to {{company_name}}, {{first_name}}!",
  "variables": ["company_name", "first_name", "dashboard_url"],
  "created_at": "2024-01-15T10:30:00Z"
}
```

## Variable Syntax

Templates support various variable features:

### Basic Variables

```html basic-variables.html theme={null}
Hello, {{first_name}}!
Your email is: {{email}}
```

### Default Values

```html default-values.html theme={null}
Hello, {{first_name | default: "there"}}!
Your plan: {{plan | default: "Free"}}
```

### Conditional Content

```html conditionals.html theme={null}
{{#if is_premium}}
<div class="premium-badge">Premium Member</div>
{{/if}}

{{#if has_items}}
<h2>Your Items</h2>
{{else}}
<p>No items yet. Start shopping!</p>
{{/if}}
```

### Loops

```html loops.html theme={null}
<h2>Your Order</h2>
<table>
  {{#each items}}
  <tr>
    <td>{{name}}</td>
    <td>{{quantity}}</td>
    <td>{{price}}</td>
  </tr>
  {{/each}}
</table>
<p>Total: {{total}}</p>
```

## Sending Emails with Templates

Use `template_id` instead of `html` when sending:

```bash cURL theme={null}
curl -X POST https://api.unosend.co/emails \
  -H "Authorization: Bearer un_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "from": "hello@yourdomain.com",
    "to": ["john@example.com"],
    "template_id": "tpl_xxxxxxxxxxxxxxxx",
    "variables": {
      "first_name": "John",
      "company_name": "Acme Inc",
      "dashboard_url": "https://app.acme.com/dashboard",
      "is_premium": true
    }
  }'
```

## Batch Sending with Templates

Send personalized emails to multiple recipients efficiently using the batch endpoint:

```bash cURL theme={null}
curl -X POST https://api.unosend.co/emails/batch \
  -H "Authorization: Bearer un_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "emails": [
      {
        "from": "hello@yourdomain.com",
        "to": ["john@example.com"],
        "template_id": "tpl_xxxxxxxxxxxxxxxx",
        "variables": {
          "first_name": "John",
          "plan": "Pro"
        }
      },
      {
        "from": "hello@yourdomain.com",
        "to": ["jane@example.com"],
        "template_id": "tpl_xxxxxxxxxxxxxxxx",
        "variables": {
          "first_name": "Jane",
          "plan": "Free"
        }
      }
    ]
  }'
```

## Common Template Examples

### Order Confirmation

```html order-confirmation.html theme={null}
<h1>Order Confirmed!</h1>
<p>Hi {{customer_name}},</p>
<p>Thanks for your order #{{order_number}}.</p>

<h2>Order Details</h2>
<table>
  <tr>
    <th>Item</th>
    <th>Qty</th>
    <th>Price</th>
  </tr>
  {{#each items}}
  <tr>
    <td>{{name}}</td>
    <td>{{quantity}}</td>
    <td>{{price}}</td>
  </tr>
  {{/each}}
</table>

<p><strong>Total: {{total}}</strong></p>
<p>Shipping to: {{shipping_address}}</p>
```

### Password Reset

```html password-reset.html theme={null}
<h1>Reset Your Password</h1>
<p>Hi {{first_name | default: "there"}},</p>
<p>We received a request to reset your password.</p>

<a href="{{reset_url}}" 
   style="display: inline-block; padding: 12px 24px; 
          background: #000; color: #fff; text-decoration: none; 
          border-radius: 6px;">
  Reset Password
</a>

<p style="margin-top: 20px; color: #666; font-size: 13px;">
  This link will expire in {{expiry_hours}} hours.
</p>

<p style="color: #666; font-size: 12px;">
  If you didn't request this, please ignore this email.
</p>
```

## Managing Templates

### Update a Template

```bash cURL theme={null}
curl -X PATCH https://api.unosend.co/templates/tpl_xxxxxxxx \
  -H "Authorization: Bearer un_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "subject": "Welcome aboard, {{first_name}}!",
    "html": "<h1>Updated welcome content</h1>"
  }'
```

### List Templates

```bash cURL theme={null}
curl https://api.unosend.co/templates \
  -H "Authorization: Bearer un_your_api_key"
```

### Get a Template

```bash cURL theme={null}
curl https://api.unosend.co/templates/tpl_xxxxxxxx \
  -H "Authorization: Bearer un_your_api_key"
```

### Delete a Template

```bash cURL theme={null}
curl -X DELETE https://api.unosend.co/templates/tpl_xxxxxxxx \
  -H "Authorization: Bearer un_your_api_key"
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.