DEV Community

Abdullah Hashem
Abdullah Hashem

Posted on

How to Connect OpenAI with Supabase in 10 Minutes for a Lightning-Fast AI MVP

In the fast-paced world of AI startups, speed is everything. Founders don't have months to wait for a sluggish development cycle. They need their Minimum Viable Product (MVP) live, tested, and generating feedback immediately.

Today, I’m going to show you a killer, production-ready snippet to securely connect OpenAI’s API with Supabase (as your backend/database) to build a lightning-fast AI features hub in less than 10 minutes.

🛠️ The Tech Stack

  • Framework: Next.js (App Router)
  • Database & Auth: Supabase
  • AI Engine: OpenAI API

🚀 Step 1: Initialize the Clients

First, let's set up our API client initialization. Security first—never expose your secret keys to the client side.


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

// Initialize Supabase
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
export const supabase = createClient(supabaseUrl, supabaseAnonKey);

// Initialize OpenAI
export const openai = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
});
Enter fullscreen mode Exit fullscreen mode

Top comments (0)