# Create an email template (/docs/api-reference/email-templates/create-email-template)

Creates a template with a draft holding its variables and a subject, HTML and text per locale. Sends use it once its draft is published. Not idempotent: retrying after a lost response can create a second template (unless it has an `alias`). Templates belong to the API key's own environment: a `coss_test_…` key only ever sees sandbox ones. Give a template the same `alias` in every environment to send it by that name.

`POST https://api.coss.com/v1/email_templates`

Authenticate with your API key as a bearer token (`Authorization: Bearer coss_test_…`). Responses are JSON with snake_case fields; errors are `{ error: { type, code, message, param?, issues?, request_id } }` (https://coss.com/docs/api-reference/errors.md).

## Request body

- `name` (string, required, 1-100 characters): Shown in the dashboard.
- `alias` (string): A name to send the template by instead of its id, e.g. `welcome`: the same alias can be used in every environment.
- `description` (string, at most 500 characters): Your note about it, shown in the dashboard.
- `default_locale` (string, at most 35 characters): The locale sends fall back to when they ask for none, or for one without content. Defaults to the first entry of `contents` (or "en").
- `variables` (object[], at most 50 items): The variables the template uses, with their types.
  - `name` (string, required): Used in the template as `{{name}}`.
  - `type` ("string" | "number" | "boolean" | "list", required): `string`, `number`, `boolean`, or `list` (an array of objects with the declared `fields`, looped over with `{{#each name}}`).
  - `required` (boolean, default true): Sends must pass it unless it has a `default`. Defaults to true.
  - `default` (string | number | boolean): Used when a send leaves it out. Not for lists.
  - `description` (string, at most 500 characters): What it's for, shown in the dashboard.
  - `fields` (object[], 1-20 items): A list's item fields (required for lists, only for lists).
    - `name` (string, required): Used inside `{{#each}}` as `{{name}}`.
    - `type` ("string" | "number" | "boolean", required): The field's type.
    - `required` (boolean, default true): Whether each item must have it. Defaults to true.
- `sample_variables` (map of any): Example values for previews and test sends in the dashboard.
- `contents` (object[], at most 30 items): The subject, HTML and text per locale.
  - `locale` (string, required, at most 35 characters): The BCP 47 language tag this content is for, unique in `contents`.
  - `subject` (string, required, 1-2000 characters): The subject template.
  - `html` (string | null, at most 524288 characters): The HTML template. Provide `html`, `text`, or both.
  - `text` (string | null, at most 262144 characters): The plain-text template.

## Example request

TypeScript (the `coss` package):

```ts
import { Coss } from "coss";

const coss = new Coss({
  apiKey: process.env.COSS_API_KEY,
  baseUrl: "https://api.coss.com",
});

const { data, error } = await coss.email.templates.create({
  name: "Welcome",
  alias: "welcome",
  default_locale: "en",
  variables: [
    {
      name: "first_name",
      type: "string",
      required: true,
    },
  ],
  sample_variables: {
    first_name: "Ada",
  },
  contents: [
    {
      locale: "en",
      subject: "Welcome, {{first_name}}",
      html: "<p>Hi {{first_name}}, thanks for signing up!</p>",
    },
    {
      locale: "de",
      subject: "Willkommen, {{first_name}}",
      html: "<p>Hallo {{first_name}}, danke für deine Anmeldung!</p>",
    },
  ],
});
```

HTTP:

```sh
curl -X POST "https://api.coss.com/v1/email_templates" \
  -H "Authorization: Bearer $COSS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "Welcome",
  "alias": "welcome",
  "default_locale": "en",
  "variables": [
    {
      "name": "first_name",
      "type": "string",
      "required": true
    }
  ],
  "sample_variables": {
    "first_name": "Ada"
  },
  "contents": [
    {
      "locale": "en",
      "subject": "Welcome, {{first_name}}",
      "html": "<p>Hi {{first_name}}, thanks for signing up!</p>"
    },
    {
      "locale": "de",
      "subject": "Willkommen, {{first_name}}",
      "html": "<p>Hallo {{first_name}}, danke für deine Anmeldung!</p>"
    }
  ]
}'
```

Python:

```python
from coss import Coss
from coss import Coss, TemplateVariableInput, TemplateContentInput

client = Coss()  # api_key= or COSS_API_KEY

client.email.templates.create(
    name="Welcome",
    alias="welcome",
    default_locale="en",
    variables=[
        TemplateVariableInput(
            name="first_name",
            type="string",
            required=True,
        )
    ],
    sample_variables={
        "first_name": "Ada"
    },
    contents=[
        TemplateContentInput(
            locale="en",
            subject="Welcome, {{first_name}}",
            html="<p>Hi {{first_name}}, thanks for signing up!</p>",
        ),
        TemplateContentInput(
            locale="de",
            subject="Willkommen, {{first_name}}",
            html="<p>Hallo {{first_name}}, danke für deine Anmeldung!</p>",
        )
    ],
)
```

Go:

```go
import (
	"context"

	coss "github.com/cosscom/coss-go"
	cossclient "github.com/cosscom/coss-go/client"
	email "github.com/cosscom/coss-go/email"
)

client := cossclient.NewClient() // option.WithAPIKey(…) or COSS_API_KEY

request := &email.CreateEmailTemplateRequest{
    Name: "Welcome",
    Alias: coss.String(
        "welcome",
    ),
    DefaultLocale: coss.String(
        "en",
    ),
    Variables: []*coss.TemplateVariableInput{
        &coss.TemplateVariableInput{
            Name: "first_name",
            Type: coss.TemplateVariableInputTypeString,
            Required: coss.Bool(
                true,
            ),
        },
    },
    SampleVariables: map[string]any{
        "first_name": "Ada",
    },
    Contents: []*coss.TemplateContentInput{
        &coss.TemplateContentInput{
            Locale: "en",
            Subject: "Welcome, {{first_name}}",
            HTML: coss.String(
                "<p>Hi {{first_name}}, thanks for signing up!</p>",
            ),
        },
        &coss.TemplateContentInput{
            Locale: "de",
            Subject: "Willkommen, {{first_name}}",
            HTML: coss.String(
                "<p>Hallo {{first_name}}, danke für deine Anmeldung!</p>",
            ),
        },
    },
}
client.Email.Templates.Create(
    context.TODO(),
    request,
)
```

Ruby:

```ruby
require "coss"

client = Coss::Client.new # api_key: or COSS_API_KEY

client.email.templates.create(
  name: "Welcome",
  alias_: "welcome",
  default_locale: "en",
  variables: [{
    name: "first_name",
    type: "string",
    required: true
  }],
  sample_variables: {
    first_name: "Ada"
  },
  contents: [{
    locale: "en",
    subject: "Welcome, {{first_name}}",
    html: "<p>Hi {{first_name}}, thanks for signing up!</p>"
  }, {
    locale: "de",
    subject: "Willkommen, {{first_name}}",
    html: "<p>Hallo {{first_name}}, danke für deine Anmeldung!</p>"
  }]
)
```

PHP:

```php
<?php

use Coss\Coss;
use Coss\Email\Templates\Requests\CreateEmailTemplateRequest;
use Coss\Types\TemplateContentInput;
use Coss\Types\TemplateVariableInput;
use Coss\Types\TemplateVariableInputType;

$client = new Coss(); // apiKey: or COSS_API_KEY

$client->email->templates->create(
    new CreateEmailTemplateRequest([
        'name' => 'Welcome',
        'alias' => 'welcome',
        'defaultLocale' => 'en',
        'variables' => [
            new TemplateVariableInput([
                'name' => 'first_name',
                'type' => TemplateVariableInputType::String->value,
                'required' => true,
            ]),
        ],
        'sampleVariables' => [
            'first_name' => "Ada",
        ],
        'contents' => [
            new TemplateContentInput([
                'locale' => 'en',
                'subject' => 'Welcome, {{first_name}}',
                'html' => '<p>Hi {{first_name}}, thanks for signing up!</p>',
            ]),
            new TemplateContentInput([
                'locale' => 'de',
                'subject' => 'Willkommen, {{first_name}}',
                'html' => '<p>Hallo {{first_name}}, danke für deine Anmeldung!</p>',
            ]),
        ],
    ]),
);
```

Java:

```java
import com.coss.Coss;
import com.coss.resources.email.templates.requests.CreateEmailTemplateRequest;
import com.coss.types.TemplateContentInput;
import com.coss.types.TemplateVariableInput;
import com.coss.types.TemplateVariableInputType;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Optional;

Coss client = Coss.builder().build(); // .apiKey(…) or COSS_API_KEY

client.email().templates().create(
    CreateEmailTemplateRequest
        .builder()
        .name("Welcome")
        .alias("welcome")
        .defaultLocale("en")
        .variables(
            Optional.of(
                Arrays.asList(
                    TemplateVariableInput
                        .builder()
                        .name("first_name")
                        .type(TemplateVariableInputType.STRING)
                        .required(true)
                        .build()
                )
            )
        )
        .sampleVariables(
            new HashMap<String, Object>() {{
                put("first_name", "Ada");
            }}
        )
        .contents(
            Optional.of(
                Arrays.asList(
                    TemplateContentInput
                        .builder()
                        .locale("en")
                        .subject("Welcome, {{first_name}}")
                        .html(
                            OptionalNullable.of("<p>Hi {{first_name}}, thanks for signing up!</p>")
                        )
                        .build(),
                    TemplateContentInput
                        .builder()
                        .locale("de")
                        .subject("Willkommen, {{first_name}}")
                        .html(
                            OptionalNullable.of("<p>Hallo {{first_name}}, danke für deine Anmeldung!</p>")
                        )
                        .build()
                )
            )
        )
        .build()
);
```

.NET:

```csharp
using Coss;
using Coss.Email;

var client = new CossClient(); // apiKey: or COSS_API_KEY

await client.Email.Templates.CreateAsync(
    new CreateEmailTemplateRequest
    {
        Name = "Welcome",
        Alias = "welcome",
        DefaultLocale = "en",
        Variables = new List<TemplateVariableInput>()
        {
            new TemplateVariableInput
            {
                Name = "first_name",
                Type = TemplateVariableInputType.String,
                Required = true,
            },
        },
        SampleVariables = new Dictionary<string, object?>() { { "first_name", "Ada" } },
        Contents = new List<TemplateContentInput>()
        {
            new TemplateContentInput
            {
                Locale = "en",
                Subject = "Welcome, {{first_name}}",
                Html = "<p>Hi {{first_name}}, thanks for signing up!</p>",
            },
            new TemplateContentInput
            {
                Locale = "de",
                Subject = "Willkommen, {{first_name}}",
                Html = "<p>Hallo {{first_name}}, danke für deine Anmeldung!</p>",
            },
        },
    }
);
```

## Response 200

The template.

- `object` ("email_template", required)
- `id` (string, required)
- `alias` (string | null, required): A name to send the template by instead of its id. `null`: none.
- `name` (string, required): Shown in the dashboard.
- `description` (string | null, required): Your note about it. `null`: none.
- `published_version` (integer | null, required): The version sends use; `null` until the draft is first published.
- `published_at` (string | null, required, date-time): When the published version was published. `null`: never.
- `has_unpublished_changes` (boolean, required): Whether the draft was changed since it was last published.
- `created_at` (string, required, date-time)
- `updated_at` (string, required, date-time)

Example:

```json
{
  "object": "email_template",
  "id": "etpl_01k7c8m4q2f3e9v6t5r1n0p8yz",
  "alias": "welcome",
  "name": "Welcome",
  "description": "Sent after sign-up.",
  "published_version": 2,
  "published_at": "2026-10-10T12:00:00.000Z",
  "has_unpublished_changes": false,
  "created_at": "2026-10-01T09:00:00.000Z",
  "updated_at": "2026-10-10T12:00:00.000Z"
}
```

## Errors

- `400`: The body is not valid JSON (`invalid_json`). Also when the `Coss-Version` header names an unknown API version (`invalid_api_version`).
- `401`: The API key is missing, invalid, revoked or expired (`authentication_error`).
- `403`: The API key lacks the permission this endpoint needs (`permission_error`), or its organization is suspended (`organization_suspended`).
- `413`: The request body is larger than 5 MB (`payload_too_large`).
- `415`: The body is not sent as `application/json` (`unsupported_media_type`).
- `422`: The body failed validation (`validation_error`), another template has the alias (`template_alias_taken`), or the environment has 500 templates (`template_limit_reached`).
- `429`: Too many requests with this API key (`rate_limited`), or too many requests with invalid keys from this address (`too_many_failed_authentications`). Retry after `Retry-After` seconds. Successful responses carry `RateLimit-Limit`, `RateLimit-Remaining` and `RateLimit-Reset` (seconds until the limit is fully restored).
- `500`: Unexpected server error (`api_error` / `internal_error`).
- `503`: The API key could not be verified right now; retry (`service_unavailable`).