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.
npm install botscentWith 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.
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.
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_nameor the page verdict to allow access. Anyone can send the headers that produce a name, and the browser computes the page verdict. UseisVerifiedon 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.
<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.
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
- The verdict: Read what
type,agent_nameandreasonsmean. - From the page to the server: Send the page verdict to a route with
reportHeaders. - Server API (TypeScript): Read every option of
inspect. - examples/sveltekit: Run a SvelteKit app with both halves.