# SvelteKit

> Mount the pipedog WebRTC offer endpoint in a SvelteKit +server.ts route.

Source: https://polvallverdu.github.io/pipedog/docs/frameworks/sveltekit/



A `+server.ts` route handles `POST /api/offer`. SvelteKit hands the route a web `Request`, so the
route passes it to `offer` unchanged.

## Install [#install]

```bash
bun add @pipedog/core @pipedog/asyncio @pipedog/transport-smallwebrtc @pipedog/transport-websocket @pipedog/serializer-protobuf @pipedog/openai @pipedog/audio-silero @pipedog/audio-smart-turn @sveltejs/kit
```

`@pipedog/audio-smart-turn` isn't imported anywhere: the default turn strategies load it. To run
the snippet from a checkout instead, see [Installation](https://polvallverdu.github.io/pipedog/docs/installation/#from-source).

## Files [#files]

The endpoints come from the shared module on the [overview](https://polvallverdu.github.io/pipedog/docs/frameworks/#the-two-endpoints).
`$lib/voice` re-exports it:

```ts title="src/lib/voice.ts"
export { offer, runSocketAgent, socket } from "../../../shared/voice.ts";

```

```ts title="src/routes/api/offer/+server.ts"
import type { RequestHandler } from "@sveltejs/kit";
import { offer } from "$lib/voice";

// Use adapter-node (or any Node, Bun or Deno server): the media runs over UDP,
// which serverless adapters don't carry.
export const POST: RequestHandler = ({ request }) => offer(request);

```

In your app, put `shared/voice.ts` and `shared/agent.ts` in `src/lib/` and drop the re-export.

## Run it [#run-it]

```bash
OPENAI_API_KEY=sk-... bunx vite dev
```

The browser then POSTs its SDP offer to `/api/offer`.

## Deploy [#deploy]

Use an adapter that runs a Node, Bun or Deno server, such as `@sveltejs/adapter-node`
(`bun add -d @sveltejs/adapter-node`, then set it in `svelte.config.js`). The serverless adapters (Vercel, Netlify, Cloudflare) don't carry the UDP media.

## WebSocket [#websocket]

This snippet mounts only the offer. To serve `socket` too, run it on a server that upgrades
WebSockets next to the SvelteKit app: [Bun](https://polvallverdu.github.io/pipedog/docs/frameworks/bun.md), [Deno](https://polvallverdu.github.io/pipedog/docs/frameworks/deno.md),
or `serveFetch(socket, { port })` from `@pipedog/transport-websocket/serve` on Node, Bun or Deno.

## Authentication [#authentication]

The route has no auth. Wrap `offer` with a session check before it runs, as in
[Authentication](https://polvallverdu.github.io/pipedog/docs/frameworks/#authentication).
