UsefulKey

Example — Hono

Using UsefulKey in a Hono server.

A minimal Hono server that creates and verifies API keys using in-memory adapters. It exposes health, key creation, and verification endpoints.

Source: examples/hono

  • Repo path: examples/hono
  • Run locally:
    • Install: pnpm i (in examples/hono)
    • Start: pnpm dev
    • Server: http://localhost:8788
  • Endpoints:
    • GET /health
    • POST /keys — demo key creation
    • GET /verify — verify via x-api-key
    • POST /verify — verify via JSON { key }
import { Hono } from "hono";
import { usefulkey, MemoryKeyStore, MemoryRateLimitStore, ConsoleAnalytics } from "betterkey";

const uk = usefulkey({
  keyPrefix: "uk",
  adapters: {
    keyStore: new MemoryKeyStore(),
    rateLimitStore: new MemoryRateLimitStore(),
    analytics: new ConsoleAnalytics(),
  },
});

const app = new Hono();

app.post("/verify", async (c) => {
  const { key } = await c.req.json();
  const ip = c.req.header("x-forwarded-for") ?? "0.0.0.0";
  const res = await uk.verifyKey({ key, ip });
  return c.json(res.result ?? { error: res.error });
});

export default app;

Tips:

  • Do not expose POST /keys publicly in production; protect it with admin auth or remove it.
  • Replace in-memory adapters with Postgres/Redis to scale horizontally.
  • For request-level rate limiting, use the rate-limit plugin and include an identifier (e.g., IP).

See examples/hono in the repo for a complete project.