|
| 1 | +# Stripe-Sync inside Supabase Edge Function |
| 2 | + |
| 3 | +Create a new [Supabase](https://supabase.com) and create a new [Edge Function](https://supabase.com/docs/guides/functions/quickstart). |
| 4 | + |
| 5 | +Make sure to run the [migrations](./packages/sync-engine/src/database/migrations/), either by executing them manually, adding them into your CI or running this locally once: |
| 6 | + |
| 7 | +```ts |
| 8 | +import { runMigrations } from '@supabase/stripe-sync-engine' |
| 9 | +;(async () => { |
| 10 | + await runMigrations({ |
| 11 | + databaseUrl: 'postgresql://postgres:..@db.<ref>.supabase.co:5432/postgre', |
| 12 | + schema: 'stripe', |
| 13 | + logger: console, |
| 14 | + }) |
| 15 | +})() |
| 16 | +``` |
| 17 | + |
| 18 | +Sample code: |
| 19 | + |
| 20 | +```ts |
| 21 | +// Setup type definitions for built-in Supabase Runtime APIs |
| 22 | +import 'jsr:@supabase/functions-js/edge-runtime.d.ts' |
| 23 | +import { StripeSync } from 'npm:@supabase/[email protected]' |
| 24 | + |
| 25 | +// Load secrets from environment variables |
| 26 | +const databaseUrl = Deno.env.get('DATABASE_URL')! |
| 27 | +const stripeWebhookSecret = Deno.env.get('STRIPE_WEBHOOK_SECRET')! |
| 28 | +const stripeSecretKey = Deno.env.get('STRIPE_SECRET_KEY')! |
| 29 | + |
| 30 | +// Initialize StripeSync |
| 31 | +const stripeSync = new StripeSync({ |
| 32 | + databaseUrl, |
| 33 | + stripeWebhookSecret, |
| 34 | + stripeSecretKey, |
| 35 | + backfillRelatedEntities: false, |
| 36 | + autoExpandLists: true, |
| 37 | + maxPostgresConnections: 5, |
| 38 | +}) |
| 39 | + |
| 40 | +Deno.serve(async (req) => { |
| 41 | + // Extract raw body as Uint8Array (buffer) |
| 42 | + const rawBody = new Uint8Array(await req.arrayBuffer()) |
| 43 | + |
| 44 | + const stripeSignature = req.headers.get('stripe-signature') |
| 45 | + |
| 46 | + await stripeSync.processWebhook(rawBody, stripeSignature) |
| 47 | + |
| 48 | + return new Response(null, { |
| 49 | + status: 202, |
| 50 | + headers: { 'Content-Type': 'application/json' }, |
| 51 | + }) |
| 52 | +}) |
| 53 | +``` |
| 54 | + |
| 55 | +Deploy your Edge Function initially. |
| 56 | + |
| 57 | +Set up a Stripe webhook with the newly deployed Supabase Edge Function url. |
| 58 | + |
| 59 | +Create a new .env file in the `supabase` directory. |
| 60 | + |
| 61 | +```.env |
| 62 | +DATABASE_URL="postgresql://postgres:..@db.<ref>.supabase.co:5432/postgres" |
| 63 | +STRIPE_WEBHOOK_SECRET="whsec_" |
| 64 | +STRIPE_SECRET_KEY="sk_test_..." |
| 65 | +``` |
| 66 | + |
| 67 | +Load the secrets: |
| 68 | + |
| 69 | +`sh |
| 70 | +supabase secrets set --env-file ./supabase/.env |
| 71 | +`. |
0 commit comments