BETTER-CONVEX

Introduction

A complete framework for building type-safe, real-time applications with Convex.

Introduction

kitcn is a complete framework for Convex. It brings tRPC-style APIs, TanStack Query, a Drizzle-style ORM, and Better Auth into one cohesive developer experience.

This is not just a query wrapper. It's how you build production Convex applications.

Philosophy

kitcn integrates Convex for realtime data, TanStack Query for caching, and Better Auth for authentication behind a consistent API:

  • Type safety end-to-end - From schema to procedures to client, with full inference
  • Real-time by default - WebSocket subscriptions flow into TanStack Query cache
  • TanStack Query native - Use useQuery and useMutation directly with cRPC options
  • Incremental adoption - Add features as you need them, migrate function by function

What's Included

ExportDescriptionDepends on
kitcn (CLI)Codegen, kitcn dev
kitcn/servercRPC builder, middleware, server caller
kitcn/ormDrizzle-style ORM schema and query helpers
kitcn/reactReact client, providers, auth hooksserver
kitcn/rscRSC prefetching, hydrationserver, react, next
kitcn/authBetter Auth adapter
kitcn/auth/clientBetter Auth client pluginsreact
kitcn/auth/configConvex auth config providerauth
kitcn/auth/httpAuth HTTP helpersauth
kitcn/auth/nextjsBetter Auth Next.js adapterserver, next
kitcn/ratelimitRate limiting pluginorm

server, orm, and auth are standalone foundations — everything else builds on one or more. You can use server or auth without orm, orm and plugins without server.

Quick Look

// Server: Define a procedure
export const list = authQuery
  .input(z.object({ limit: z.number().optional() }))
  .query(async ({ ctx, input }) => {
    return ctx.orm.query.posts.findMany({ limit: input.limit ?? 10 });
  });

// Client: Use it with TanStack Query
const { data: posts } = useQuery(crpc.posts.list.queryOptions({ limit: 10 }));

Requirements

  • Convex 1.31+
  • TanStack Query 5+
  • React 18+

Next Steps


For AI Agents

If you're an AI agent integrating kitcn, ask the user to select their stack before starting. Use your AskUserQuestion tool (or equivalent) to let them pick:

Required Choices

FeatureOptionsDefault
BootstrapCLI (kitcn init / add), Docs by sectionCLI
React FrameworkNext.js App Router, TanStack Start, OtherNext.js App Router
DatabaseORM (ctx.orm)ORM

Optional Features

FeatureOptionsWhen to Include
AuthBetter Auth, Custom, NoneMost apps need auth
SSR/RSCYes, NoNext.js App Router apps
TriggersYes, NoAuto side effects on data changes
AggregatesYes, NoCounts, sums, leaderboards
Rate LimitingYes, NoAPI protection
SchedulingYes, NoDelayed/background work
HTTP routerYes, NoREST/webhook endpoints
RLSYes, NoRuntime row-level access control
Auth pluginsadmin, organizations, polar, noneOnly when required by product

After Selection

CLI bootstrap:

  1. Run kitcn init -t next --yes for the shortest fresh local Convex path.
  2. Add only the features you chose with kitcn add <plugin>:
    • kitcn add auth
    • kitcn add ratelimit
    • kitcn add resend
  3. On backend convex, init and dev run convex init before kitcn codegen.
  4. Use kitcn verify for a one-shot local runtime proof in the current app. Use kitcn dev --bootstrap when you specifically want one-shot local setup. During local Convex dev, convex/.env edits auto-sync. kitcn add auth --yes also finishes local auth bootstrap in one pass. Use kitcn env push for --prod, --rotate, or explicit repair against an active deployment.
  5. Use kitcn insights when you need the upstream Convex insights view from the same CLI entrypoint.
  6. If browser smoke says use http://localhost:3000 but port 3000 is occupied, run smoke on the actual dev server port instead and note it in the report.

Docs-by-section approach:

  1. Follow Quickstart for initial setup
  2. Read individual docs for each feature needed
  3. Use CLI Registry as the bootstrap reference
  4. Confirm deployment/bootstrap is configured before relying on generated imports

Quick Reference by Stack

StackKey Docs
Next.js + Better AuthCLI Registry, Next.js Setup, Auth Server
Vite + No AuthReact Setup, Server Setup
Any + Custom AuthServer Setup, Middlewares

Documentation Map

Getting Started

PageWhen to Use
QuickstartFirst-time setup. Follow step-by-step to get a working app.
ConceptsUnderstand architecture, folder structure (convex/functions/, convex/lib/, convex/shared/), and design decisions.
CLI RegistryBootstrap new or existing apps with kitcn init, add features with kitcn add, and inspect plans with view, info, and docs.

Server (Backend)

PageWhen to Use
Server SetupInitialize cRPC builder with initCRPC. Configure dataModel, context, meta.
ProceduresDefine queries, mutations, actions with .input(), .output(), .query()/.mutation()/.action(). Paginated queries with .paginated().
ContextExtend context with ctx.orm, ctx.auth. Add custom properties via .context().
MiddlewaresCreate reusable middleware with .use(). Auth guards, logging, rate limiting. Chain with .pipe().
MetadataAdd procedure metadata with .meta(). Used by CLI for auth-aware query handling.
Error HandlingThrow CRPCError with codes like UNAUTHORIZED, NOT_FOUND, BAD_REQUEST.
Server-Side CallsCall procedures from server with createCallerFactory. RSC data fetching.
SchedulingRun cron jobs and one-time background work with Convex scheduler APIs.

Plugins

PageWhen to Use
PluginsDiscover cross-cutting features that span schema, runtime, and client DX.
Rate LimitingAdd token bucket and fixed/sliding window rate limiting with kitcn/ratelimit.

Database

PageWhen to Use
ORMDrizzle-style schema, queries, mutations, RLS, runtime constraints via ctx.orm.
TriggersAutomatic side effects on insert/update/delete with schema triggers.
AggregatesScalar metrics (count, aggregate) and ranked aggregate runtime.

React (Client)

PageWhen to Use
React SetupConfigure providers: QueryClientProvider, CRPCProvider. Singleton helpers.
QueriesuseQuery with crpc.path.queryOptions(). Real-time updates, skipToken for conditional queries.
MutationsuseMutation with crpc.path.mutationOptions(). Toast patterns, optimistic updates.
Infinite QueriesuseInfiniteQuery with crpc.path.infiniteQueryOptions(). Cursor-based pagination.
Type InferenceinferApiInputs, inferApiOutputs for tRPC-style type extraction.
Error HandlingHandle CRPCClientError on client. Error boundaries, toast notifications.

Next.js

PageWhen to Use
Next.js SetupConfigure convexBetterAuth for RSC. Server caller factory, JWT caching.
RSCServer components with prefetch, preloadQuery, HydrateClient. SSR data fetching.

Auth (Better Auth)

PageWhen to Use
Auth OverviewArchitecture overview. When to use Better Auth vs other auth solutions.
Auth ServerComplete Better Auth setup: schema, adapters, HTTP routes, helpers.
Auth ClientuseAuth, Authenticated/Unauthenticated components.

Reference

PageWhen to Use
CLI Backendkitcn dev for codegen. Procedure metadata generation.
MigrationsMigration hub with paths for Core, ORM, and Auth.
ComparisonCompare with tRPC, Convex hooks, other patterns.

Common Workflows

New project setup:

  1. Quickstart → basic setup
  2. CLI Registry → use kitcn init for scaffolding or adoption, and kitcn add for feature layers
  3. Concepts → understand folder structure

Add authentication:

  1. Auth Server → configure Better Auth
  2. Auth Client → use auth in components
  3. Middlewares → protect procedures

Add database features:

  1. ORM → schema + queries via ctx.orm
  2. Triggers → automatic side effects
  3. Aggregates → counts and sums

Server components (RSC):

  1. Next.js Setup → configure caller
  2. RSC → prefetch and hydrate

On this page