DATABASES / LESSON 13
Setting Up Convex.
Add Convex to a new Next.js app by following the official quickstart step by step

Loading the lesson…
Loading activities…
DATABASES / LESSON 13
Add Convex to a new Next.js app by following the official quickstart step by step

Loading the lesson…
Loading activities…
You know what Convex is now. Time to actually plug it into a real Next.js app. We'll follow the official Next.js quickstart from the Convex docs, using the App Router.
You'll need Node.js and npm installed, plus a terminal you don't mind typing into. The Convex tutorial asks for Node.js version 20 or newer. Check yours by running node --version.
Make a new Next.js app with the create-next-app command. The docs say to pick the default option for every prompt, so just smash Enter each time.
Hop into your new app folder and install the convex package. This one package gives you both the client and server library.
Now run the Convex dev command.
The quickstart says this will prompt you to log in with GitHub, create a project, and save your deployment URLs.
It also creates a convex/ folder in your app. That's where your backend functions are going to live.
Leave this command running. It keeps an eye on your convex/ folder and syncs your functions to your dev deployment in the cloud every time you hit save.
After npx convex dev runs the first time, you'll spot a few new things in your project.
The convex/ folder. This is home base for your query and mutation functions. Inside it, Convex also keeps a _generated folder. The dev command updates it automatically so your editor knows the names and types of your functions. You don't touch those files by hand.
The .env.local file. Convex saves your project settings here so future runs of npx convex dev know which deployment to connect to. The CLI docs say this includes a CONVEX_DEPLOYMENT variable with the name of your dev deployment. For a Next.js app, your deployment URL gets saved under a name starting with NEXT_PUBLIC_, so your browser code can read it. The docs show an example like this one.
Yours will be different. That's normal (and no, you don't get to pick the funny name).
In your editor, create a file called sampleData.jsonl in your my-app folder and paste in these three lines. Each line is one document.
In your second terminal, inside the my-app folder, use the Convex import command to add a tasks table with that sample data.
Remember, Convex tables pop into existence as soon as they get their first document. No need to create the tasks table first.
In the convex/ folder, add a new file called tasks.ts with this code.
This query reads every document in the tasks table. Since the file is called tasks.ts and the export is called get, your app calls it as api.tasks.get.
Your React components need a connection to Convex. That connection is a ConvexReactClient, and you hand it to your app through a ConvexProvider.
In the app/ folder, add a new file called ConvexClientProvider.tsx.
See how it reads NEXT_PUBLIC_CONVEX_URL? That's the value Convex already saved to .env.local for you. The "use client" line at the top tells Next.js this component runs in the browser.
Open app/layout.tsx and wrap the children of the body element with ConvexClientProvider. Here's the full file from the docs.
The only new bits are the ConvexClientProvider import and the line that wraps children. Everything else came with your Next.js app.
Replace app/page.tsx with this code. It uses the useQuery hook to call your api.tasks.get function.
That ?. after tasks matters a LOT. While the data's still loading, useQuery returns undefined, so this keeps the page from crashing before your tasks show up.
With npx convex dev still running in your first terminal, start Next.js in the second one.
Open http://localhost:3000 in your browser. You should see your three tasks sitting there.
npm install convex, then run npx convex devnpx convex dev creates the convex/ folder and saves your deployment details to .env.localnpx convex dev running while you work so your functions stay in syncconvex/tasks.ts named get becomes api.tasks.get in your appConvexClientProvider connects your Next.js app to Convex using NEXT_PUBLIC_CONVEX_URLuseQuery loads data and keeps it fresh all on its ownYour app can read data now. Next we'll add a schema, write a mutation that adds new tasks and call it from a form with useMutation.