Create a new Supabase project and create a new Edge Function.
Make sure to run the migrations, either by executing them manually, adding them into your CI, or running this locally once:
import { runMigrations } from '@supabase/stripe-sync-engine'
;(async () => {
await runMigrations({
databaseUrl: 'postgresql://postgres:..@db.<ref>.supabase.co:5432/postgre',
schema: 'stripe',
logger: console,
})
})()Sample code:
// Setup type definitions for built-in Supabase Runtime APIs
import 'jsr:@supabase/functions-js/edge-runtime.d.ts'
import { StripeSync } from 'npm:@supabase/stripe-sync-engine@0.37.2'
// Load secrets from environment variables
const stripeWebhookSecret = Deno.env.get('STRIPE_WEBHOOK_SECRET')!
const stripeSecretKey = Deno.env.get('STRIPE_SECRET_KEY')!
// Initialize StripeSync
const stripeSync = new StripeSync({
poolConfig: {
connectionString: Deno.env.get('DATABASE_URL')!,
max: 20,
keepAlive: true,
// optional SSL configuration
ssl: {
ca: Buffer.from(Deno.env.get('DATABASE_SSL_CA')!).toString('utf-8'),
},
},
stripeWebhookSecret,
stripeSecretKey,
backfillRelatedEntities: false,
autoExpandLists: true,
maxPostgresConnections: 5,
})
Deno.serve(async (req) => {
// Extract raw body as Uint8Array (buffer)
const rawBody = new Uint8Array(await req.arrayBuffer())
const stripeSignature = req.headers.get('stripe-signature')
await stripeSync.processWebhook(rawBody, stripeSignature)
return new Response(null, {
status: 202,
headers: { 'Content-Type': 'application/json' },
})
})Deploy your Edge Function initially.
Set up a Stripe webhook with the newly deployed Supabase Edge Function URL.
Create a new .env file in the supabase directory.
DATABASE_URL="postgresql://postgres:..@db.<ref>.supabase.co:5432/postgres"
DATABASE_SSL_CA="<base64-encoded-ca>"
STRIPE_WEBHOOK_SECRET="whsec_"
STRIPE_SECRET_KEY="sk_test_..."Load the secrets:
supabase secrets set --env-file ./supabase/.envNote: Replace
<base64-encoded-ca>with your actual base64-encoded certificate.
To generate a base64-encoded CA certificate, follow these steps:
-
Obtain the CA certificate file (e.g.,
prod-ca-2021.crt). -
Use the following command on Unix-based systems:
base64 -i prod-ca-2021.crt -o CA.base64
-
Open the
CA.base64file and copy its contents. -
Use the base64 string in your configuration or environment variables.