expressMiddleware
Create an Express/Connect-compatible middleware function that validates
incoming requests against the configured authentication methods and returns a
401 Unauthorized response when auth fails. Framework-agnostic equivalent of
the Python SDK’s get_fastapi_dependency.
For Hono apps, use middleware() instead. For standalone
validation without a framework, use validate() directly.
Parameters
optional
When true, unauthenticated requests are allowed through instead of being
rejected. Useful for progressive-auth flows where the handler downstream
decides whether to require credentials.
Returns
An async Express-compatible middleware:
(req, res, next) => Promise<void>.
On failed auth with optional=false, responds with HTTP 401 and body
{ error: 'Unauthorized' }. On success or when optional=true, calls next().