Created
August 21, 2025 16:51
-
-
Save DreamingInBinary/9322389a0828f669a9253c052a8fa35b to your computer and use it in GitHub Desktop.
Use an edge function from Supabase to ingest a Superwall Webhooks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { createClient } from 'npm:@supabase/supabase-js@2'; | |
| // Initialize client... | |
| const supabase = createClient(Deno.env.get('SUPABASE_URL') ?? '', Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''); | |
| Deno.serve(async (req)=>{ | |
| // Only accept POST requests | |
| if (req.method !== 'POST') { | |
| return new Response('Method Not Allowed', { | |
| status: 405 | |
| }); | |
| } | |
| try { | |
| // Parse the incoming webhook payload | |
| const payload = await req.json(); | |
| // Validate structure | |
| if (!payload.object || !payload.type || !payload.data) { | |
| return new Response(JSON.stringify({ | |
| error: 'Invalid payload structure' | |
| }), { | |
| status: 400, | |
| headers: { | |
| 'Content-Type': 'application/json' | |
| } | |
| }); | |
| } | |
| // Extract the webhook info | |
| const { type, projectId, data: { id, productId, originalAppUserId, price, proceeds, store, environment, purchasedAt } } = payload; | |
| // Log it out | |
| console.log('Superwall Webhook Received:', { | |
| eventType: type, | |
| projectId, | |
| productId, | |
| userId: originalAppUserId, | |
| price, | |
| store, | |
| environment | |
| }); | |
| // Optional: Here, we insert into a database in Supabase, but do whatever you need now... | |
| const { error: insertError } = await supabase.from('superwall_webhooks').insert({ | |
| event_type: type, | |
| project_id: projectId, | |
| product_id: productId, | |
| user_id: originalAppUserId, | |
| price, | |
| proceeds, | |
| store, | |
| environment, | |
| purchased_at: new Date(purchasedAt).toISOString(), | |
| raw_payload: payload | |
| }); | |
| if (insertError) { | |
| console.error('Database insertion error:', insertError); | |
| } | |
| // Return a success response | |
| return new Response(JSON.stringify({ | |
| status: 'success', | |
| message: 'Webhook processed successfully' | |
| }), { | |
| headers: { | |
| 'Content-Type': 'application/json' | |
| }, | |
| status: 200 | |
| }); | |
| } catch (error) { | |
| console.error('Superwall Webhook Error:', error); | |
| return new Response(JSON.stringify({ | |
| status: 'error', | |
| message: error.message | |
| }), { | |
| headers: { | |
| 'Content-Type': 'application/json' | |
| }, | |
| status: 500 | |
| }); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment