📖 Scenario: You are building a web app that needs to call a serverless function hosted on Supabase Edge Functions. This function will return a greeting message based on the user's name.
🎯 Goal: Learn how to set up a client call to a Supabase Edge Function and handle the response.
📋 What You'll Learn
Create a variable to hold the user's name
Create a Supabase client instance with the correct URL and anon key
Call the Edge Function named 'hello' with the user's name as a parameter
Handle the response and store the greeting message
💡 Why This Matters
🌍 Real World
Calling serverless functions from client apps is common for adding dynamic, secure backend logic without managing servers.
💼 Career
Understanding how to invoke cloud functions from clients is essential for modern web developers and cloud engineers working with serverless architectures.
Progress0 / 4 steps
1
Create a variable for the user's name
Create a variable called userName and set it to the string 'Alice'.
Supabase
Hint
Use const to declare the variable and assign the exact string 'Alice'.
2
Create a Supabase client instance
Create a constant called supabase and assign it the result of calling createClient with the URL 'https://xyzcompany.supabase.co' and anon key 'public-anon-key'.
Supabase
Hint
Use createClient from @supabase/supabase-js with the exact URL and anon key.
3
Call the Edge Function with the user's name
Use await supabase.functions.invoke to call the Edge Function named 'hello' with options { body: { name: userName } }. Destructure the result as const { data: response }.
Supabase
Hint
Use await, destructure { data: response } from the invoke call, and pass { body: { name: userName } } as options.
4
Extract and store the greeting message
Create a constant called greeting and assign it the text content of response by calling await response.text().
Supabase
Hint
Use await response.text() to get the message string.
Practice
(1/5)
1. What is the main purpose of using supabase.functions.invoke() in a client application?
easy
A. To create new user accounts automatically
B. To directly access the database without authentication
C. To upload files to Supabase storage
D. To call Edge Functions and get fast responses near the user
Solution
Step 1: Understand what Edge Functions do
Edge Functions run code close to users to reduce delay and improve speed.
Step 2: Identify the role of supabase.functions.invoke()
This method is used to call those Edge Functions from the client side.
Final Answer:
To call Edge Functions and get fast responses near the user -> Option D
Quick Check:
Invoke calls Edge Functions [OK]
Hint: Invoke calls Edge Functions for fast user responses [OK]
Common Mistakes:
Thinking it accesses database directly
Confusing with file upload methods
Assuming it creates users automatically
2. Which of the following is the correct syntax to invoke an Edge Function named hello using Supabase client in JavaScript?
easy
A. await supabase.functions.invoke('hello')
B. supabase.invoke.functions('hello')
C. await supabase.functions.call('hello')
D. supabase.functions.run('hello')
Solution
Step 1: Recall the correct method name
The method to call Edge Functions is supabase.functions.invoke().
Step 2: Check the syntax for calling a function named 'hello'
The correct syntax is await supabase.functions.invoke('hello').
Final Answer:
await supabase.functions.invoke('hello') -> Option A
Quick Check:
Correct method is invoke() [OK]
Hint: Use supabase.functions.invoke('functionName') [OK]
Common Mistakes:
Using wrong method names like call or run
Swapping order of functions and invoke
Forgetting to await the async call
3. What will be the output of the following code snippet if the Edge Function returns {"message": "Hello World"}?
B. Missing await keyword for the async invoke call
C. Using console.log instead of alert
D. Incorrect function name 'dataFetch'
Solution
Step 1: Check async call usage
supabase.functions.invoke() returns a promise and must be awaited.
Step 2: Identify missing await
The code assigns the promise to response without awaiting, so response.data is undefined.
Final Answer:
Missing await keyword for the async invoke call -> Option B
Quick Check:
Async calls need await [OK]
Hint: Always await async invoke calls [OK]
Common Mistakes:
Forgetting await on async functions
Assuming invoke returns data synchronously
Ignoring promise nature of invoke()
5. You want to send JSON data {"userId": 123} to an Edge Function named getUserData and handle errors properly. Which code snippet correctly does this?
hard
A. const { data, error } = await supabase.functions.invoke('getUserData', body: { userId: 123 }); if (error) console.error(error); else console.log(data);
B. const { data, error } = supabase.functions.invoke('getUserData', { body: JSON.stringify({ userId: 123 }) }); if (error) console.error(error); else console.log(data);