# Create a template

> Creates a template as a draft. Variables use triple braces, {{{ORDER_ID}}} or {{{NAME|fallback}}}, in subject, html and text; declare up to 50 typed variables (string or number) with optional…

Source: https://docs.pidgely.com/api-reference/templates/create/

`POST https://api.pidgely.com/templates`

Creates a template as a draft. Variables use triple braces, `{{{ORDER_ID}}}` or `{{{NAME|fallback}}}`, in `subject`, `html` and `text`; declare up to 50 typed variables (`string` or `number`) with optional fallbacks. `FIRST_NAME`, `LAST_NAME`, `EMAIL` and `UNSUBSCRIBE_URL` are reserved. [Publish](https://docs.pidgely.com/api-reference/templates/publish/) the draft before sending with it.

**Authorization:** `Bearer pdg_…`. An API key with `full_access`. A `sending_access` key gets `401 restricted_api_key`.

## Body parameters

JSON object.

| Name | Type | Required | Description |
|---|---|---|---|
| `name` | string | No | Display name. |
| `alias` | string | No | Unique, human-readable key you can use instead of the id: `^[A-Za-z0-9][A-Za-z0-9_.-]{0,99}$`, not a UUID. |
| `from` | string | No | — |
| `subject` | string | No | Subject line, up to 998 bytes. Required unless a template provides it. |
| `reply_to` | string \| string[] | No | Reply-To address or list of addresses. |
| `html` | string | No | HTML body. `html` or `text` is required (unless you send with a template). |
| `text` | string | No | — |
| `variables` | object[] | No | Typed variables (up to 50). `FIRST_NAME`, `LAST_NAME`, `EMAIL` and `UNSUBSCRIBE_URL` are reserved. |
| `variables[].key` | string | Yes | Variable name, `^[A-Za-z0-9_]{1,50}$`. |
| `variables[].type` | string | Yes | `string` or `number`. One of `string`, `number`. |
| `variables[].fallback_value` | string \| number | No | Used when a send does not provide the variable. Must match the type. |

## Request

```js tab="Node.js"
import { Pidgely } from 'pidgely';

const pidgely = new Pidgely(process.env.PIDGELY_API_KEY);

const { data, error } = await pidgely.templates.create({
  name: 'Order confirmation',
  alias: 'order-confirmation',
  from: 'Acme <orders@acme.com>',
  subject: 'Order {{{ORDER_ID}}} confirmed',
  html: '<p>Hi {{{CUSTOMER_NAME|there}}}, your order {{{ORDER_ID}}} totals {{{TOTAL}}}.</p>',
  variables: [
    {
      key: 'ORDER_ID',
      type: 'string',
    },
    {
      key: 'TOTAL',
      type: 'number',
      fallbackValue: 0,
    },
    {
      key: 'CUSTOMER_NAME',
      type: 'string',
      fallbackValue: 'there',
    },
  ],
});
console.log(data ?? error);
```

```python tab="Python"
import os
import pidgely

pidgely.api_key = os.environ["PIDGELY_API_KEY"]

result = pidgely.Templates.create({
    "name": "Order confirmation",
    "alias": "order-confirmation",
    "from": "Acme <orders@acme.com>",
    "subject": "Order {{{ORDER_ID}}} confirmed",
    "html": "<p>Hi {{{CUSTOMER_NAME|there}}}, your order {{{ORDER_ID}}} totals {{{TOTAL}}}.</p>",
    "variables": [
        {
            "key": "ORDER_ID",
            "type": "string",
        },
        {
            "key": "TOTAL",
            "type": "number",
            "fallback_value": 0,
        },
        {
            "key": "CUSTOMER_NAME",
            "type": "string",
            "fallback_value": "there",
        },
    ],
})
print(result)
```

```php tab="PHP"
<?php

require __DIR__ . '/vendor/autoload.php';

$pidgely = Pidgely::client(getenv('PIDGELY_API_KEY'));

$result = $pidgely->templates->create([
    'name' => 'Order confirmation',
    'alias' => 'order-confirmation',
    'from' => 'Acme <orders@acme.com>',
    'subject' => 'Order {{{ORDER_ID}}} confirmed',
    'html' => '<p>Hi {{{CUSTOMER_NAME|there}}}, your order {{{ORDER_ID}}} totals {{{TOTAL}}}.</p>',
    'variables' => [
        [
            'key' => 'ORDER_ID',
            'type' => 'string',
        ],
        [
            'key' => 'TOTAL',
            'type' => 'number',
            'fallback_value' => 0,
        ],
        [
            'key' => 'CUSTOMER_NAME',
            'type' => 'string',
            'fallback_value' => 'there',
        ],
    ],
]);
var_dump($result);
```

```go tab="Go"
package main

import (
	"context"
	"fmt"
	"os"

	"github.com/pidgely/pidgely-go"
)

func main() {
	ctx := context.Background()
	client := pidgely.NewClient(os.Getenv("PIDGELY_API_KEY"))

	result, err := client.Templates.Create(ctx, &pidgely.CreateTemplateRequest{
		Name:      "Order confirmation",
		Alias:     "order-confirmation",
		From:      "Acme <orders@acme.com>",
		Subject:   "Order {{{ORDER_ID}}} confirmed",
		Html:      "<p>Hi {{{CUSTOMER_NAME|there}}}, your order {{{ORDER_ID}}} totals {{{TOTAL}}}.</p>",
		Variables: []pidgely.TemplateVariableInput{
		{
			Key:  "ORDER_ID",
			Type: "string",
		},
		{
			Key:           "TOTAL",
			Type:          "number",
			FallbackValue: 0,
		},
		{
			Key:           "CUSTOMER_NAME",
			Type:          "string",
			FallbackValue: "there",
		},
	},
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(result)
}
```

```ruby tab="Ruby"
require "pidgely"

Pidgely.api_key = ENV["PIDGELY_API_KEY"]

result = Pidgely::Templates.create({
  name: "Order confirmation",
  alias: "order-confirmation",
  from: "Acme <orders@acme.com>",
  subject: "Order {{{ORDER_ID}}} confirmed",
  html: "<p>Hi {{{CUSTOMER_NAME|there}}}, your order {{{ORDER_ID}}} totals {{{TOTAL}}}.</p>",
  variables: [
    {
      key: "ORDER_ID",
      type: "string"
    },
    {
      key: "TOTAL",
      type: "number",
      fallback_value: 0
    },
    {
      key: "CUSTOMER_NAME",
      type: "string",
      fallback_value: "there"
    }
  ]
})
puts result
```

```bash tab="cURL"
curl -X POST 'https://api.pidgely.com/templates' \
  -H "Authorization: Bearer $PIDGELY_API_KEY" \
  -H 'Content-Type: application/json' \
  -d '{
  "name": "Order confirmation",
  "alias": "order-confirmation",
  "from": "Acme <orders@acme.com>",
  "subject": "Order {{{ORDER_ID}}} confirmed",
  "html": "<p>Hi {{{CUSTOMER_NAME|there}}}, your order {{{ORDER_ID}}} totals {{{TOTAL}}}.</p>",
  "variables": [
    {
      "key": "ORDER_ID",
      "type": "string"
    },
    {
      "key": "TOTAL",
      "type": "number",
      "fallback_value": 0
    },
    {
      "key": "CUSTOMER_NAME",
      "type": "string",
      "fallback_value": "there"
    }
  ]
}'
```

## Response

`200 OK`

```json
{
  "object": "template",
  "id": "0199d7f7-7e2f-7b5c-8a4d-1c9e3f6a0b27"
}
```

### Response fields

| Name | Type | Required | Description |
|---|---|---|---|
| `object` | string | Yes | Object type. |
| `id` | string | Yes | Unique id (UUID). UUID. |

## Errors

Errors have the shape `{"statusCode", "name", "message"}`. See [Errors](https://docs.pidgely.com/errors/) for the full list.

| Status | Name | When |
|---|---|---|
| 401 | `missing_api_key`, `restricted_api_key` | No `Authorization: Bearer` header, or a `sending_access` key called an endpoint that needs `full_access`. |
| 403 | `invalid_api_key`, `validation_error` | The API key is invalid or revoked, or the request is not allowed for this account (unverified domain, plan limit, paused account). |
| 404 | `not_found` | The resource does not exist in this account (or the id is not a valid UUID). |
| 422 | `validation_error` | A field is missing or invalid; `message` names it. |
| 429 | `rate_limit_exceeded` | Too many requests per second. Wait for `Retry-After` seconds. |
| 409 | `alias_taken` | Another template in the account already uses this alias. |
