📖 Scenario: You are building a small cloud function that runs at the edge. This function will call an external API to get some data and then return it. Edge Functions run close to users for fast response.
🎯 Goal: Create an Edge Function in Supabase that calls the external API https://api.agify.io?name=michael to get the predicted age for the name 'michael'. You will set up the function, configure the API URL, fetch the data, and return the JSON response.
📋 What You'll Learn
Create a variable with the external API URL
Add a configuration variable for the name parameter
Write the fetch call to get data from the API
Return the JSON response from the Edge Function
💡 Why This Matters
🌍 Real World
Edge Functions are used to run code close to users for fast responses. Calling external APIs lets you enrich your functions with live data from other services.
💼 Career
Many cloud jobs require writing serverless functions that integrate with external APIs. This project teaches the basics of that important skill.
Progress0 / 4 steps
1
Set up the API URL variable
Create a variable called apiUrl and set it to the string "https://api.agify.io?name=michael".
Supabase
Hint
Use const apiUrl = "https://api.agify.io?name=michael"; to store the API URL.
2
Add a configuration variable for the name
Create a variable called name and set it to the string "michael". Then update apiUrl to use this name variable in the URL with template literals.
Supabase
Hint
Use const name = "michael"; and template literals like `https://api.agify.io?name=${name}`.
3
Fetch data from the external API
Write an async function called handler that fetches data from apiUrl using fetch. Await the response and then await response.json() to get the data.
Supabase
Hint
Use export async function handler() and inside it const response = await fetch(apiUrl); then const data = await response.json();.
4
Return the JSON response from the Edge Function
In the handler function, return a new Response object with the JSON stringified data and set the header Content-Type to application/json.
Supabase
Hint
Use return new Response(JSON.stringify(data), { headers: { "Content-Type": "application/json" } }); to return JSON properly.
Practice
(1/5)
1. What is the main purpose of using fetch in Supabase Edge Functions when calling external APIs?
easy
A. To create user interface components
B. To store data permanently in Supabase database
C. To run SQL queries inside the Edge Function
D. To send requests and receive data from external services
Solution
Step 1: Understand the role of fetch in Edge Functions
fetch is used to send HTTP requests to external APIs and get responses back.
Step 2: Identify what fetch does not do
It does not store data permanently or create UI components; it only communicates with external services.
Final Answer:
To send requests and receive data from external services -> Option D
Quick Check:
fetch calls external APIs = C [OK]
Hint: Remember: fetch is for talking to outside services [OK]
Common Mistakes:
Thinking fetch stores data permanently
Confusing fetch with database queries
Assuming fetch creates UI elements
2. Which of the following is the correct syntax to call an external API using fetch inside a Supabase Edge Function?
easy
A. const response = fetch('https://api.example.com/data').then(res => res.json())
B. const response = fetch.get('https://api.example.com/data')
C. const response = fetch('https://api.example.com/data')
D. const response = fetch('https://api.example.com/data').json()
Solution
Step 1: Recall the correct usage of fetch
fetch returns a promise; to get JSON data, you call then(res => res.json()).
Step 2: Check each option for syntax correctness
const response = fetch('https://api.example.com/data') misses handling the promise; B uses invalid method; D incorrectly calls json() directly on fetch.
Final Answer:
const response = fetch('https://api.example.com/data').then(res => res.json()) -> Option A
Quick Check:
fetch + then(res.json()) = A [OK]
Hint: Always handle fetch with then() or await for JSON [OK]
Common Mistakes:
Calling json() directly on fetch without then or await
Using fetch.get which does not exist
Ignoring the promise returned by fetch
3. Consider this Edge Function code snippet:
export async function handler() {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
return new Response(JSON.stringify({ result: data.value }), { status: 200 });
}
If the external API returns { "value": 42 }, what will the Edge Function return?
medium
A. An error because data.value is undefined
B. A response with body '{"result":42}' and status 200
C. A response with body '42' and status 404
D. A response with body '{"value":42}' and status 200
Solution
Step 1: Understand the API response and parsing
The API returns JSON {"value": 42}, which is parsed into data object with data.value = 42.
Step 2: Check the returned response from the Edge Function
The function returns JSON string with key "result" and value data.value (42), status 200.
Final Answer:
A response with body '{"result":42}' and status 200 -> Option B
Quick Check:
API value 42 mapped to result key = D [OK]
Hint: Map API data correctly before returning JSON [OK]
Common Mistakes:
Returning original API JSON without mapping
Confusing status codes
Assuming data.value is undefined
4. You wrote this Edge Function code to call an external API:
export async function handler() {
const response = fetch('https://api.example.com/data');
const data = await response.json();
return new Response(JSON.stringify(data), { status: 200 });
}
What is the main error in this code?
medium
A. Missing await before fetch causing response to be a Promise
B. Using await on response.json() is incorrect
C. Response constructor cannot take JSON string
D. The function should not return a Response object
Solution
Step 1: Identify how fetch is called
fetch returns a Promise, so it must be awaited to get the response object.
Step 2: Check usage of await on response.json()
Awaiting response.json() is correct; the error is missing await on fetch.
Final Answer:
Missing await before fetch causing response to be a Promise -> Option A
Quick Check:
Missing await before fetch causing response to be a Promise = B [OK]
Hint: Always await fetch before calling response methods [OK]
Common Mistakes:
Not awaiting fetch before response.json()
Thinking response.json() does not need await
Misunderstanding Response constructor usage
5. You want to call two different external APIs sequentially inside a Supabase Edge Function and combine their results before returning. Which code snippet correctly does this?