Skip to content

Instantly share code, notes, and snippets.

@DreamingInBinary
Created August 21, 2025 16:51
Show Gist options
  • Select an option

  • Save DreamingInBinary/9322389a0828f669a9253c052a8fa35b to your computer and use it in GitHub Desktop.

Select an option

Save DreamingInBinary/9322389a0828f669a9253c052a8fa35b to your computer and use it in GitHub Desktop.

Revisions

  1. DreamingInBinary created this gist Aug 21, 2025.
    75 changes: 75 additions & 0 deletions superwallWebhook.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    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
    });
    }
    });