Authaz logoAuthaz
DocumentationAPI Reference
  • Get Started

    • Authaz
    • Core Concepts
    • Set up your app
    • Quickstart — cURL
  • Authentication

    • Authentication Settings
    • Signup
    • Invitations
    • Password Authentication
    • Multi-Factor Auth
    • Magic Link
    • OAuth / Social Login
    • Passkey (WebAuthn)
    • SAML SSO
    • Machine-to-Machine (M2M)
    • API Keys
  • Authorization

    • Authorization
    • Resources
    • Policies
    • Roles
    • Access Explorer
  • Tenancy

    • Multi-tenancy
    • Tenancy Customization
  • Brand & Host

    • Branding
    • Custom Domains
    • Communications & Email Templates
  • Operate

    • Users
    • Analytics
    • Audit Logs
    • Application Settings
  • SDK Quickstarts

    • Quickstart — Next.js
    • Quickstart — React SPA
    • Quickstart — Hono
    • Quickstart — .NET (Authaz.Sdk)
  • Recipes

    • Recipes & Cookbook
    • Next.js — first integration
    • Next.js — B2B SaaS (multi-tenant)
    • Hono — first integration
    • Hono — B2B SaaS (multi-tenant)
    • React SPA — first integration
    • React SPA — B2B SaaS (multi-tenant)
    • .NET — first integration
    • .NET — B2B SaaS (multi-tenant)
  • Reference

    • Tokens
    • API Reference
    • Errors & Troubleshooting
  • Documentation

    • How Authaz is Built
  1. Authaz
  2. Docs
  3. SDK Quickstarts
  4. Quickstart — Hono

SDK Quickstarts

Quickstart — Hono

3 min read·Updated Jun 19, 2026

cURL · Next.js · React · Hono · .NET

@authaz/hono mounts the OAuth flow as a sub-app on your Hono server. It runs anywhere Hono runs — Node, Bun, Cloudflare Workers, Deno, AWS Lambda. About 8 minutes.

Prerequisites#

  • Node 18+ (or Bun, or your runtime of choice).

  • A Hono app. If you're starting from scratch:

    npm create hono@latest my-app
    cd my-app
    npm install
  • An Authaz application with http://localhost:3000/auth/callback registered as a redirect URI. New to Authaz? Set up your app in 60 seconds — you'll come back with , , and ready to paste in.

Previous
Quickstart — React SPA
Next
Quickstart — .NET (Authaz.Sdk)
AUTHAZ_CLIENT_ID
AUTHAZ_CLIENT_SECRET
AUTHAZ_ORGANIZATION_ID

Step 1 — Install#

npm install @authaz/hono
# pnpm: pnpm add @authaz/hono
# yarn: yarn add @authaz/hono
# bun:  bun add @authaz/hono

Step 2 — Mount the auth handler#

// src/auth.ts
import { createAuthazHandler } from "@authaz/hono";
 
export const authHandler = createAuthazHandler({
  clientId: process.env.AUTHAZ_CLIENT_ID!,
  clientSecret: process.env.AUTHAZ_CLIENT_SECRET!,
  organizationId: process.env.AUTHAZ_ORGANIZATION_ID!,
  tenantId: process.env.AUTHAZ_TENANT_ID,
  afterLoginUrl: "/dashboard",
  afterLogoutUrl: "/",
});

Then mount it on your main app:

// src/index.ts
import { Hono } from "hono";
import { serve } from "@hono/node-server";
import { authHandler } from "./auth";
 
const app = new Hono();
 
app.route("/api/auth", authHandler);
 
app.get("/", (c) => c.text("Hello!"));
 
serve({ fetch: app.fetch, port: 3000 });

The handler exposes:

RouteMethodWhat it does
/api/auth/loginGETRedirects to Authaz Sign-In.
/api/auth/callbackPOSTExchanges the OAuth code, sets session cookies.
/api/auth/logoutPOSTClears session, redirects to Authaz logout.
/api/auth/meGETReturns the current user, or 401.
/api/auth/refreshPOSTRefreshes the access token.

Step 3 — Add the OAuth callback page#

Authaz redirects users back to /auth/callback as a GET, but the handler expects a POST (so the auth code never ends up logged in browser history). Serve a tiny HTML page that re-POSTs:

app.get("/auth/callback", (c) => c.html(`
  <!doctype html>
  <html><body>
  <p>Completing login…</p>
  <script>
    const params = new URLSearchParams(window.location.search);
    const form = document.createElement("form");
    form.method = "POST";
    form.action = "/api/auth/callback";
    ["code", "state", "error", "error_description"].forEach((p) => {
      const v = params.get(p);
      if (v) {
        const input = document.createElement("input");
        input.type = "hidden";
        input.name = p;
        input.value = v;
        form.appendChild(input);
      }
    });
    document.body.appendChild(form);
    form.submit();
  </script>
  </body></html>
`));

If your frontend is a separate SPA, put this page in the SPA instead — the SPA's router serves it.

Step 4 — Protect routes#

@authaz/hono ships two middleware factories.

authMiddleware() — fast, cookie-only check. Returns 401 if no session cookie:

import { authMiddleware } from "@authaz/hono";
 
app.use("/api/protected/*", authMiddleware());
 
app.get("/api/protected/data", (c) => {
  return c.json({ secret: "only signed-in users see this" });
});

createAuthMiddleware({ authazDomain, apiKey }) — fetches the user via OIDC userinfo and attaches them to the context. Use when you actually need the user object:

import { createAuthMiddleware } from "@authaz/hono";
 
const { requireUser, optionalUser } = createAuthMiddleware({
  authazDomain: "https://auth.authaz.io",
  apiKey: process.env.AUTHAZ_API_KEY!,
});
 
app.use("/api/profile", requireUser);
 
app.get("/api/profile", (c) => {
  const user = c.get("user");
  return c.json({ user });
});

optionalUser does the same fetch but does not 401 if the user is unauthenticated — useful for personalized public pages.

Step 5 — Helpers in regular handlers#

Need the access token, refresh token, or auth state outside middleware? The package re-exports cookie helpers:

import { getAccessToken, isAuthenticated } from "@authaz/hono";
 
app.get("/debug/auth", (c) => {
  return c.json({
    authed: isAuthenticated(c),
    tokenLen: getAccessToken(c)?.length ?? 0,
  });
});

Step 6 — Run it#

.env:

AUTHAZ_CLIENT_ID=app_01h...
AUTHAZ_CLIENT_SECRET=secret_...
AUTHAZ_ORGANIZATION_ID=0199...

Then:

npm run dev

Visit http://localhost:3000/api/auth/login — you'll be redirected to Authaz Sign-In. Sign up, get redirected back, then call:

curl http://localhost:3000/api/auth/me --cookie-jar /tmp/c --cookie /tmp/c
# → { "id": "user_01h...", "email": "you@example.com", "name": "...", "organizations": [...] }

If /api/auth/me returns your user, auth is working end-to-end.

If something's wrong#

SymptomLikely causeFix
invalid_request after sign-inRedirect URI mismatch.Add exactly http://localhost:3000/auth/callback in Settings → Redirect URIs.
Land on /auth/callback and see a 404The Step 3 re-POST page isn't mounted, so the GET callback has no handler.Add the app.get("/auth/callback", ...) handler from Step 3.
401 invalid_client on token exchangeAUTHAZ_CLIENT_SECRET wrong, or you swapped Application ID and Client Secret.Re-copy from Settings → API Keys.
/api/auth/me returns 401 after a clean loginSession cookie not being set — usually a same-site or origin mismatch.In dev, hit the Hono server directly or proxy from your SPA.
Workers / Bun: process.env.X is undefinedThose runtimes don't populate process.env from .env.Pass env via wrangler.toml (Workers) or Bun.env / bun --env-file=.env.

For everything else, the errors catalog covers every code Authaz returns.

Pairing with a React frontend#

Run the React quickstart on the same origin (or proxy /api/auth/* from your dev server) and the SDK on both sides will share the session cookie automatically. Vite proxy snippet:

// vite.config.ts
export default {
  server: {
    proxy: { "/api": "http://localhost:3000" },
  },
};

Going further#

  • Cloudflare Workers / Bun — the SDK runs unmodified. Pass env vars via your platform's secrets, not process.env.
  • Custom domains — pass authazIdentityDomain: "https://auth.yourapp.com" once your custom domain is verified.
  • Debug logging — set debug: true in createAuthazHandler to log every step (no secrets).

Complete worked example#

  • Hono — first integration (single-tenant) — the full app, end to end.
  • Hono — B2B SaaS (multi-tenant) — same, with tenant pickers and tenant-scoped tokens.

Next steps#

  • React quickstart — the frontend half.
  • Multi-tenancy — tenant pickers and tenant-scoped tokens.
  • API Reference — calling Management API endpoints from your Hono backend.