Getting Started

Getting Started with Supabase by integrating it to a Next.js application

What is Supabase?

Supabase can simply be defined as an alternative to Firebase. Diving deeper, Supabase provides core services such as database, authentication, file storage, and autogenerated APIs. It is very useful for frontend developers or mobile developers are you can have a simple functional backend in less than 10 minutes. In this article, we will see how to integrate Supabase into a Next.js application. You can read more about the Next.js project here. However, if you are in hurry feel free to clone and set up the project.

git clone https://github.com/app-generator/sample-next-js-getting-started.git

Then create a new branch called supabase.

git checkout -b supabase

The project serves some products but it's actually retrieving the data from a json file. We will use supabase to fetch the products information. Let's start by retrieving API keys and creating a Product table in the Supabase dashboard.

Supabase setup

Make sure to have an account created at Supabase. When the account is created and verified, you can create a new project.

Button to create a project

Then, enter the necessary information to create the project.

Creating project

After that, you will have access to options on the Dashboard. We need to create a new table and insert data. We will do this directly in the SQL editor.

Clicking on SQL editor

Here's the SQL script.

Now you will have a ready-to-use Table with data we can fetch.

Configuring supabase client

We’ll need to install the Supabase client package to connect to our Next.js app later. We can do so by running the following command.

After that, create a new file in the project root called supabaseClient.js.

We are using environment variables so we need to create a .env file at the root of the project.

To avoid unexpected behaviors, keep the .env file and the supabaseClient.js in the same directory.

You can find the keys on the project page.

Supabase keys

Let's fetch the product list from Supabase now.

Fetching data from the Supabase backend

In the pages/index.js file, add an asynchronous function to fetch data from supabase.

The supabase client provides functions and methods to fetch data from a Table in the most intuitive way possible. Remove this line from the imports to avoid conflicts.

Start the dev server and head to http://localhost:3000.

Final result

Conclusion

In this document, we made a quick introduction to Supabase by learning how to set up a project, create a table and fetch data. In the next steps, we will dive into authentication and more complex CRUD operations using Next.js and Supabase.

Last updated

Was this helpful?