Supabase

How to use Supabase with Express.js?

Discover how to effortlessly blend Supabase with Express.js to create robust, scalable apps with minimal hassle. Includes a detailed step-by-step guide.

Developer profile skeleton
a developer thinking

Overview

Combining Supabase with Express.js offers the ease of a PostgreSQL-based backend-as-a-service alongside the versatility of a renowned Node.js web framework. With Supabase, you get instant APIs, authentication, and real-time capabilities ready to use, simplifying the creation of scalable applications significantly. Using Express.js, a minimalist web framework, promotes rapid development and integrates smoothly with different middleware. This guide outlines the key steps to link Supabase to an Express.js server — covering everything from installation and setup to basic CRUD operations — to streamline your development workflow efficiently.

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 use Supabase with Express.js?

Step 1: Set up a new Express.js project

  1. First things first, make sure you've got Node.js and npm (Node Package Manager) installed on your machine. If not, you can grab them from nodejs.org.
  2. Create a new directory for your project and navigate into it:
    ```bash
    mkdir my-express-app
    cd my-express-app
    ```
  3. Initialize a new Node.js project using npm:
    ```bash
    npm init -y
    ```
  4. Install Express.js:
    ```bash
    npm install express
    ```

Step 2: Install Supabase client libraries

  1. Install the Supabase client library:
    ```bash
    npm install @supabase/supabase-js
    ```

Step 3: Set up Supabase project

  1. Head over to the Supabase website and sign up if you don't already have an account.
  2. Create a new project and jot down your API URL and anon key. You'll need these to configure your Supabase client.

Step 4: Create an Express.js server

  1. Create a new file named app.js in your project directory:
    ```bash
    touch app.js
    ```

  2. Open app.js in your text editor and set up a basic Express server:

    ```javascript
    const express = require('express');
    const { createClient } = require('@supabase/supabase-js');

    const app = express();
    const port = 3000;

    const SUPABASE_URL = 'https://your-supabase-url.supabase.co';
    const SUPABASE_ANON_KEY = 'your-supabase-anon-key';

    const supabase = createClient(SUPABASE_URL, SUPABASE_ANON_KEY);

    app.use(express.json());

    app.get('/', (req, res) => {
    res.send('Hello World!');
    });

    app.listen(port, () => {
    console.log(Server running at http://localhost:${port});
    });
    ```

  3. Replace https://your-supabase-url.supabase.co and your-supabase-anon-key with the actual values from your Supabase project.

Step 5: Use Supabase client within Express.js routes

  1. To interact with your database, create a new route in app.js. Here's an example route to fetch data from a table named users:

    ```javascript
    app.get('/users', async (req, res) => {
    const { data, error } = await supabase
    .from('users')
    .select('*');
    if (error) {
    console.error(error);
    return res.status(500).json({ error: error.message });
    }

    res.json(data);
    });
    ```

  2. To insert data into the users table, add a POST route:

    ```javascript
    app.post('/users', async (req, res) => {
    const { name, email } = req.body;
    const { data, error } = await supabase
    .from('users')
    .insert([{ name, email }]);
    if (error) {
    console.error(error);
    return res.status(500).json({ error: error.message });
    }

    res.status(201).json(data);
    });
    ```

Step 6: Test your routes

  1. Start your Express.js server:
    ```bash
    node app.js
    ```
  2. Open your browser or a tool like Postman and navigate to http://localhost:3000/users to fetch users or send a POST request to http://localhost:3000/users with JSON data { "name": "John Doe", "email": "[email protected]" } to insert a new user.

Now the Supabase and Express.js integration is ready for further development!

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.