Skip to content

Commit e0e9fc9

Browse files
authored
feat: edge function docs (#150)
1 parent 0ab5e71 commit e0e9fc9

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ Each package has its own README with installation, configuration, and usage inst
4040

4141
---
4242

43+
## Supabase Edge Function
44+
45+
To deploy the sync-engine to a Supabase edge function, follow this [guide](./edge-function.md).
46+
4347
## Webhook Support
4448

4549
- [ ] `balance.available`

edge-function.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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

Comments
 (0)