docs / hono / cloudflare workers

Hono / Cloudflare Workers

Hono is a Fetch-API-first web framework that runs on Cloudflare Workers, Bun, Deno, and Node. The Fetch-style ocGateFetch plugs in cleanly.

Install

npm install hono @orangecheck/gate

Minimum working example

import { Hono } from 'hono';
import { ocGateFetch } from '@orangecheck/gate';

const app = new Hono();

app.use('/protected/*', async (c, next) => {
  const decision = await ocGateFetch(c.req.raw, {
    minSats: 100_000,
    minDays: 30,
    address: { from: 'header' },
  });
  if (!decision.ok) {
    return c.json({ error: decision.reason }, 403);
  }
  c.set('orangecheck', decision.check);
  await next();
});

app.post('/protected/post', (c) => c.json({ ok: true }));

export default app;

Cloudflare Workers deployment

The same code runs unchanged on Workers. wrangler.toml:

name = "my-gated-api"
compatibility_date = "2024-04-01"
main = "src/index.ts"
// src/index.ts
import { Hono } from 'hono';
import { ocGateFetch } from '@orangecheck/gate';

const app = new Hono();

app.post('/post', async (c) => {
  const decision = await ocGateFetch(c.req.raw, {
    minSats: 100_000,
    address: { from: 'header' },
  });
  if (!decision.ok) return c.json({ error: decision.reason }, 403);
  return c.json({ ok: true });
});

export default app;

wrangler deploy and you have a globally-distributed OrangeCheck-gated API.

Bun / Deno

Same pattern. Bun:

import { Hono } from 'hono';
import { ocGateFetch } from '@orangecheck/gate';

const app = new Hono();
app.post('/post', async (c) => {
  const decision = await ocGateFetch(c.req.raw, { minSats: 100_000, address: { from: 'header' } });
  if (!decision.ok) return c.json({ error: decision.reason }, 403);
  return c.json({ ok: true });
});

Bun.serve({ fetch: app.fetch });

Accessing the CheckResult downstream

type Variables = {
  orangecheck: import('@orangecheck/sdk').CheckResult;
};

const app = new Hono<{ Variables: Variables }>();

app.post('/protected/post', async (c) => {
  const oc = c.get('orangecheck');
  return c.json({ sats: oc.sats });
});

Further