# Templates

> Save reusable email content and render it with variables at send time.

A **template** is reusable email content — a name plus an optional `subject`, `html`, and `text` body. Instead of hard-coding the same markup in your app, save it once as a template and reference it by `id` when you send. Templates support `{{ variable }}` placeholders that you fill per send.

## Create a template

Create templates from the dashboard (**Templates → New**) or via the API. Provide a `name` and at least one of `html` or `text`; `subject` is optional but recommended.

**Node.js**

```js
import { MailBlastr } from 'mailblastr';

const mb = new MailBlastr('mb_xxxxxxxxx');

const { data, error } = await mb.templates.create({
  "name": "Welcome email",
  "subject": "Welcome, {{first_name}}!",
  "html": "<h1>Hi {{first_name}} 👋</h1>"
});
console.log({ data, error });
```

**Ruby**

```ruby
require "mailblastr"

Mailblastr.api_key = "mb_xxxxxxxxx"

Mailblastr::Templates.create({
  "name": "Welcome email",
  "subject": "Welcome, {{first_name}}!",
  "html": "<h1>Hi {{first_name}} 👋</h1>"
})
```

**PHP**

```php
$mailblastr = Mailblastr::client('mb_xxxxxxxxx');

$mailblastr->templates->create([
  'name' => "Welcome email",
  'subject' => "Welcome, {{first_name}}!",
  'html' => "<h1>Hi {{first_name}} 👋</h1>"
]);
```

**Python**

```python
import mailblastr

mailblastr.api_key = "mb_xxxxxxxxx"

mailblastr.Templates.create({
  "name": "Welcome email",
  "subject": "Welcome, {{first_name}}!",
  "html": "<h1>Hi {{first_name}} 👋</h1>"
})
```

**Go**

```go
package main

import (
    "fmt"
    "io"
    "net/http"
    "strings"
)

func main() {
    req, _ := http.NewRequest("POST", "https://api.mailblastr.com/templates", strings.NewReader(`{
  "name": "Welcome email",
  "subject": "Welcome, {{first_name}}!",
  "html": "<h1>Hi {{first_name}} 👋</h1>"
}`))
    req.Header.Set("Authorization", "Bearer mb_xxxxxxxxx")
    req.Header.Set("Content-Type", "application/json")
    res, _ := http.DefaultClient.Do(req)
    defer res.Body.Close()
    out, _ := io.ReadAll(res.Body)
    fmt.Println(string(out))
}
```

**Rust**

```rust
use reqwest::Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let res = Client::new()
        .post("https://api.mailblastr.com/templates")
        .header("Authorization", "Bearer mb_xxxxxxxxx")
        .header("Content-Type", "application/json")
        .body(r#"{
  "name": "Welcome email",
  "subject": "Welcome, {{first_name}}!",
  "html": "<h1>Hi {{first_name}} 👋</h1>"
}"#)
        .send()
        .await?;
    println!("{}", res.text().await?);
    Ok(())
}
```

**Java**

```java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder()
    .uri(URI.create("https://api.mailblastr.com/templates"))
    .header("Authorization", "Bearer mb_xxxxxxxxx")
    .header("Content-Type", "application/json")
    .method("POST", HttpRequest.BodyPublishers.ofString("""
{
  "name": "Welcome email",
  "subject": "Welcome, {{first_name}}!",
  "html": "<h1>Hi {{first_name}} 👋</h1>"
}
"""))
    .build();
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
```

**.NET**

```csharp
using System.Net.Http;
using System.Text;

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://api.mailblastr.com/templates");
request.Headers.Add("Authorization", "Bearer mb_xxxxxxxxx");
request.Content = new StringContent(@"{
  ""name"": ""Welcome email"",
  ""subject"": ""Welcome, {{first_name}}!"",
  ""html"": ""<h1>Hi {{first_name}} 👋</h1>""
}", Encoding.UTF8, "application/json");
var response = await client.SendAsync(request);
Console.WriteLine(await response.Content.ReadAsStringAsync());
```

**cURL**

```bash
curl -X POST 'https://api.mailblastr.com/templates' \
  -H 'Authorization: Bearer mb_xxxxxxxxx' \
  -H 'Content-Type: application/json' \
  -d '{
  "name": "Welcome email",
  "subject": "Welcome, {{first_name}}!",
  "html": "<h1>Hi {{first_name}} 👋</h1>"
}'
```

**CLI**

```bash
mailblastr templates create \
  --name 'Welcome email' \
  --subject 'Welcome, {{first_name}}!' \
  --html '<h1>Hi {{first_name}} 👋</h1>'
```

## Variables

Anywhere in the `subject`, `html`, or `text`, write `{{ key }}` and pass matching `variables` when sending. Whitespace inside the braces is ignored, so `{{first_name}}` and `{{ first_name }}` are equivalent. Unknown placeholders render as an empty string, so a recipient never sees a raw `{{ ... }}` tag.

A template may contain up to **50 variables**. We recommend capitalizing keys (e.g. `PRODUCT_NAME`). Each variable can declare a `type` of `string` or `number` and an optional `fallback_value` — the value used when you don't supply one at send time. If a variable has no fallback and you omit its value, the send fails with a validation error.

> **Warning:** These variable names are reserved and cannot be used: `FIRST_NAME`, `LAST_NAME`, `EMAIL`, `UNSUBSCRIBE_URL`, `contact`, `this`.

> **Note:** Variables are simple string substitution — there is no logic (no conditionals or loops). For per-recipient marketing merge tags in bulk sends, see [Campaigns](https://www.mailblastr.com/docs/campaigns/managing).

## Next

- [Send with a template](https://www.mailblastr.com/docs/templates/sending) — use a template in `POST /emails`.
- [Create template (API)](https://www.mailblastr.com/docs/api/templates-create) — full API reference.
