- an opt-in bearer-token guard that validates JSON Web Tokens (JWT) before any built-in route executes;
- the ability to chain custom Hono middleware or register bespoke routes with their own protections.
Enabling the bearer guard
Provide aserver.auth object when instantiating ServerKit to require a bearer token on every request. The guard is active as soon as you pass a non-empty secret; the enabled flag defaults to true so you only need to flip it when turning the guard off temporarily.
- requests missing the
Authorizationheader receive401 Missing Authorization header; - headers that do not use the
Bearerscheme (for exampleBasicor an empty value) receive401 Authorization header must use the Bearer scheme; - malformed or expired tokens raise
401 Invalid authorization token.
enabled: false to keep the configuration around while bypassing the guard, for example in local development.
Issuing compatible tokens
The guard expects a JWT signed with the shared secret using an HMAC algorithm such asHS256. A quick way to mint tokens is with the jose package that AI Kit already depends on.
Authorization: Bearer <token> header when calling any server endpoint (agents, workflows, streaming SSE, custom routes, Swagger, etc.).
Reading the decoded payload
Once the middleware verifies a token, it stores the raw token and decoded payload on the Hono context under theauth key. Any downstream middleware or handler can read it to tailor behaviour to the current principal.
Combining with custom strategies
Need API keys, IP allow-lists, or multi-tenant scoping? Chain additional middleware via theserver.middleware array or directly inside registerApiRoute.
registerApiRoute automatically inherit both the bearer guard and the middleware chain, ensuring consistent protection across your API.