> ## Documentation Index
> Fetch the complete documentation index at: https://docs.elasticfunnels.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Emails

> Manage email templates, access the visual builder, and retrieve automation variables

<Note>
  Email endpoints sit under `/api/brands/{brand}/emails/` with `moduleAccess:emails` middleware. Authenticate with `EF-Access-Key`.
</Note>

***

## List Emails

```
GET /api/brands/{brand}/emails
```

<ParamField query="per_page" type="number">
  Results per page (1–100, default: 25)
</ParamField>

<ParamField query="q" type="string">
  Search by name or subject
</ParamField>

<ParamField query="sort" type="string">
  `newest`, `oldest`, `created_at`, `updated_at`
</ParamField>

```bash cURL theme={null}
curl https://app.elasticfunnels.io/api/brands/{brand_id}/emails \
  -H "EF-Access-Key: your_api_key_here"
```

<ResponseExample>
  ```json Response theme={null}
  {
    "current_page": 1,
    "data": [
      {
        "id": 3,
        "code": "welcome-email",
        "name": "Welcome Email",
        "subject": "Welcome to our community!",
        "from_name": "Jane at MyBrand",
        "from_email": "hello@mybrand.com",
        "reply_to_email": null,
        "automation_id": 12,
        "created_at": "2024-11-01T00:00:00.000000Z"
      }
    ],
    "per_page": 25,
    "total": 7,
    "last_page": 1
  }
  ```
</ResponseExample>

***

## List Emails (Unpaginated)

Returns all emails with rendered HTML included (CSS is merged inline).

```
GET /api/brands/{brand}/emails/all
```

<ResponseExample>
  ```json Response theme={null}
  [
    {
      "id": 3,
      "name": "Welcome Email",
      "subject": "Welcome to our community!",
      "from_name": "Jane at MyBrand",
      "from_email": "hello@mybrand.com",
      "html": "<style>/* ... */</style><html>..."
    }
  ]
  ```
</ResponseExample>

***

## Get Email

```
GET /api/brands/{brand}/emails/{email}
```

<ParamField path="email" type="string" required>
  Email numeric ID or `code` slug
</ParamField>

<ResponseExample>
  ```json Response theme={null}
  {
    "id": 3,
    "code": "welcome-email",
    "name": "Welcome Email",
    "subject": "Welcome to our community!",
    "preview_text": "Thanks for joining!",
    "from_name": "Jane at MyBrand",
    "from_email": "hello@mybrand.com",
    "reply_to_email": null,
    "automation_id": 12,
    "variable_scope": null,
    "html": "...",
    "css": "...",
    "inline": null,
    "screenshot": null,
    "config": {}
  }
  ```
</ResponseExample>

***

## Create Email

```
POST /api/brands/{brand}/emails
```

<ParamField body="name" type="string" required>
  Email template name (max 256). Defaults to `"Untitled Email"` if empty after validation.
</ParamField>

<ParamField body="subject" type="string">
  Email subject line (max 512)
</ParamField>

<ParamField body="preview_text" type="string">
  Preheader / preview text (max 512)
</ParamField>

<ParamField body="from_name" type="string">
  Sender display name (max 256)
</ParamField>

<ParamField body="from_email" type="string">
  Sender email address (max 256)
</ParamField>

<ParamField body="reply_to_email" type="string">
  Reply-to address (max 256)
</ParamField>

<ParamField body="automation_id" type="number">
  Link this email to a specific automation
</ParamField>

```bash cURL theme={null}
curl -X POST https://app.elasticfunnels.io/api/brands/{brand_id}/emails \
  -H "EF-Access-Key: your_api_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Welcome Email",
    "subject": "Welcome to our community!",
    "from_name": "Jane at MyBrand",
    "from_email": "hello@mybrand.com",
    "automation_id": 12
  }'
```

<ResponseExample>
  ```json 200 Created theme={null}
  {
    "id": 3,
    "email": {
      "id": 3,
      "code": "welcome-email",
      "name": "Welcome Email",
      "subject": "Welcome to our community!",
      "from_name": "Jane at MyBrand",
      "from_email": "hello@mybrand.com",
      "automation_id": 12,
      "brand_id": 42,
      "created_at": "2024-12-11T10:00:00.000000Z"
    }
  }
  ```

  ```json 422 Missing required field theme={null}
  {
    "errors": {
      "name": ["The name field is required."]
    }
  }
  ```
</ResponseExample>

***

## Update Email

```
PUT /api/brands/{brand}/emails/{email}
```

<Warning>
  Use the **numeric ID** in the path when updating — using a `code` slug will cause a 404 because the `UpdateEmail` authorization query only looks up by numeric ID.
</Warning>

Same body as Create. `name` is required.

***

## Delete Email

```
DELETE /api/brands/{brand}/emails/{email}
```

<ResponseExample>
  ```json Response theme={null}
  { "success": true }
  ```
</ResponseExample>

***

## Get Builder Config

Retrieve the visual email builder config. Add `?html=1` to get the rendered HTML version.

```
GET /api/brands/{brand}/emails/{email}/builder
```

<ParamField query="html" type="boolean">
  If `1`, returns rendered HTML output instead of builder config
</ParamField>

```bash cURL — get builder config theme={null}
curl https://app.elasticfunnels.io/api/brands/{brand_id}/emails/3/builder \
  -H "EF-Access-Key: your_api_key_here"

# Get rendered HTML
curl "https://app.elasticfunnels.io/api/brands/{brand_id}/emails/3/builder?html=1" \
  -H "EF-Access-Key: your_api_key_here"
```

<ResponseExample>
  ```json Builder config response theme={null}
  {
    "assets": [ /* brand file assets */ ],
    "data": { /* email content blocks */ },
    "pageComponents": [ /* reusable components */ ],
    "fonts": [ "Montserrat", "Open Sans" ]
  }
  ```

  ```json ?html=1 response theme={null}
  {
    "html": "<!DOCTYPE html><html>...</html>",
    "config": "{ /* JSON string of config */ }",
    "inline": "<!DOCTYPE html><html>...</html>"
  }
  ```
</ResponseExample>

***

## Save Builder Config

```
POST /api/brands/{brand}/emails/{email}/builder
```

<ParamField body="config" type="string" required>
  Builder config as a **JSON string** (not a JSON object — must be `JSON.stringify`'d before sending)
</ParamField>

<ParamField body="html" type="string">
  Raw HTML content
</ParamField>

<ParamField body="css" type="string">
  CSS styles
</ParamField>

<ParamField body="comments" type="array">
  Comments data for the email template
</ParamField>

```bash cURL theme={null}
curl -X POST https://app.elasticfunnels.io/api/brands/{brand_id}/emails/3/builder \
  -H "EF-Access-Key: your_api_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "config": "{\"data\": {}}",
    "html": "<html>...</html>",
    "css": "body { font-family: sans-serif; }"
  }'
```

<ResponseExample>
  ```json Response theme={null}
  {
    "id": 3,
    "name": "Welcome Email"
  }
  ```
</ResponseExample>

***

## Get Automation Variables

Lists the merge-tag variables available for use in this email's content, based on the automation it belongs to.

```
GET /api/brands/{brand}/emails/{email}/automation-variables
```

<ResponseExample>
  ```json Email linked to automation theme={null}
  {
    "variables": [
      { "key": "{{first_name}}", "value": "First Name", "group": "Customer" },
      { "key": "{{product_name}}", "value": "Product Name", "group": "Order" },
      { "key": "{{order_total}}", "value": "Order Total", "group": "Order" }
    ],
    "automation": {
      "id": 12,
      "title": "Welcome Email Sequence"
    },
    "context": { /* automation context */ }
  }
  ```

  ```json No automation linked theme={null}
  {
    "variables": [],
    "automation": null,
    "context": {
      "source": "none",
      "title": "No automation linked",
      "badge_label": "No automation"
    }
  }
  ```
</ResponseExample>

***

## Notes

<Info>
  * The `config` field in the builder `POST` must be sent as a **JSON string** (double-encoded), not a JSON object
  * Saving the builder config automatically regenerates inline CSS and the `inline` version of the email
  * Use numeric IDs in the URL for `PUT` / `PATCH` — `code` strings only work for `GET`, `show`, and `DELETE`
  * The `all` endpoint merges CSS inline into the HTML for direct use in rendering/preview contexts
</Info>
