# Render a template (/docs/api-reference/email-templates/render-email-template)

Renders the draft (or a published `version`) with `variables`, exactly as a send would, and returns the subject, HTML and text: a preview, nothing is sent or stored. Without `variables` it renders the version's `sample_variables`.

`POST https://api.coss.com/v1/email_templates/{id}/render`

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).

## Path parameters

- `id` (string, required): The template's id or alias.

## Request body

- `version` (integer, at least 1): A published version to render. Default: the draft.
- `locale` (string, at most 35 characters): The locale to render, e.g. "pt-BR". Falls back to the language ("pt"), then to the default locale, as sends do.
- `variables` (map of any): Values by variable name, checked as a send checks them. Default: `sample_variables`.

## 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.render("etpl_01k7c8m4q2f3e9v6t5r1n0p8yz", {
  locale: "en",
  variables: {
    first_name: "Ada",
  },
});
```

HTTP:

```sh
curl -X POST "https://api.coss.com/v1/email_templates/etpl_01k7c8m4q2f3e9v6t5r1n0p8yz/render" \
  -H "Authorization: Bearer $COSS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "locale": "en",
  "variables": {
    "first_name": "Ada"
  }
}'
```

Python:

```python
from coss import Coss

client = Coss()  # api_key= or COSS_API_KEY

client.email.templates.render(
    id="etpl_01k7c8m4q2f3e9v6t5r1n0p8yz",
    locale="en",
    variables={
        "first_name": "Ada"
    },
)
```

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.RenderEmailTemplateRequest{
    ID: "etpl_01k7c8m4q2f3e9v6t5r1n0p8yz",
    Locale: coss.String(
        "en",
    ),
    Variables: map[string]any{
        "first_name": "Ada",
    },
}
client.Email.Templates.Render(
    context.TODO(),
    request,
)
```

Ruby:

```ruby
require "coss"

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

client.email.templates.render(
  id: "etpl_01k7c8m4q2f3e9v6t5r1n0p8yz",
  locale: "en",
  variables: {
    first_name: "Ada"
  }
)
```

PHP:

```php
<?php

use Coss\Coss;
use Coss\Email\Templates\Requests\RenderEmailTemplateRequest;

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

$client->email->templates->render(
    'etpl_01k7c8m4q2f3e9v6t5r1n0p8yz',
    new RenderEmailTemplateRequest([
        'locale' => 'en',
        'variables' => [
            'first_name' => "Ada",
        ],
    ]),
);
```

Java:

```java
import com.coss.Coss;
import com.coss.resources.email.templates.requests.RenderEmailTemplateRequest;
import java.util.HashMap;

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

client.email().templates().render(
    "etpl_01k7c8m4q2f3e9v6t5r1n0p8yz",
    RenderEmailTemplateRequest
        .builder()
        .locale("en")
        .variables(
            new HashMap<String, Object>() {{
                put("first_name", "Ada");
            }}
        )
        .build()
);
```

.NET:

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

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

await client.Email.Templates.RenderAsync(
    new RenderEmailTemplateRequest
    {
        Id = "etpl_01k7c8m4q2f3e9v6t5r1n0p8yz",
        Locale = "en",
        Variables = new Dictionary<string, object?>() { { "first_name", "Ada" } },
    }
);
```

## Response 200

The rendered template.

- `object` ("email_template_render", required)
- `template_id` (string, required): The template's id.
- `version` (integer | null, required): The version rendered; `null` for the draft.
- `locale` (string, required): The locale rendered.
- `subject` (string, required): The rendered subject.
- `html` (string | null, required): The rendered HTML. `null`: the template has none.
- `text` (string | null, required): The rendered plain text. `null`: the template has none.

Example:

```json
{
  "object": "email_template_render",
  "template_id": "etpl_01k7c8m4q2f3e9v6t5r1n0p8yz",
  "version": null,
  "locale": "en",
  "subject": "Welcome, Ada",
  "html": "<p>Hi Ada, thanks for signing up!</p>",
  "text": null
}
```

## 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`).
- `404`: No template with this id or alias exists in the API key's environment (`not_found`).
- `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`), the draft has problems (`template_invalid`, see `issues`), the variables don't match its declarations (`invalid_template_variables`), it has no such version (`template_version_not_found`), or it doesn't render (`template_render_failed`).
- `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`).