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
- How to add the widget to SvelteKit
- How to check the installation
- How to identify logged-in visitors
- How to hide the widget on some pages
- How to troubleshoot the installation
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 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>
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. 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.
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.
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.
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.htmlor the layout loader, not both. - If server rendering reports that
windowordocumentis undefined, keep browser access insideonMountor$effect. - If your Content Security Policy blocks Tidio, allow
code.tidio.coinscript-srcand check the full list of required hosts. - If an automated test loads the script but shows no widget, check in a regular browser window.
