To handle Stripe billing webhooks in a Supabase Edge Function, use the billingWebhooksWrapper and stripeWebhookHandler utilities from the Basejump library.
- Initialize a
Stripe client using your STRIPE_API_KEY. When running in Deno (Supabase Edge Functions), you must use Stripe.createFetchHttpClient() to ensure compatibility with the Fetch API. - Create a
stripeResponse by calling stripeWebhookHandler with your stripeClient and your STRIPE_WEBHOOK_SIGNING_SECRET. - Wrap that response using
billingWebhooksWrapper to create the final webhookEndpoint. - Serve the
webhookEndpoint using the Deno serve function.
Required Environment Variables:
STRIPE_API_KEY: Your Stripe secret API key.STRIPE_WEBHOOK_SIGNING_SECRET: The signing secret provided by Stripe for your webhook endpoint.
import {serve} from "https://deno.land/std@0.168.0/http/server.ts";
import {billingWebhooksWrapper, stripeWebhookHandler} from "https://deno.land/x/basejump@v2.0.3/billing-functions/mod.ts";
import Stripe from "https://esm.sh/stripe@11.1.0?target=deno";
const stripeClient = new Stripe(Deno.env.get("STRIPE_API_KEY") as string, {
apiVersion: "2022-11-15",
httpClient: Stripe.createFetchHttpClient(),
});
const stripeResponse = stripeWebhookHandler({
stripeClient,
stripeWebhookSigningSecret: Deno.env.get("STRIPE_WEBHOOK_SIGNING_SECRET") as string,
});
const webhookEndpoint = billingWebhooksWrapper(stripeResponse);
serve(async (req) => {
const response = await webhookEndpoint(req);
return response;
});