Skip to Content
Frameworks

Hono integration

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

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

  • 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.
  • The packages, and your app connected with init, which writes the CLIPMUX_* settings to .env.local:
npm i @clipmux/node @clipmux/uploader @clipmux/player
npx @clipmux/node init --server https://video.example.com

Mount the route

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'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

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

<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

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

On this page