/supabase-to-loops

✨ Trigger.dev Supabase Webhooks to Loops.so

Primary LanguageTypeScriptMIT LicenseMIT

✨ Trigger.dev Supabase to Loops.so

This repo uses Supabase Webhooks with the generic webhookEvent trigger to send updates whenever a user record is created in a Supabase table to Loops.so, the Loops.so API and our generic fetch function.

import { secureString, Trigger, webhookEvent } from "@trigger.dev/sdk";

new Trigger({
  // Give your Trigger a stable ID
  id: "supabase-to-loops",
  name: "Supabase to Loops.so",
  // Trigger on a webhook event, see https://docs.trigger.dev/triggers/webhooks
  on: webhookEvent({
    service: "supabase", // this is arbitrary, you can set it to whatever you want
    eventName: "user.inserted", // this is arbitrary, you can set it to whatever you want
    filter: {
      type: ["INSERT"], // only trigger on INSERT events
      table: ["users"], // only trigger on the users table
    },
  }),
  // The payload of the webhook is passed as the event argument
  async run(event, ctx) {
    if (!process.env.LOOPS_API_KEY) {
      throw new Error("Missing LOOPS_API_KEY environment variable");
    }

    await ctx.fetch(
      "Send to Loops",
      "https://app.loops.so/api/v1/contacts/create",
      {
        method: "POST",
        headers: {
          Accept: "application/json",
          Authorization: secureString`Bearer ${process.env.LOOPS_API_KEY}`,
        },
        body: {
          email: event.record.email,
          createdAt: event.record.created_at,
          firstName: event.record.first_name,
          lastName: event.record.last_name,
          userId: String(event.record.id),
        },
      }
    );
  },
}).listen();

🔧 Install

You can easily create a new project interactively based on this template by running:

npx create-trigger@latest supabase-to-loops
# or
yarn create trigger supabase-to-loops
# or
pnpm create trigger@latest supabase-to-loops

Follow the instructions in the CLI to get up and running locally in <30s.

🔁 Get your Loops.so API Key

Follow the instructions in the Loops.so API docs for how to get your API Key.

Once you have the API Key from Loops, set the LOOPS_API_KEY env variable in the .env file at the root of this project:

LOOPS_API_KEY="your Loops api key here"