# 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 }`. A draft cannot be sent yet — call [`POST /templates/:id/publish`](https://www.sendping.co/docs/api/templates-publish) before referencing it as a `template_id`, or the send fails with `validation_error`.

**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. An array is stored — and returned by `GET /templates/:id` — as a single comma-separated string. 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 { SendPing } from 'sendping';

const mb = new SendPing('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 "sendping"

SendPing.api_key = "mb_xxxxxxxxx"

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

**PHP**

```php
<?php
require 'vendor/autoload.php';

use SendPing\SendPing;

$sendping = SendPing::client('mb_xxxxxxxxx');

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

**Python**

```python
import sendping

sendping.api_key = "mb_xxxxxxxxx"

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

**Go**

```go
import "github.com/shekhu10/sendping-sdks/sendping-go"

client := sendping.NewClient("mb_xxxxxxxxx")

template, err := client.Templates.Create(&sendping.CreateTemplateRequest{
    Name:    "Welcome email",
    Subject: "Welcome, {{first_name}}!",
    Html:    "<h1>Hi {{first_name}}</h1>",
})
```

**Rust**

```rust
use sendping::{CreateTemplateOptions, SendPing};

let mb = SendPing::new("mb_xxxxxxxxx");

let params = CreateTemplateOptions::new("Welcome email")
    .with_subject("Welcome, {{first_name}}!")
    .with_html("<h1>Hi {{first_name}}</h1>");
let _template = mb.templates.create(params).await?;
```

**Java**

```java
import co.sendping.SendPing;
import co.sendping.SendPingResponse;
import co.sendping.requests.CreateTemplateRequest;

SendPing sendping = new SendPing("mb_xxxxxxxxx");

CreateTemplateRequest request = CreateTemplateRequest.builder()
        .name("Welcome email")
        .subject("Welcome, {{first_name}}!")
        .html("<h1>Hi {{first_name}}</h1>")
        .build();

SendPingResponse response = sendping.templates().create(request);
```

**.NET**

```csharp
using SendPing;

ISendPing sendping = SendPingClient.Create("mb_xxxxxxxxx");

var resp = await sendping.TemplateCreateAsync(new TemplateCreateOptions
{
    Name = "Welcome email",
    Subject = "Welcome, {{first_name}}!",
    HtmlBody = "<h1>Hi {{first_name}}</h1>",
});
```

**cURL**

```bash
curl -X POST 'https://www.sendping.co/api/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
sendping templates create \
  --name 'Welcome email' \
  --subject 'Welcome, {{first_name}}!' \
  --html '<h1>Hi {{first_name}}</h1>'
```

### Field limits

These caps are enforced at the API boundary. Exceeding one returns a `validation_error` naming the field, e.g. *`subject` must be 998 characters or fewer.*

| Field | Maximum length |
| --- | --- |
| `name` | 255 |
| `subject` | 998 |
| `from` | 320 |
| `reply_to` | 320 |
| `alias` | 255 |

### Response

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

```json
{
  "object": "template",
  "id": "43f68331-0622-4e15-8202-246a0388854b"
}
```

> **Note:** Errors: `missing_required_field` if `name` is absent; `validation_error` if neither `html` nor `text` is provided, if any field above exceeds its maximum length, if the `alias` is already used by another of your templates, if `variables` is not an array or holds more than 50 entries, or if a variable uses a reserved key, a duplicate key, an invalid key, an unknown `type`, or a `fallback_value` that does not match its `type`. See [Errors](https://www.sendping.co/docs/api/errors).
