# Embedding in Your ERP

> Show a customer's tickets, a request form or one ticket inside your own app with embed tokens and the <lemu-tickets> element

URL: https://docs.lemuai.com/tickets-api/embedding

The `<lemu-tickets>` element puts LemuTickets screens inside your ERP, customer portal or website, without you building them. It shows one customer (a requester) their own tickets, lets them raise a new one, and lets them reply. Your agents see and answer everything in LemuTickets as usual.

It works in two steps:

1. **Your server** asks the API for a short-lived **embed token** for the signed-in customer.
2. **Your page** loads the script and adds a `<lemu-tickets>` element with that token.

**Warning:** Never put an API key in a web page. An embed token is safe to put in the page: it only works for one requester, only on the embed screens, only from the sites you list, and only for a short time.

## 1. Get an embed token on your server

**Endpoint:** `POST /v1/embed/tokens`

Call this with an [API key](https://docs.lemuai.com/tickets-api/authentication) or OAuth token that has the `tickets.write` scope.

| Field | Type | Required | Description |
|---|---|---|---|
| `requester_id` | string (UUID) | One of the three | The LemuTickets requester id. |
| `requester_external_id` | string | One of the three | Your own id for the customer, as set with [Upsert a requester by external ID](https://docs.lemuai.com/tickets-api/requesters/upsert-by-external-id). |
| `requester` | object | One of the three | `email`, `phone` and `name`. Finds the requester by email or phone, or creates one. Needs an `email` or a `phone`. |
| `scope` | string | No | What the token allows. `list` (default), `create` or `ticket`. See below. |
| `ticket_key` | string | For `ticket` | The ticket to show, for example `SUP-1042`. It must belong to this requester. |
| `project` | string | No | Project key or id for tickets the customer creates. Without it, new tickets go to your first open service desk project. |
| `ttl_minutes` | integer | No | How long the token lasts, `1` to `1440` minutes. Default `60`. |
| `allowed_origins` | array of strings | No | The sites allowed to use the token, as origins like `https://erp.example.com` (no path). Strongly recommended. Empty means any site. |

| Scope | The customer can |
|---|---|
| `list` | See all their tickets, open any of them, reply, and raise new tickets |
| `create` | Raise new tickets only |
| `ticket` | Open and reply on the one ticket in `ticket_key` |

**Example request:**
```bash
curl -X POST https://api.tickets.lemuai.com/v1/embed/tokens \
  -H "Authorization: Bearer ltk_live_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "requester_external_id": "CUST-20931",
    "scope": "list",
    "project": "SUP",
    "ttl_minutes": 60,
    "allowed_origins": ["https://erp.example.com"]
  }'
```

**`201`**
```json
{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "expires_at": "2026-11-25T10:12:40.512000+00:00",
  "scope": "list",
  "requester_id": "8b4d6f2a-0c1e-4a3b-9d5f-7e6a8c0b2d4f",
  "ticket_key": null,
  "allowed_origins": ["https://erp.example.com"],
  "script_url": "https://api.tickets.lemuai.com/v1/public/embed.js",
  "snippet": "<script src=\"https://api.tickets.lemuai.com/v1/public/embed.js\" async></script>\n<lemu-tickets token=\"eyJhbGciOi...\" view=\"list\"></lemu-tickets>"
}
```

`snippet` is ready-made HTML for this token.

| Status | `code` | When |
|---|---|---|
| `422` | `missing_requester` | None of `requester_id`, `requester_external_id` or `requester` (with an email or phone) was sent |
| `422` | `invalid_requester` | No requester with that id or external id |
| `422` | `requester_unavailable` | The requester's self-service access is turned off, or their data was anonymised |
| `422` | `missing_ticket_key` | `scope` is `ticket` but there is no `ticket_key` |
| `422` | `ticket_not_requesters` | The ticket belongs to another requester |
| `422` | `invalid_origin` | An entry in `allowed_origins` is not an origin like `https://erp.example.com` |

## 2. Add the element to your page

Load the script once per page, then add an element for each screen:
```html
<script src="https://api.tickets.lemuai.com/v1/public/embed.js" async></script>

<!-- The customer's tickets (token with scope "list") -->
<lemu-tickets token="TOKEN_FROM_YOUR_SERVER" view="list"></lemu-tickets>

<!-- A "raise a request" form (token with scope "create") -->
<lemu-tickets token="TOKEN_FROM_YOUR_SERVER" view="create"></lemu-tickets>

<!-- One ticket with its replies (token with scope "ticket") -->
<lemu-tickets token="TOKEN_FROM_YOUR_SERVER" view="ticket" ticket="SUP-1042"></lemu-tickets>
```

| Attribute | Description |
|---|---|
| `token` | The embed token from step 1. Required. |
| `view` | `list` (default), `create` or `ticket`. It should match the token's scope. |
| `ticket` | The ticket key, for `view="ticket"`. |
| `status` | Optional, for `view="list"`. `open` shows every ticket that is not done. Otherwise a comma-separated list of status categories, for example `waiting` or `new,open`. |

The customer only ever sees their own tickets and public replies. Internal notes stay hidden. Tickets they raise have the source `portal`.

## Refreshing tokens

Tokens expire (after 60 minutes by default). Ask your server for a new token each time the page loads. If the page stays open a long time, fetch a new token before `expires_at` and set it on the element:
```js
document.querySelector("lemu-tickets").setAttribute("token", newToken);
```

Changing an attribute redraws the element.

## Events

The element sends DOM events you can listen to, for example to refresh your own screen:

| Event | `event.detail` |
|---|---|
| `lemu:ticket-created` | The new ticket: `id`, `key`, `title`, `status`, `status_category`, `priority`, `type`, `created_at` and more |
| `lemu:reply-sent` | The reply: `id`, `ticket_key`, `body_text`, `created_at` |
| `lemu:error` | `{ code, message }`, for example `embed_token_expired` when the token ran out |
```js
document.addEventListener("lemu:error", (e) => {
  if (e.detail.code === "embed_token_expired") refreshToken();
});
```

## Styling

The element draws inside its own shadow DOM, so your page's styles do not leak into it. Set these CSS variables on the element to match your brand:
```css
lemu-tickets {
  --accent: #0b6e4f;
  --accent-hover: #095c42;
  --ink: #1d1d1f;
  --slate: #6b6b73;
  --mist: #f4f4f5;
  --line: #ececee;
  --radius: 12px;
}
```

## When something goes wrong

| What you see | Why |
|---|---|
| "Missing token." | The `token` attribute is empty. |
| `network_error` ("Could not reach the support service.") | Often the page's origin is not in the token's `allowed_origins`, so the browser blocks the response. Origins must match exactly, including `https` and any port. |
| `embed_token_expired` | The token ran out. Get a new one from your server. |
| `embed_scope` | The token's scope does not allow this view, for example a `create` token used with `view="list"`. |
