Supabase Functions are incredibly flexible, allowing you to handle various HTTP methods to perform different actions on your data. Whether you're fetching information (GET), creating new records (POST), updating existing ones (PUT), or removing data (DELETE), your Supabase Functions can be tailored to respond accordingly. This makes them a powerful backend for your applications, mirroring the capabilities you'd expect from a traditional RESTful API.
The core of handling different HTTP methods in Supabase Functions lies in inspecting the incoming request object. Specifically, the request.method property tells you which HTTP verb was used by the client. You can then use a simple if/else if or switch statement to route the logic for each method.
import { serve } from 'https://deno.land/std@0.177.0/http/server.ts'
serve({
async fetch(request: Request) {
const url = new URL(request.url);
const pathname = url.pathname;
if (request.method === 'GET') {
// Handle GET requests
return new Response(JSON.stringify({ message: 'This is a GET request!' }), {
headers: { 'Content-Type': 'application/json' },
});
} else if (request.method === 'POST') {
// Handle POST requests
const body = await request.json();
return new Response(JSON.stringify({ message: 'Received POST data', data: body }), {
headers: { 'Content-Type': 'application/json' },
});
} else if (request.method === 'PUT') {
// Handle PUT requests
return new Response(JSON.stringify({ message: 'This is a PUT request!' }), {
headers: { 'Content-Type': 'application/json' },
});
} else if (request.method === 'DELETE') {
// Handle DELETE requests
return new Response(JSON.stringify({ message: 'This is a DELETE request!' }), {
headers: { 'Content-Type': 'application/json' },
});
} else {
// Handle other methods or return an error
return new Response(JSON.stringify({ message: 'Method not allowed' }), {
status: 405,
headers: { 'Content-Type': 'application/json' },
});
}
},
})The example above demonstrates how to check request.method and return different responses for GET, POST, PUT, and DELETE. For POST requests, we also show how to parse the request body using await request.json() to access any data sent by the client.
It's good practice to also handle unsupported HTTP methods by returning a 405 Method Not Allowed status code. This provides clear feedback to the client about which operations are permissible.
graph TD;
A[Incoming Request] --> B{Check HTTP Method};
B -- GET --> C[Handle GET Logic];
B -- POST --> D[Handle POST Logic];
B -- PUT --> E[Handle PUT Logic];
B -- DELETE --> F[Handle DELETE Logic];
B -- Other --> G[Respond with 405];
C --> H[Send GET Response];
D --> I[Send POST Response];
E --> J[Send PUT Response];
F --> K[Send DELETE Response];
G --> L[Send 405 Response];
You can further refine your API by routing based on the URL path in addition to the HTTP method. For instance, you might have different logic for /users (GET, POST) versus /users/:id (GET, PUT, DELETE). This allows you to build a comprehensive API endpoint within a single Supabase Function.