Supabase

How to set up Supabase with Next.js from scratch?

Discover how to set up Supabase with Next.js effortlessly. Dive into our detailed step-by-step guide, and seamlessly integrate these robust tools into your next web development project.

Developer profile skeleton
a developer thinking

Overview

Setting up Supabase with Next.js blends a serverless backend with a fast front end beautifully. Supabase's real-time database and authentication work hand in hand with Next.js's static and dynamic rendering, giving developers the tools to build strong full-stack apps. You'll start off by setting up a Next.js project, then configure Supabase, and finally, smoothly integrate them. This guide covers key steps, from prepping your development environment to getting your project deployed, making sure the integration of these awesome tools goes off without a hitch.

Get a Free No-Code Consultation
Meet with Will, CEO at Bootstrapped to get a Free No-Code Consultation
Book a Call
Will Hawkins
CEO at Bootstrapped

How to set up Supabase with Next.js from scratch?

Step 1: Create a Next.js Project

First things first, make sure you've got Node.js installed on your machine. Then, let's kick off a new Next.js project. Just pop open your terminal and run:

npx create-next-app@latest supabase-next
cd supabase-next

Step 2: Install Supabase Client

Now, head over to your project directory and install the Supabase client. It's super easy:

npm install @supabase/supabase-js

Step 3: Set Up Supabase Project

Alright, time to sign up or log in to Supabase. Once you're in, create a new project. After that, go to "Settings" → "API" to grab your API URL and API KEY.

Step 4: Configure Environment Variables

In the root of your Next.js project, create a .env.local file. Add these variables with the values you got from the Supabase dashboard:

NEXT_PUBLIC_SUPABASE_URL=your-supabase-url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-supabase-anon-key

Step 5: Initialize Supabase Client

Next, create a file named supabaseClient.js in your project directory. This is where you'll initialize the Supabase client:

import { createClient } from '@supabase/supabase-js';

const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;

export const supabase = createClient(supabaseUrl, supabaseAnonKey);

Step 6: Use Supabase in a Next.js Page

To use Supabase in a Next.js page, import the Supabase client and perform queries or mutations. For example, create a pages/index.js file:

import React, { useEffect, useState } from 'react';
import { supabase } from '../supabaseClient';

export default function Home() {
  const [data, setData] = useState([]);

  useEffect(() => {
    const fetchData = async () => {
      let { data: items, error } = await supabase
        .from('your-table-name')
        .select('*');
      
      if (error) console.error('Error fetching data:', error);
      else setData(items);
    };

    fetchData();
  }, []);

  return (
    <div>
      <h1>Data from Supabase</h1>
      <pre>{JSON.stringify(data, null, 2)}</pre>
    </div>
  );
}

Step 7: Test the Setup

Fire up the Next.js development server to make sure everything's working:

npm run dev

Then, open your browser and go to http://localhost:3000 to see the data fetched from Supabase.

Step 8: Optional - Configure Authentication

If you want to enable authentication, set up Auth within the Supabase dashboard:

  1. Go to the "Authentication" tab in your Supabase project.
  2. Enable the authentication providers you want (like email, GitHub, Google).
  3. Configure sign-in/sign-up functionality in your Next.js app using the Supabase client.
// Example of a sign-in function
const signInWithEmail = async (email, password) => {
  const { user, session, error } = await supabase.auth.signIn({ email, password });
  if (error) console.error('Error signing in:', error);
  else console.log('User signed in:', user);
};

Note: Make sure to add the corresponding UI elements like forms and buttons so users can interact with the authentication functions.

Explore more Supabase tutorials

Complete Guide to Supabase: Tutorials, Tips, and Best Practices

Explore our Supabase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.

Why are companies choosing Bootstrapped?

40-60%

Faster with no-code

Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.

90 days

From idea to MVP

Save time, money, and energy with an optimized hiring process. Access a pool of experts who are sourced, vetted, and matched to meet your precise requirements.

1 283 apps

built by our developers

With the Bootstrapped platform, managing projects and developers has never been easier.

hero graphic

Our capabilities

Bootstrapped offers a comprehensive suite of capabilities tailored for startups. Our expertise spans web and mobile app development, utilizing the latest technologies to ensure high performance and scalability. The team excels in creating intuitive user interfaces and seamless user experiences. We employ agile methodologies for flexible and efficient project management, ensuring timely delivery and adaptability to changing requirements. Additionally, Bootstrapped provides continuous support and maintenance, helping startups grow and evolve their digital products. Our services are designed to be affordable and high-quality, making them an ideal partner for new ventures.

Engineered for you

1

Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.

2

Tailored Solutions: The company offers customized app development, adapting to specific business needs and goals, which ensures your app stands out in the competitive market.

3

Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.

4

Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.

5

Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.

6

Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.

Yes, if you can dream it, we can build it.