# 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 { 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>'
```

## 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.sendping.co/docs/campaigns/managing).

## Next

- [Publish a template](https://www.sendping.co/docs/api/templates-publish) — required before any send can use it.
- [Send with a template](https://www.sendping.co/docs/templates/sending) — use a template in `POST /emails`.
- [Create template (API)](https://www.sendping.co/docs/api/templates-create) — full API reference.
