Skip to content
Botscent

SvelteKit

Add both halves of Botscent to a SvelteKit app and check the install.

Add Botscent to a SvelteKit app. The page half runs in the browser from src/hooks.client.ts. The server half runs in your server hooks or routes, where you call inspect.

Before you start

You need:

  • SvelteKit with Svelte 4 or 5.
  • Node.js 22.12 or later.

1. Install the package

Install botscent from npm.

Terminal
npm install botscent

With pnpm, yarn or bun, use their add command.

2. Add the page half

In src/hooks.client.ts, import botscent/auto. If the file exists, add the line to the existing file.

src/hooks.client.ts
import 'botscent/auto'

The page half now starts once, in the browser.

3. Add the server half

Botscent has no server adapter for SvelteKit. In src/hooks.server.ts or in a route, call inspect from botscent/server with the request.

src/routes/api/visit/+server.ts
import { json } from '@sveltejs/kit'
import { inspect } from 'botscent/server'
import type { RequestHandler } from './$types'

export const POST: RequestHandler = async ({ request }) => {
  const verdict = await inspect(request)
  return json(verdict)
}

The route now returns the request's own verdict as JSON.

Without a server adapter, the request's own verdict does not reach the page. See From the server to the page.

Warning: Do not use agent_name or the page verdict to allow access. Anyone can send the headers that produce a name, and the browser computes the page verdict. Use isVerified on the request's own verdict, as The trust model describes.

4. Read the verdict

In any component, import the botscent store from botscent/svelte. Read the page verdict as $botscent.

src/routes/+page.svelte
<script lang="ts">
  import { botscent } from 'botscent/svelte'
</script>

<pre>{JSON.stringify($botscent)}</pre>

The server renders {"type":"human","reasons":[]}, and after hydration the store follows the page verdict.

5. Check the install

Start the dev server. Then run the check against its URL.

Terminal
npx botscent check http://localhost:5173/

The page-script check prints pass, and the command exits with code 0.

The server-half check prints unknown, because no server adapter sends the request's own verdict to the page. If a check fails, see Verify your install.

Next steps