0
0
Supabasecloud~10 mins

Calling external APIs from Edge Functions in Supabase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Calling external APIs from Edge Functions
Edge Function Triggered
Prepare API Request
Send HTTP Request to External API
Wait for API Response
Process Response Data
Return Data to Client
End
When an Edge Function runs, it prepares and sends a request to an external API, waits for the response, processes it, and returns the result to the client.
Execution Sample
Supabase
export default async function handler(req) {
  const response = await fetch('https://api.example.com/data');
  const data = await response.json();
  return new Response(JSON.stringify(data), { status: 200 });
}
This Edge Function fetches data from an external API and returns it as JSON to the client.
Process Table
StepActionEvaluationResult
1Edge Function is triggered by client requestN/AFunction starts execution
2Prepare fetch request to 'https://api.example.com/data'N/ARequest ready to send
3Send HTTP GET requestAwait responseRequest sent, waiting for response
4Receive HTTP responseCheck status codeStatus 200 OK received
5Parse response JSONConvert response body to JSON{"key":"value"}
6Create new Response with JSON dataWrap data for clientResponse ready with status 200
7Return response to clientSend data backClient receives JSON data
8Function execution endsN/AEdge Function completes
💡 Function ends after returning the processed API data to the client
Status Tracker
VariableStartAfter Step 3After Step 5Final
reqClient request objectClient request objectClient request objectClient request object
responseundefinedPending HTTP responseHTTP response object with status 200HTTP response object
dataundefinedundefined{"key":"value"}{"key":"value"}
Key Moments - 3 Insights
Why do we use 'await' before fetch and response.json()?
Because fetch and response.json() are asynchronous operations that take time to complete. Using 'await' pauses the function until the data is ready, as shown in steps 3 and 5 of the execution_table.
What happens if the external API returns an error status?
The function receives the error status in step 4. You should check the status code and handle errors accordingly before parsing the response, which is not shown here but important for real use.
Can the Edge Function call multiple external APIs in one execution?
Yes, you can make multiple fetch calls sequentially or in parallel. Each call would follow similar steps as shown in the execution_table for each API.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'data' after step 5?
AClient request object
Bundefined
C{"key":"value"}
DPending HTTP response
💡 Hint
Check the 'data' variable in variable_tracker after step 5
At which step does the function send the HTTP request to the external API?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Action' column in execution_table for sending the request
If the external API took longer to respond, which step would be delayed?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Step 4 is when the function waits to receive the HTTP response
Concept Snapshot
Calling external APIs from Edge Functions:
- Use 'fetch' with 'await' to send HTTP requests.
- Wait for the response before processing.
- Parse response with response.json() to get data.
- Return processed data as a Response object.
- Handle errors by checking response status.
- Edge Functions run close to users for fast API calls.
Full Transcript
When an Edge Function is triggered, it prepares a fetch request to an external API. It sends the HTTP GET request and waits asynchronously for the response. Once the response arrives, it checks the status code to ensure success. Then it parses the response body as JSON data. Finally, it wraps this data in a new Response object and returns it to the client. The function ends after sending the data. Using 'await' pauses execution until each asynchronous step completes, ensuring the function processes data correctly. This flow allows Edge Functions to fetch and return external API data quickly and efficiently.