Two paths. Pick whichever fits your day.
Start from a template
One command, a running Next.js SaaS, KavachOS wired up.
Add to an existing app
Install, create, authorize, audit. Six steps.
Start from a template
The fastest path is the scaffolder. One command, three prompts, a running Next.js SaaS with KavachOS wired up.- pnpm
- npm
- yarn
- bun
terminal
__APP_NAME__, __DB_DRIVER__, __DB_URL__) are replaced on copy.
Next steps are printed at the end: cd, install, db:push, dev.
Prefer to wire up an existing app? Keep reading.
Your first agent
1
Install
- pnpm
- npm
- yarn
terminal
2
Create an instance
Pass a database config tocreateKavach. Use SQLite for local development and Postgres in production.For in-memory storage (useful in tests), pass
url: ':memory:' instead.Set
requireVerification: false during development so you can sign in without verifying email. Remove this in production.3
Create an agent
An agent always has an owner: the user ID from your existing auth system. KavachOS does not manage human authentication.4
Authorize an action
Callkavach.authorize before any sensitive operation. It returns { allowed, reason?, auditId }.authorizeByToken instead:5
Check the audit trail
Every authorization decision is logged. Query by agent, filter by result, or export for compliance.Delegation
An orchestrator agent can delegate a subset of its permissions to a sub-agent. The delegation has its own expiry and amaxDepth to prevent unbounded chains.
An agent cannot delegate permissions it does not hold itself. Attempts to escalate are rejected at the point of delegation, not at authorization time.
Full working example
Cloudflare Workers
KavachOS runs on Workers with no changes. Pass a D1 binding as the database and use the Hono adapter.wrangler.toml:
npx wrangler d1 execute kavach --file=./kavach-schema.sql to apply the schema, or set skipMigrations: false to let KavachOS run migrations on first boot.
createKavach is async when using D1. Workers and Deno both support top-level await, so you can also initialize outside the handler if you use a module worker.Troubleshooting
”Invalid email or password” after sign-up
Sign-in requires email verification by default. Either:- Verify the email using the token from the sign-up response
- Set
requireVerification: falsein theemailPassword()config
”FOREIGN KEY constraint failed” when creating agents
You need a user in thekavach_users table before creating agents. Sign up via the email auth plugin, or seed a user manually:
Session not persisting after page reload
The React hooks store sessions inlocalStorage. Make sure your app is wrapped in <KavachProvider>. If using SSR (Next.js), wrap the provider in a "use client" component.
Next steps
Permission engine
Wildcards, rate limits, time windows, IP allowlists, approval gates.
Delegation chains
Sub-agent delegation with depth limits and cascading revocation.
MCP OAuth 2.1
Set up the authorization server for MCP tool servers.
Framework adapters
Drop-in middleware for ten frameworks.
Coming from another library
Migration guides from better-auth and Clerk.
Configuration
All
createKavach() options and environment patterns.