# Install Tidio on SvelteKit

Section: Install Tidio

Description: Add Tidio to SvelteKit, configure a public environment key, identify logged-in visitors, and control widget visibility during navigation.

Verified against Tidio panel 12.331.0

Last updated: 2026-11-18

Source: https://help.tidio.com/articles/install-sveltekit/

Add Tidio to your SvelteKit app with one script in the root HTML template. You do not need an npm package for Tidio. These examples were checked with SvelteKit 3.3.1 and Svelte 5.58.0.

**In this article, you'll learn:**

- [How to find your Public key](https://help.tidio.com/articles/install-sveltekit/#public-key)
- [How to add the widget to SvelteKit](https://help.tidio.com/articles/install-sveltekit/#install)
- [How to check the installation](https://help.tidio.com/articles/install-sveltekit/#check)
- [How to identify logged-in visitors](https://help.tidio.com/articles/install-sveltekit/#identify-visitors)
- [How to hide the widget on some pages](https://help.tidio.com/articles/install-sveltekit/#hide-on-pages)
- [How to troubleshoot the installation](https://help.tidio.com/articles/install-sveltekit/#troubleshooting)

## Find your Public key

Log in to your Tidio panel and go to **Settings > Developer > Project data**. Click the **Public key** field to select its value, then copy it.

![The Project data page with the Public key field, masked private key and Visit widget SDK documentation button visible](https://help.tidio.com/article-media/install-sveltekit/project-data-public-key.png)

The Public key is part of the script address that every visitor's browser loads, so you can keep it in front-end code. The private key on the same page is different: never put it in your website.

You'll find the same key inside the installation code in **Settings > Live Chat > Installation**, under **Manual install**:

```
<script src="//code.tidio.co/YOUR_PUBLIC_KEY.js" async></script>
```

![Settings > Live Chat > Installation with Manual install selected, the code snippet visible, and the Copy to clipboard button.](https://help.tidio.com/article-media/install-sveltekit/installation-manual-install.png)

## Add the widget to SvelteKit

In ` src/app.html `, add the script after the wrapper containing ` %sveltekit.body% `, just before ` </body> `. Replace ` YOUR_PUBLIC_KEY ` with your Public key. Keep any existing head elements or attributes your app needs. A minimal template looks like this:

```
<!doctype html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="text-scale" content="scale" />
        %sveltekit.head%
    </head>
    <body data-sveltekit-preload-data="hover">
        <div style="display: contents">%sveltekit.body%</div>
        <script src="https://code.tidio.co/YOUR_PUBLIC_KEY.js" async></script>
    </body>
</html>
```

The root template keeps one script across client-side navigation. A plain Svelte app on Vite uses ` index.html ` the same way.

### Use a public environment variable instead

Use this alternative if you want to configure the key separately. Remove the Tidio script from ` src/app.html ` first. Set this value in your project's ` .env ` file:

```
PUBLIC_TIDIO_KEY=YOUR_PUBLIC_KEY
```

In SvelteKit 3, declare the public variable in ` src/env.ts `. If this file already exists, add the variable to its existing definition:

```
import { defineEnvVars } from '@sveltejs/kit/env';
export const variables = defineEnvVars({
    PUBLIC_TIDIO_KEY: { public: true }
});
```

Use this loader in ` src/routes/+layout.svelte `. Keep your existing layout markup around ` {@render children()} `. ` onMount ` runs only in the browser:

```
<script lang="ts">
    import { onMount } from 'svelte';
    import { PUBLIC_TIDIO_KEY } from '$app/env/public';
    import type { LayoutProps } from './$types';
    let { children }: LayoutProps = $props();
    onMount(() => {
        const script = document.createElement('script');
        script.src = `https://code.tidio.co/${PUBLIC_TIDIO_KEY}.js`;
        script.async = true;
        document.body.appendChild(script);
    });
</script>
{@render children()}
```

SvelteKit 3 deprecates the older ` $env/static/public ` module in favor of [explicit environment variables](https://svelte.dev/docs/kit/environment-variables). Rebuild and redeploy after changing your environment configuration. With ` adapter-node `, the built server also checks ` PUBLIC_TIDIO_KEY ` when it starts, so set the variable in your production environment too.

If an AI coding assistant edits this project for you, give it the prompt from [Install Tidio with an AI coding agent](https://help.tidio.com/articles/install-ai-coding-agents/).

## Check the installation

Open your site in a regular browser window. The chat widget appears in the corner of the page and stays there when you move between pages. Headless and automated browsers may not show the widget, so do not rely on them for this check.

Loading a page with the widget is what activates it. After that, **Settings > Live Chat > Installation** in your Tidio panel says **Chat Widget is installed properly**.

[Video](https://help.tidio.com/article-media/install-sveltekit/walkthrough.mp4)

Silent walkthrough. A demo SvelteKit site with the Tidio chat widget in the corner. The widget stays when the visitor returns to the home page and is hidden on the Checkout page.

## Identify logged-in visitors

Use the public environment setup above, but replace its layout loader with this version. Do not also load Tidio in ` src/app.html `. This example expects your existing authentication system to return ` user ` through layout data, with a stable ` distinct_id `, ` email `, and ` name `, or ` null ` when logged out. Refresh that data after login or logout. Adapt the fields to your user model, and do not send session tokens.

The loader sets ` document.tidioIdentify ` before inserting the script, so Tidio can merge conversations for that visitor. Later changes call ` setVisitorData `, which updates visitor data but does not merge conversations. Logout removes the identify data before calling ` resetVisitor `. The final effect also handles the route visibility rule described below.

```
<script lang="ts">
    import { onMount } from 'svelte';
    import { page } from '$app/state';
    import { PUBLIC_TIDIO_KEY } from '$app/env/public';
    import type { LayoutProps } from './$types';
    type Visitor = { distinct_id: string; email: string; name: string };
    type ChatApi = {
        on(event: 'ready', callback: () => void): void;
        setVisitorData(visitor: Visitor): void;
        resetVisitor(): void;
        display(visible: boolean): void;
    };
    let { children }: LayoutProps = $props();
    let ready = $state(false);
    let api: ChatApi;
    let previous: Visitor | null | undefined;
    onMount(() => {
        const doc = document as Document & { tidioIdentify?: Visitor };
        const win = window as Window & { tidioChatApi?: ChatApi };
        previous = page.data.user as Visitor | null;
        if (previous) doc.tidioIdentify = previous;
        const onReady = () => { api = win.tidioChatApi!; ready = true; };
        if (win.tidioChatApi) win.tidioChatApi.on('ready', onReady);
        else document.addEventListener('tidioChat-ready', onReady);
        const script = document.createElement('script');
        script.src = `https://code.tidio.co/${PUBLIC_TIDIO_KEY}.js`;
        script.async = true;
        document.body.appendChild(script);
        return () => document.removeEventListener('tidioChat-ready', onReady);
    });
    $effect(() => {
        const visitor = page.data.user as Visitor | null;
        if (!ready || visitor === previous) return;
        if (visitor) api.setVisitorData(visitor);
        else {
            delete (document as Document & { tidioIdentify?: Visitor }).tidioIdentify;
            api.resetVisitor();
        }
        previous = visitor;
    });
    $effect(() => {
        const path = page.url.pathname;
        if (ready) api.display(path !== '/checkout' && !path.startsWith('/checkout/'));
    });
</script>
{@render children()}
```

## Hide the widget on some pages

The last ` $effect ` in the layout above reads the reactive ` page.url.pathname ` from ` $app/state `. After the ready event, it calls ` display(false) ` on ` /checkout ` and its child paths, and ` display(true) ` elsewhere, including after client-side navigation. Replace those paths with yours. You can use this layout with ` user ` set to ` null ` if you only need route visibility.

Hiding the widget does not stop Flows from triggering. To load the widget only on a list of addresses instead, see [Show the widget only on specific pages](https://help.tidio.com/articles/show-widget-only-on-specific-pages/).

## Troubleshooting

- Check that the script address contains your Public key, not ` YOUR_PUBLIC_KEY `. For the environment option, check the variable definition and value, then rebuild.
- If you see two widgets or duplicate script requests, remove the second installation. Use either ` src/app.html ` or the layout loader, not both.
- If server rendering reports that ` window ` or ` document ` is undefined, keep browser access inside ` onMount ` or ` $effect `.
- If your Content Security Policy blocks Tidio, allow ` code.tidio.co ` in ` script-src ` and check the [full list of required hosts](https://developers.tidio.com/docs/widget-security-policy).
- If an automated test loads the script but shows no widget, check in a regular browser window.

## Further reading

- [Install Tidio on a website](https://help.tidio.com/articles/install-website/)
- [Widget SDK](https://help.tidio.com/articles/widget-sdk/)
- [Show the widget only on specific pages](https://help.tidio.com/articles/show-widget-only-on-specific-pages/)
- [SvelteKit project structure](https://svelte.dev/docs/kit/project-structure)
