Skip to main content

Overview

Add your Invent AI assistant to any SvelteKit application. Perfect for Svelte applications with server-side rendering and static generation.

Installation

1

Create Assistant Component

2

Configure Environment

Add to .env:

User Authentication

Security Requirement: When using any user-* attributes (user-id, user-name, user-avatar), you must also provide user-hash. Both user-id and user-hash must be provided together, or neither should be provided. The user-hash must be generated on your backend using HMAC-SHA256 with your assistant’s secret key. Never expose the secret key to the client.

Server-Side Hash Generation

Use SvelteKit server endpoints:

Using Server Load Functions

Then use in your layout:

Integration with Lucia Auth

TypeScript Support

Add type declarations:

Using Stores

Create a store for user authentication:
Usage:

Tips for SvelteKit

Server Load Functions

Use server load for secure hash generation

Type Safety

Full TypeScript support with proper types

SSR Compatible

Works with SSR and SPA modes

Reactive

Leverage Svelte’s reactivity system

Troubleshooting

Solutions:
  • Check browser guard: if (browser)
  • Ensure script loads in onMount
  • Verify URL is correct
  • Check browser console for errors
Solutions:
  • Use browser check before accessing window
  • Load script client-side only
  • Ensure user data is fetched client-side if needed
Solutions:
  • Restart dev server after adding env variables
  • Use PUBLIC_ prefix for public variables
  • Keep secret key without prefix (private)
  • Check import from correct $env module

Best Practices

  • Use server load functions or endpoints for hash generation
  • Keep secret key in .env (never commit)
  • Use PUBLIC_ prefix only for non-sensitive variables
  • Always check browser before accessing window
  • Leverage SvelteKit’s built-in security features