S
Supabase3mo ago
luxxe

Need help with edge functions

I want to connect my website to an edge function which has access to my ai api key. How would I do this? Thanks
22 Replies
garyaustin
garyaustin3mo ago
This shows how to use the supabase client in an edge function to make API calls and a user JWT. The API keys are environment variables. You would not need the part loading the authorization header if you are not call with with a supabase key or user JWT from your app. https://supabase.com/docs/guides/functions/auth There are many other examples there.
luxxe
luxxeOP3mo ago
With all honesty I don’t understand what this is as I’m new to supabase and only grasp the basic concept So wouldn’t I store the api key in as a secret then what would I do let me know please thanks
garyaustin
garyaustin3mo ago
The supabase keys are stored for you as secret and that code shows using them to createClient to talk to the Supabase REST API.
luxxe
luxxeOP3mo ago
Where would I store my open ai api key for example
garyaustin
garyaustin3mo ago
No description
garyaustin
garyaustin3mo ago
Ah. I missed the little ai.
garyaustin
garyaustin3mo ago
You store that in secrets using the UI for the edge functions in the dashboard. or https://supabase.com/docs/guides/functions/secrets
Managing Secrets (Environment Variables) | Supabase Docs
Managing secrets and environment variables.
luxxe
luxxeOP3mo ago
Ok I’ve done this I just don’t understand the step after this
garyaustin
garyaustin3mo ago
You use Deno.env.get('name_of_secret') like shown above.
luxxe
luxxeOP3mo ago
As whenever I deploy the edge function and connect it to my website there are a few issues from supabase So I just copy and paste this correct? But replace the secret and anon?
garyaustin
garyaustin3mo ago
Where you see 'SUPABASE_URL' in the image I clipped you would do a similar thing with your secret. There are many examples in the edge function section and I'm sure there is one using 3rd party API keys. https://supabase.com/docs/guides/functions/examples/amazon-bedrock-image-generator That one for instance shows using several keys/urls for an AI API.
luxxe
luxxeOP3mo ago
Got you But this would ideally get the job done for a chatbot just the ai logic part?
garyaustin
garyaustin3mo ago
I have no idea what it takes to do a chatbot. Look thru the 2 sections of examples, or the web or ask an AI.
Edge functions can do many different things and you are not really showing how you plan to call them or use the. You just asked about the api key.
luxxe
luxxeOP3mo ago
What I’m planning to do is the following: use the edge function just to give my website the ability to connect to the open ai api key And through my website directly I’ll edit the ui, give it a script and ect.
garyaustin
garyaustin3mo ago
Seems like using the secret to store the key, loading like above and making your API calls in the edge function with I assume data sent to it and then returning the data is certainly doable.
luxxe
luxxeOP3mo ago
Yes this is exactly what I mean
garyaustin
garyaustin3mo ago
Then find a close example or ask the/an AI to help you if you need help writing the code. Seems pretty straight forward to get the secret. Calling the API is going to be up to how that API works. And there are examples of edge functions getting data from the edge function URL Or body.
luxxe
luxxeOP3mo ago
Any chance you could help me out with the code?
garyaustin
garyaustin3mo ago
I don't think you will find users here willing to write code for you. You are the dev it is up to you to either read the docs, ask an AI and the ask for help here if you are stuck or don't understand a feature.
luxxe
luxxeOP3mo ago
As there’s an ai for creating edge functions what would I tell it?
garyaustin
garyaustin3mo ago
I don't use AI's for big picture stuff so not sure what would work. Try describing what your providing to the function and what the result you need to send back and what your AI API will do with the data... maybe.
luxxe
luxxeOP3mo ago
On supabase how’d I find the CORS settings?

Did you find this page helpful?