Back to Supabase

Use Supabase with Next.js

apps/docs/content/guides/getting-started/quickstarts/nextjs.mdx

1.26.083.0 KB
Original Source
<AiPrompt id="nextjs" />

<$Partial path="quickstart_db_setup.mdx" />

3. Create a Next.js app

Use the create-next-app command and the with-supabase template, to create a Next.js app pre-configured with Cookie-based Auth, TypeScript, and Tailwind CSS.

bash
npx create-next-app -e with-supabase

4. Install Supabase's Agent Skills (optional)

Supabase's Agent Skills is a curated set of instructions that give your AI agent procedural knowledge about working with Supabase.

Install them so your AI coding agent can produce more accurate, reliable code using current Supabase patterns, such as authentication, server-side rendering, and database migrations, rather than relying solely on training data.

To install, run the following command in the root of your project:

bash
npx skills add supabase/agent-skills

5. Declare Supabase environment variables

Rename .env.example to .env.local and populate with your Supabase connection variables that you can get from the helper below, or from the project Connect panel.

<Button variant="primary" asChild> <a href="/dashboard/project/_?showConnect=true&connectTab=frameworks&framework=nextjs"> Open Connect panel </a> </Button>
text
NEXT_PUBLIC_SUPABASE_URL=<SUBSTITUTE_SUPABASE_URL>
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=<SUBSTITUTE_SUPABASE_PUBLISHABLE_KEY>

<$Partial path="api_settings.mdx" variables={{ "framework": "nextjs", "tab": "frameworks" }} />

6. Query Supabase data from Next.js

Create a new file at app/instruments/page.tsx and populate with the following.

This selects all the rows from the instruments table you created earlier and renders them on the page.

<$CodeTabs>

ts
import { createClient } from "@/lib/supabase/server";
import { Suspense } from "react";

async function InstrumentsData() {
  const supabase = await createClient();
  const { data: instruments } = await supabase.from("instruments").select();

  return <pre>{JSON.stringify(instruments, null, 2)}</pre>;
}

export default function Instruments() {
  return (
    <Suspense fallback={<div>Loading instruments...</div>}>
      <InstrumentsData />
    </Suspense>
  );
}

</$CodeTabs>

7. Start the app

Run the development server, go to http://localhost:3000/instruments in a browser and you should see the list of instruments.

bash
npm run dev

Next steps