Skip to main content
You already have users. You just added agents. This is the ten-minute recipe.
KavachOS does not replace your human auth. It runs alongside it. This guide assumes you already have a way to get a stable userId for every request.

What you need before you start

A user ID on every request

Your existing auth must expose a stable string ID, anything from Clerk’s userId, Auth.js’s session.user.id, or a row id from your own users table.

A database

Postgres, MySQL, SQLite, or Cloudflare D1. Kavach creates its own tables, prefixed kavach_, alongside yours.

Steps

1

Install

Substitute the adapter for your framework: @kavachos/hono, @kavachos/express, @kavachos/fastify, and so on. See Framework adapters.
2

Create the Kavach instance

lib/kavach.ts
The lazy pattern lets Next.js build without opening a DB connection.
3

Wire the route handler

app/api/kavach/[...kavach]/route.ts
Pick a path that does not collide with your existing auth. /api/kavach/* lives next to /api/auth/* (Clerk / Auth.js) without stepping on it.
4

Create an agent when a user signs up or activates AI features

Hook into your existing post-sign-in flow. For Clerk, that is a webhook or a server action. For Auth.js, the signIn event. For better-auth, the onSignIn hook.
wherever you create user-scoped resources
The token is shown once. Store it in your secrets store or hand it directly to the agent process. If you lose it, rotate with kavach.agent.rotate(agentId) to issue a new one.
5

Authorize in your handlers

Anywhere your agent code runs, check authorization before the call.
app/api/agent-action/route.ts

Fitting in with Clerk, Auth.js, and better-auth

Common first-day questions

No by default. Kavach runs its own migrations on first boot. Set skipMigrations: true in createKavach if you want to run them yourself via kavach-cli migrate.
Kavach stores ownerId as a string and never interprets it. Migrating from numeric IDs to UUIDs later means updating kavach_agents.owner_id in a single SQL update.
Yes, via event streaming and webhooks. Wire them to your analytics or to Stripe / PostHog / Slack.

Next steps

Permissions

Define what agents can and cannot do.

MCP OAuth 2.1

Run your own authorization server for MCP tools.

Delegation

Let agents spawn sub-agents with scoped permissions.

Audit trail

Query and export every authorization decision.
Last modified on April 20, 2026