# Deno

> Serve the pipedog WebRTC offer and WebSocket endpoints with Deno.serve.

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



`Deno.serve` routes both endpoints. `socket` is a fetch handler, and Deno upgrades a WebSocket from
fetch, so the upgrade uses `Deno.upgradeWebSocket` under the hood.

## Install [#install]

```bash
deno add npm:@pipedog/core npm:@pipedog/asyncio npm:@pipedog/transport-smallwebrtc npm:@pipedog/transport-websocket npm:@pipedog/serializer-protobuf npm:@pipedog/openai npm:@pipedog/audio-silero npm:@pipedog/audio-smart-turn
```

`@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).
`voice.ts` re-exports it:

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

```

```ts title="main.ts"
import { offer, socket } from "./voice.ts";

// `deno run -A main.ts` starts it on port 3000, or on $PORT.
Deno.serve({ port: Number(Deno.env.get("PORT") ?? 3000) }, (request) => {
  const { pathname } = new URL(request.url);
  if (pathname === "/api/offer") return offer(request);
  if (pathname === "/ws") return socket(request);
  return new Response("not found", { status: 404 });
});

```

## Run it [#run-it]

```bash
OPENAI_API_KEY=sk-... deno run -A main.ts
```

It listens on port 3000, or on `$PORT`. The offer is at `POST /api/offer` and the WebSocket at `/ws`.

## Deploy [#deploy]

Run it as a long-lived Deno process on a host that allows UDP traffic, for the offer's media. A host
that runs Deno without UDP can still serve `socket`.

## Authentication [#authentication]

Neither route has auth. Check the session in the `Deno.serve` handler before either endpoint runs,
as in [Authentication](https://polvallverdu.github.io/pipedog/docs/frameworks/#authentication).
