Skip to main content
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.
terminal
The CLI asks for a directory, a template, a package manager, and a database driver. Only the Next.js SaaS template is shipping today; the Hono MCP and Expo templates print a coming-soon note and exit. Placeholders in the template (__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

terminal
2

Create an instance

Pass a database config to createKavach. 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.
The token is shown exactly once, at creation time. Store it immediately in your secrets manager or pass it directly to the agent. It cannot be recovered after this point, only rotated.
There are three agent types:
4

Authorize an action

Call kavach.authorize before any sensitive operation. It returns { allowed, reason?, auditId }.
If you only have the raw bearer token (from an incoming HTTP request, for example), use 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 a maxDepth 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.
Bind a D1 database in your wrangler.toml:
Run 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:
  1. Verify the email using the token from the sign-up response
  2. Set requireVerification: false in the emailPassword() config

”FOREIGN KEY constraint failed” when creating agents

You need a user in the kavach_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 in localStorage. 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.
Last modified on April 29, 2026