# Create template

> POST /templates — save a reusable template.

`POST /templates`

Creates a template with `status: "draft"`. Provide a `name` and at least one of `html` or `text`. Returns `{ object: "template", id }`. Use `POST /templates/:id/publish` to publish the draft.

**Body parameters**

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `name` | string | Yes | A name for the template (shown in the dashboard). |
| `alias` | string | No | A unique, human-readable handle. Pass `alias` instead of `id` wherever a template id is accepted (e.g. `template_id` on sends, `GET /templates/:id`). Must be unique across your templates. |
| `subject` | string | No | Default subject line. Supports `{{ variables }}`. Can be overridden when sending. |
| `html` | string | No | HTML body. Required if `text` is omitted. Supports `{{ variables }}`. |
| `text` | string | No | Plain-text body. Required if `html` is omitted. Supports `{{ variables }}`. If omitted, a plain-text version is generated from the HTML; set it to an empty string to opt out. |
| `from` | string | No | Default sender address. Use `"Your Name <sender@domain.com>"` for a friendly name. Can be overridden when sending. |
| `reply_to` | string | string[] | No | Default Reply-To address, or an array of addresses. Can be overridden when sending. |
| `variables` | array | No | The variables used in the template (up to 50). Each is an object with `key`, `type`, and an optional `fallback_value` — see below. |

**variables[] object**

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `key` | string | Yes | The variable key. We recommend capitalizing it (e.g. `PRODUCT_NAME`). Reserved names that cannot be used: `FIRST_NAME`, `LAST_NAME`, `EMAIL`, `UNSUBSCRIBE_URL`, `contact`, `this`. Keys must start with a letter or underscore and contain only letters, digits, underscores, or dots. |
| `type` | 'string' | 'number' | No | The type of the variable. Defaults to `string` when omitted. |
| `fallback_value` | string | number | null | No | The value used when none is supplied at send time. Must match `type`. If omitted, a value for this variable is required when sending. |

**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>'
```

### Response

Returns `{ object: "template", id }` — a slim acknowledgement. Fetch the full object with `GET /templates/:id`.

```json
{
  "object": "template",
  "id": "tmpl_8f5c2a1e"
}
```

> **Note:** Errors: `missing_required_field` if `name` is absent; `validation_error` if neither `html` nor `text` is provided, or if a variable uses a reserved key or a `fallback_value` that does not match its `type`.
