> For the complete documentation index, see [llms.txt](https://docs.toucanai.cloud/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.toucanai.cloud/embed/embedding-overview/how-to/embed-an-ai-chat.md).

# Embed an AI Chat

{% hint style="info" %}
**Target Audience**: Developers
{% endhint %}

### Goal

Embed a self-service AI chat from Toucan AI into a host application to enable conversational analytics.

***

### Prerequisites

* At least one [connected and active database](/build/data-connections/how-to/add-a-database.md).
* A [valid API key](/embed/authentication/how-to/generate-an-api-key.md) for token generation.
* (Recommended): [Enriched metadata](/build/analyze-your-database-with-ai/how-to/analyze-your-database-with-ai.md) to improve the AI assistant's accuracy.
* (Recommended): [Row-Level Security (RLS) configured](/embed/permissions-and-row-level-security/how-to/apply-rls-to-your-database.md) for multi-tenant data isolation.

***

### Steps

#### 1. Prepare the data layer

* Execute an AI analysis on your datasets to generate semantic metadata.
* Review column descriptions and metric definitions to ensure the AI assistant has sufficient context.
* High-quality metadata directly improves the relevance of natural language answers.

#### 2. Configure security and RLS

* Define required token attributes (e.g., `customer_id` or `region`).
* Map these attributes to specific dataset columns using the RLS interface.
* Validate the configuration with different attribute values to confirm data isolation.

#### 3. Generate an authentication token

* Generate a token including all necessary user attributes.
* Set an appropriate expiration time for the session.
* Ensure the token scope includes permission for the AI assistant capability.

#### 4. Retrieve the embed code

* Locate the embed snippet in your admin panel or use the following standard web component format:

```html
<script type="module" src="https://toucanai.cloud/embed/embed.js"></script>
<link rel="stylesheet" href="https://toucanai.cloud/embed/embed.css" />

<tc-ai-assistant
  auth-token="YOUR_AUTH_TOKEN"
  server-url="https://toucanai.cloud/api"
  data-theme="light"
></tc-ai-assistant>
```

#### 5. Integrate and test

* Paste the embed code into your application's HTML.
* Replace `YOUR_AUTH_TOKEN` with a dynamic variable from your backend.
* Load your application and submit a test prompt (e.g., "How many hires last quarter?") to verify the connection.

<figure><img src="/files/N7je2tTfv7FdrMlQY1zx" alt="Embed AI Chat"><figcaption></figcaption></figure>

***

#### Multi-tenant Example: HR SaaS

In a multi-tenant environment, the integration follows this logic to ensure data privacy:

| Step            | Configuration                                                          |
| --------------- | ---------------------------------------------------------------------- |
| **Attribute**   | `customer_id` defined in the token.                                    |
| **RLS Mapping** | Token `customer_id` maps to database `customer_id` column.             |
| **User Query**  | "How many hires last quarter?".                                        |
| **Execution**   | The query is automatically filtered by `customer_id` before execution. |
| **Result**      | The user only sees data belonging to their specific organization.      |

***

### Conclusion

The AI assistant is now embedded and respects the security boundaries defined by your authentication tokens. Users can perform ad-hoc analysis through natural language while Toucan AI handles the underlying query generation and data visualization.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.toucanai.cloud/embed/embedding-overview/how-to/embed-an-ai-chat.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
