With Supabase Edge Functions
Create a new Supabase project and create a new Edge Function.
Prepare your database
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,
})
})()
|
Usage
Sample code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39 | // 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/.env
|
Note:
Replace <base64-encoded-ca>
with your actual base64-encoded certificate.
Generating Base64 from CA 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.base64
file and copy its contents.
- Use the base64 string in your configuration or environment variables.