# Embed an AI Chat (/core/embed/embedding-overview/how-to/embed-an-ai-chat)





<CalloutContainer type="info">
  <CalloutDescription>
    **Target Audience**: Developers
  </CalloutDescription>
</CalloutContainer>

### Goal [#goal]

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

***

### Prerequisites [#prerequisites]

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

***

### Steps [#steps]

#### 1. Prepare the data layer [#1-prepare-the-data-layer]

* Execute an AI analysis on your datasets, then [review and confirm proposals](/core/build/analyze-your-database-with-ai/how-to/review-ai-analysis-proposals) so semantic metadata is applied.
* 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 [#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 [#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 [#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 [#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.

<img alt="Embed AI Chat" src="__img0" />

***

#### Multi-tenant Example: HR SaaS [#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 [#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.
