# Hono integration

Add ClipMux uploads, playback and webhooks to a Hono app by passing its raw request to the ClipMux handler on one route.

Source: https://clipmux.com/docs/build/frameworks/hono

`clipmuxHandler` takes a Fetch `Request` and returns a `Response`, and Hono
gives you the raw `Request` as `c.req.raw`. One route answers the uploader,
the player and ClipMux's webhooks.

## Before you start [#before-you-start]

- A Hono app on a runtime with `process.env`: Node 20.19 or later, or Bun.
  On Cloudflare Workers, see
  [SvelteKit, Remix, Bun, Deno and Workers](/docs/build/frameworks/fetch-runtimes).
- The packages, and your app connected with `init`, which writes the
  `CLIPMUX_*` settings to `.env.local`:

<Tabs items={["npm","pnpm"]}>
<Tab value="npm">
```bash
npm i @clipmux/node @clipmux/uploader @clipmux/player
npx @clipmux/node init --server https://video.example.com
```
</Tab>

<Tab value="pnpm">
```bash
pnpm add @clipmux/node @clipmux/uploader @clipmux/player
pnpm dlx @clipmux/node init --server https://video.example.com
```
</Tab>
</Tabs>

## Mount the route [#mount-the-route]

```ts title="src/index.ts"
import { Hono } from 'hono'
import { clipmuxHandler } from '@clipmux/node/handlers'

const clipmux = clipmuxHandler({
  async upload(req, { file }) {
    const user = await requireInstructor(req)
    const lesson = new URL(req.url).searchParams.get('lesson')
    return { passthrough: `lesson_${lesson}`, title: file.name, generate: { subtitles: true } }
  },
  async playback(req, { playbackId }) {
    const user = await getUser(req)
    return (await canWatch(user, playbackId)) ? { viewerId: user.id } : null
  },
  async asset(asset) {
    await db.lesson.update({
      where: { ref: asset.passthrough },
      data: { status: asset.status, playbackId: asset.playback_ids[0]?.id },
    })
  },
})

const app = new Hono()
app.post('/api/clipmux/:action', (c) => clipmux(c.req.raw))

export default app
```

The callbacks are the same as in the [quickstart](/docs/build/quickstart)'s
route file; `requireInstructor`, `getUser`, `canWatch` and `db` stand for
your own code. They get the Fetch `Request`, so read cookies and headers
from `req.headers` and query parameters from `new URL(req.url)`.

Let the handler read the body. Don't parse it in a middleware or validator
on this route first: the webhook's signature covers the raw body.

## Add the elements [#add-the-elements]

Load the two elements in your pages and point them at the route:

```html
<script type="module">
  import '@clipmux/uploader/element'
  import '@clipmux/player/element'
</script>
<clipmux-uploader create-url="/api/clipmux/upload?lesson=42"></clipmux-uploader>
<clipmux-player playback-id="pid_…" session-url="/api/clipmux/playback"></clipmux-player>
```

## Check it works [#check-it-works]

```bash
npx @clipmux/node doctor
npx @clipmux/node listen --forward-to localhost:3000/api/clipmux/webhook
```

`doctor` ends with `✓ Ready.` Upload a file from your page; when it's
encoded, `listen` prints `✓ video.asset.ready ast_… → 200`. A `400` there
means the secret is wrong, or the body was read before the handler.

## Next [#next]

- [Receive webhooks](/docs/build/webhooks)
- [Add the player](/docs/build/play/player)
- [Production checklist](/docs/build/going-live/production-checklist)
