Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Initialize a new Convex project or add Convex to an existing React, Next.js, Vue, or Svelte app.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
SKILL.md
1---2name: convex-quickstart3description:4Creates or adds Convex to an app. Use for new Convex projects, npm create5convex@latest, frontend setup, env vars, or the first npx convex dev run.6---78# Convex Quickstart910Set up a working Convex project as fast as possible.1112## When to Use1314- Starting a brand new project with Convex15- Adding Convex to an existing React, Next.js, Vue, Svelte, or other app16- Scaffolding a Convex app for prototyping1718## When Not to Use1920- The project already has Convex installed and `convex/` exists - just start21building22- You only need to add auth to an existing Convex app - use the23`convex-setup-auth` skill2425## Workflow26271. Determine the starting point: new project or existing app282. If new project, pick a template and scaffold with `npm create convex@latest`293. If existing app, install `convex` and wire up the provider304. Run `npx convex dev --once` to provision a local anonymous deployment, push31the current `convex/` code, typecheck it, and regenerate types — all in one32shot, exiting cleanly. The output tells the agent whether the schema and33functions are valid.345. Ask the user (or, for cloud agents, start in the background) `npm run dev` —35Convex templates wire the watcher and the frontend into a single command. If36the project has no combined dev script, use `npx convex dev` for the watcher37and run the frontend separately.386. Verify the setup works3940## Path 1: New Project (Recommended)4142Use the official scaffolding tool. It creates a complete project with the43frontend framework, Convex backend, and all config wired together.4445### Pick a template4647| Template | Stack |48| -------------------------- | ----------------------------------------- |49| `react-vite-shadcn` | React + Vite + Tailwind + shadcn/ui |50| `nextjs-shadcn` | Next.js App Router + Tailwind + shadcn/ui |51| `react-vite-clerk-shadcn` | React + Vite + Clerk auth + shadcn/ui |52| `nextjs-clerk` | Next.js + Clerk auth |53| `nextjs-convexauth-shadcn` | Next.js + Convex Auth + shadcn/ui |54| `nextjs-lucia-shadcn` | Next.js + Lucia auth + shadcn/ui |55| `bare` | Convex backend only, no frontend |5657If the user has not specified a preference, default to `react-vite-shadcn` for58simple apps or `nextjs-shadcn` for apps that need SSR or API routes.5960You can also use any GitHub repo as a template:6162```bash63npm create convex@latest my-app -- -t owner/repo64npm create convex@latest my-app -- -t owner/repo#branch65```6667### Scaffold the project6869Always pass the project name and template flag to avoid interactive prompts:7071```bash72npm create convex@latest my-app -- -t react-vite-shadcn73cd my-app74npm install75```7677The scaffolding tool creates files but does not run `npm install`, so you must78run it yourself.7980To scaffold in the current directory (if it is empty):8182```bash83npm create convex@latest . -- -t react-vite-shadcn84npm install85```8687### Provision the deployment and push code8889Run this yourself — it is a one-shot command that exits cleanly:9091```bash92npx convex dev --once93```9495In a non-TTY environment (which is true for almost every agent run), this:9697- Provisions an _anonymous_ local Convex backend bound to `127.0.0.1`. No98browser login, no team/project prompts.99- Writes `CONVEX_DEPLOYMENT` and the framework's `*_CONVEX_URL` variables to100`.env.local`.101- Generates `convex/_generated/`.102- Pushes the current `convex/` code to the deployment, **typechecks it**, and103**validates the schema**. The agent reads this output to find out if the code104it just wrote is broken.105106To be explicit (recommended), set `CONVEX_AGENT_MODE=anonymous` so the behavior107does not depend on TTY detection:108109```bash110CONVEX_AGENT_MODE=anonymous npx convex dev --once111```112113The deployment lives under `~/.convex/` and persists across runs. Re-running114`convex dev --once` after editing `convex/` files is the agent's main feedback115loop while the user-launched `npm run dev` is not in use.116117If the template's `package.json` defines a `predev` script (Convex Auth118templates and similar do), `npm run predev` runs `convex init` plus any one-time119setup (e.g. minting auth keys). Use it _in addition to_ `convex dev --once` when120present — `predev` handles the one-time setup, `convex dev --once` pushes and121validates the code.122123### Start the dev loop124125In most Convex templates, `npm run dev` runs both the Convex watcher and the126frontend dev server together (typically `convex dev --start 'vite --open'` or127the Next.js equivalent). That is what the user should run.128129```bash130npm run dev131```132133If the project does not have a combined `dev` script — e.g. the `bare` template,134or an existing app where you haven't wired the frontend dev server into Convex's135`--start` flag — the user can run the Convex watcher directly:136137```bash138npx convex dev139```140141`npx convex dev` is the same long-running watcher `npm run dev` invokes under142the hood; it just doesn't start the frontend. Use it when there is no frontend,143or when the user prefers to run the frontend in a separate terminal.144145Either way, the agent should not invoke the watcher in the foreground because it146does not exit. Two options:147148- **Local development (user is at the keyboard):** ask the user to run149`npm run dev` (or `npx convex dev`) in a terminal. The deployment provisioned150by `convex dev --once` above is already selected, so the watcher picks up151immediately with no prompts.152- **Cloud or headless agents:** start `npm run dev` (or `npx convex dev`) in the153background.154155Vite apps serve on `http://localhost:5173`, Next.js on `http://localhost:3000`.156157### What you get158159After scaffolding, the project structure looks like:160161```162my-app/163convex/ # Backend functions and schema164_generated/ # Auto-generated types (check this into git)165schema.ts # Database schema (if template includes one)166src/ # Frontend code (or app/ for Next.js)167package.json168.env.local # CONVEX_URL / VITE_CONVEX_URL / NEXT_PUBLIC_CONVEX_URL169```170171The template already has:172173- `ConvexProvider` wired into the app root174- Correct env var names for the framework175- Tailwind and shadcn/ui ready (for shadcn templates)176- Auth provider configured (for auth templates)177178Proceed to adding schema, functions, and UI.179180## Path 2: Add Convex to an Existing App181182Use this when the user already has a frontend project and wants to add Convex as183the backend.184185### Install186187```bash188npm install convex189```190191### Provision and push192193Run `npx convex dev --once` yourself to provision a local anonymous deployment,194write `.env.local`, generate types, push the current `convex/` code, and195typecheck it. This is one-shot and exits:196197```bash198npx convex dev --once199```200201The output tells you whether the schema and functions are valid — use it as your202feedback loop while iterating.203204Then ask the user to start the watcher (or, for cloud/headless agents, start it205in the background). You have two options:206207- **Wire Convex into `npm run dev`** — change the existing app's `dev` script to208`convex dev --start '<existing dev command>'`. That's the standard pattern209Convex templates use; the user then runs a single `npm run dev` to start both.210- **Run them separately** — leave `npm run dev` for the frontend and tell the211user to run `npx convex dev` in a second terminal for the Convex watcher.212213See "Start the dev loop" above for why the agent should not run the watcher in214the foreground.215216### Wire up the provider217218The Convex client must wrap the app at the root. The setup varies by framework.219220Create the `ConvexReactClient` at module scope, not inside a component:221222```tsx223// Bad: re-creates the client on every render224function App() {225const convex = new ConvexReactClient(226import.meta.env.VITE_CONVEX_URL as string,227);228return <ConvexProvider client={convex}>...</ConvexProvider>;229}230231// Good: created once at module scope232const convex = new ConvexReactClient(import.meta.env.VITE_CONVEX_URL as string);233function App() {234return <ConvexProvider client={convex}>...</ConvexProvider>;235}236```237238#### React (Vite)239240```tsx241// src/main.tsx242import { StrictMode } from "react";243import { createRoot } from "react-dom/client";244import { ConvexProvider, ConvexReactClient } from "convex/react";245import App from "./App";246247const convex = new ConvexReactClient(import.meta.env.VITE_CONVEX_URL as string);248249createRoot(document.getElementById("root")!).render(250<StrictMode>251<ConvexProvider client={convex}>252<App />253</ConvexProvider>254</StrictMode>,255);256```257258#### Next.js (App Router)259260```tsx261// app/ConvexClientProvider.tsx262"use client";263264import { ConvexProvider, ConvexReactClient } from "convex/react";265import { ReactNode } from "react";266267const convex = new ConvexReactClient(process.env.NEXT_PUBLIC_CONVEX_URL!);268269export function ConvexClientProvider({ children }: { children: ReactNode }) {270return <ConvexProvider client={convex}>{children}</ConvexProvider>;271}272```273274```tsx275// app/layout.tsx276import { ConvexClientProvider } from "./ConvexClientProvider";277278export default function RootLayout({279children,280}: {281children: React.ReactNode;282}) {283return (284<html lang="en">285<body>286<ConvexClientProvider>{children}</ConvexClientProvider>287</body>288</html>289);290}291```292293#### Other frameworks294295For Vue, Svelte, React Native, TanStack Start, Remix, and others, follow the296matching quickstart guide:297298- [Vue](https://docs.convex.dev/quickstart/vue)299- [Svelte](https://docs.convex.dev/quickstart/svelte)300- [React Native](https://docs.convex.dev/quickstart/react-native)301- [TanStack Start](https://docs.convex.dev/quickstart/tanstack-start)302- [Remix](https://docs.convex.dev/quickstart/remix)303- [Node.js (no frontend)](https://docs.convex.dev/quickstart/nodejs)304305### Environment variables306307The env var name depends on the framework:308309| Framework | Variable |310| ------------ | ------------------------ |311| Vite | `VITE_CONVEX_URL` |312| Next.js | `NEXT_PUBLIC_CONVEX_URL` |313| Remix | `CONVEX_URL` |314| React Native | `EXPO_PUBLIC_CONVEX_URL` |315316`npx convex dev` writes the correct variable to `.env.local` automatically.317318## Agent Mode319320`CONVEX_AGENT_MODE=anonymous` forces an unauthenticated local backend. It is321already the implicit default for any non-TTY run of `npx convex init` or322`npx convex dev`, but set it explicitly so the behavior does not depend on TTY323detection:324325```bash326CONVEX_AGENT_MODE=anonymous npx convex dev --once327```328329Use it for:330331- Any AI coding agent (local or cloud).332- CI-like setup scripts.333- Cases where the user is logged in but you do not want to touch their personal334dev deployment.335336The resulting backend runs on `127.0.0.1` and is not associated with any team or337project until the user later claims it via `npx convex login` and the338`npx convex deployment` commands.339340## Verify the Setup341342After setup, confirm everything is working:3433441. `npx convex dev --once` exited without errors (deployment provisioned, code345pushed, schema validated, typecheck clean)3462. The `convex/_generated/` directory exists and has `api.ts` and `server.ts`3473. `.env.local` contains a `CONVEX_DEPLOYMENT` value and the framework's348`*_CONVEX_URL` variable3494. (If applicable) `npm run dev` (or `npx convex dev` for the watcher alone) is350running without errors in another terminal or in the background351352## Writing Your First Function353354Once the project is set up, create a schema and a query to verify the full loop355works.356357`convex/schema.ts`:358359```ts360import { defineSchema, defineTable } from "convex/server";361import { v } from "convex/values";362363export default defineSchema({364tasks: defineTable({365text: v.string(),366completed: v.boolean(),367}),368});369```370371`convex/tasks.ts`:372373```ts374import { query, mutation } from "./_generated/server";375import { v } from "convex/values";376377export const list = query({378args: {},379handler: async (ctx) => {380return await ctx.db.query("tasks").collect();381},382});383384export const create = mutation({385args: { text: v.string() },386handler: async (ctx, args) => {387await ctx.db.insert("tasks", { text: args.text, completed: false });388},389});390```391392Use in a React component (adjust the import path based on your file location393relative to `convex/`):394395```tsx396import { useQuery, useMutation } from "convex/react";397import { api } from "../convex/_generated/api";398399function Tasks() {400const tasks = useQuery(api.tasks.list);401const create = useMutation(api.tasks.create);402403return (404<div>405<button onClick={() => create({ text: "New task" })}>Add</button>406{tasks?.map((t) => (407<div key={t._id}>{t.text}</div>408))}409</div>410);411}412```413414## Development vs Production415416Always use `npx convex dev` during development. It runs against your personal417dev deployment and syncs code on save.418419When ready to ship, deploy to production:420421```bash422npx convex deploy423```424425This pushes to the production deployment, which is separate from dev. Do not use426`deploy` during development.427428## Next Steps429430- Add authentication: use the `convex-setup-auth` skill431- Design your schema: see432[Schema docs](https://docs.convex.dev/database/schemas)433- Build components: use the `convex-create-component` skill434- Plan a migration: use the `convex-migration-helper` skill435- Add file storage: see436[File Storage docs](https://docs.convex.dev/file-storage)437- Set up cron jobs: see [Scheduling docs](https://docs.convex.dev/scheduling)438439## Checklist440441- [ ] Determined starting point: new project or existing app442- [ ] If new project: scaffolded with `npm create convex@latest` using443appropriate template444- [ ] If existing app: installed `convex` and wired up the provider445- [ ] Agent ran `npx convex dev --once`: deployment provisioned, code pushed,446typecheck clean447- [ ] `npm run dev` (or `npx convex dev` for the watcher alone) is running —448user-launched terminal, or background for cloud agents449- [ ] `convex/_generated/` directory exists with types450- [ ] `.env.local` has the deployment URL451- [ ] Verified a basic query/mutation round-trip works452