/supabase-tutorials

How to connect Supabase with PostgREST API?

Step-by-step guide to connect Supabase with PostgREST API: set up your project, configure the database, enable the API, secure it, and integrate with client apps.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to connect Supabase with PostgREST API?

 

Step 1: Set Up Supabase Project

 

  1. Go to the Supabase website and create an account if you haven't already.
  2. Once logged in, create a new project by clicking the "New Project" button.
  3. Fill in the necessary details for your project, such as the name, organization, and database password.
  4. Click on "Create New Project" to initiate the setup.

 

Step 2: Configure Supabase Database

 

  1. After your project is created, you will be redirected to the project's dashboard.
  2. Navigate to the "Database" section from the left-hand menu.
  3. Here you can see the connection details for your database; make sure you note down the credentials as you will need them later.

 

Step 3: Enable API

 

  1. In the project dashboard, navigate to the "API" section.
  2. Here, you will find the details about your project's RESTful API.
  3. Ensure the PostgREST API is enabled and take note of the URL as it will be essential for connecting clients.

 

Step 4: Configure PostgREST on Supabase

 

  1. Head to the "Database" section and open the SQL editor.
  2. Write SQL queries to define your database schema, tables, and functions as needed.
  3. Each table and function you create will automatically be exposed by the PostgREST API provided by Supabase.

Example SQL for creating a table:


CREATE TABLE profiles (
  id SERIAL PRIMARY KEY,
  username TEXT UNIQUE,
  bio TEXT
);

 

Step 5: Test PostgREST API

 

  1. Once your tables are set up, go back to the "API" tab.
  2. From here, you can explore the RESTful endpoints automatically generated by PostgREST.
  3. Use a tool like Postman or curl command to test these endpoints.

Example curl command to fetch all profiles:


curl -X GET 'https://your-project-ref.supabase.co/rest/v1/profiles' \\
-H "apikey: your_anon_key"

 

Step 6: Secure API Access

 

  1. Navigate to the "Auth" section to manage user authentication and policies.
  2. Utilize Supabase Auth to control access to the API by defining RLS (Row Level Security) policies.
  3. In the SQL editor, write policies to control access to your database tables.

Example RLS policy to allow authenticated users to select from profiles:


CREATE POLICY "Allow authenticated read access" 
ON profiles 
FOR SELECT 
USING (auth.role() = 'authenticated');

 

Step 7: Integrate PostgREST API with Client Applications

 

  1. Use any HTTP client library in your application to make API requests.
  2. Include the API URL and necessary headers like API key or authentication token in your requests.

Example JavaScript fetch request:


fetch('https://your-project-ref.supabase.co/rest/v1/profiles', {
  method: 'GET',
  headers: {
    'apikey': 'your_anon_key',
    'Authorization': 'Bearer your_jwt_token'
  }
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022