How do you connect your build function in next.js to supabase?

View profile

I'm trying to store API keys for Tavily API and Bifrost Virtual Keys in my Build Functions, but can't seem to connect it to supabase..

Answer
MKMG
2 answers

Join Vultr Community

Points, levels & badges

Enter challenges & submit builds

Register to events & meetups

Get your work featured

2 Answers

0
Answered: 9 days ago

To connect your Next.js build/server functions to Supabase without exposing your Tavily or Bifrost API keys, instantiate a server-side admin client using your SUPABASE_SERVICE_ROLE_KEY (never prefix it with NEXT_PUBLIC_):

0
0
Answered: 10 days ago

Hey Chideraaodesigo! If you're trying to connect your Next.js build/server functions to Supabase and securely manage keys like your Tavily API and Bifrost Virtual Keys, here is the cleanest way to set it up:


  1. Store Keys in Environment Variables: Never hardcode API keys directly inside your build functions or client-side code. Add them to your .env.local file (and in your deployment dashboard's environment settings):
    Code snippet
    NEXT_PUBLIC_SUPABASE_URL=your_supabase_url
    NEXT_PUBLIC_SUPABASE_ANON_KEY=your_anon_key
    SUPABASE_SERVICE_ROLE_KEY=your_service_role_key
    TAVILY_API_KEY=your_tavily_key
    BIFROST_VIRTUAL_KEY=your_bifrost_key
    
    (Notice that sensitive keys like SUPABASE_SERVICE_ROLE_KEY, TAVILY_API_KEY, and BIFROST_VIRTUAL_KEY do not have the NEXT_PUBLIC_ prefix so they stay strictly on the server/build side).
  2. Initialize Supabase on the Server: Install @supabase/supabase-js and create a server-side client inside your API route, Server Action, or build script (getStaticProps / Server Component):
    JavaScript
    import { createClient } from '@supabase/supabase-js';
    
    const supabase = createClient(
      process.env.NEXT_PUBLIC_SUPABASE_URL,
      process.env.SUPABASE_SERVICE_ROLE_KEY // Use service role key for server/build functions
    );
    
  3. Storing/Fetching Keys in Supabase (Optional): If you are saving user-specific Tavily or Bifrost keys inside a Supabase table rather than just reading them from .env, ensure Row Level Security (RLS) is enabled on that table and only query it from your server-side Next.js routes using the SUPABASE_SERVICE_ROLE_KEY so the keys are never exposed to the browser.

Hope this helps get your connection working! Let me know if you're hitting a specific error message.

0

Post Answer