Skip to content

OneSignalDevelopers/onesignal-supabase-sample-integration-api

Repository files navigation

OneSignal

Quickstart   •   Website   •   Docs   •   Examples

This is a Next.js project bootstrapped with create-next-app.

Supabase Sample Integration API

OneSignal makes engaging customers simple and is the fastest, most reliable service to send push notifications, in-app messages, SMS, and emails.

This repo contains the companion API to the Onesignal + Supabase Sample Integration guide.

This project exists solely so the sample integration works in both local and production environments. While the event-handler implemented in this project can be implemented Supabase, it may negatively impact your dev experience due to a constraint with Supabase hosted on Docker: it can only serve one edge function at a time. Hosting a seperate API is a simple work-around for the limitation.

How it works

  1. This API recieves successful Stripe webhook events
  2. If the event matched the correct intent, an Order record is inserted into the database
  3. A Supabase Database Webhook, defined in the project calls a Supabase Edge Function when the record is inserted, calls a Supabase Edge Function which sends a request to the OneSignal API to create a push notificaiton.

Function implementation

This enables us to Insert Order record before sending a message to the user. We may want to do this for a host of reason such as wanting to wait for shipping to process before alerting to wanting to make sure the payment is received before moving the customer to the next stage of their journey. Supabase Database Webhooks enable us to react to state changes from our source of truth: the database.

const payloadJson = JSON.stringify(req.body)
const header = Stripe.webhooks.generateTestHeaderString({
payload: payloadJson,
secret: StripeWebhookSecret,
})
const event = Stripe.webhooks.constructEvent(
payloadJson,
header,
StripeWebhookSecret
)
console.log("Stripe Event\n\t", event)
switch (event.type) {
case "payment_intent.succeeded":
const { id, customer, amount, currency } = req.body.data.object
const prismaResult = await Supabase.from("orders").insert({
stripe_pi_id: id,
stripe_customer_id: customer,
amount: amount,
currency: currency,
})
console.log({ prismaResult })
res.status(200).send({ message: "Ok" })
break
default:
res.status(400).send({ message: `Unhandled event type ${event.type}` })
}

🚦Getting Started

First, make a copy of .env.example and set each variable

Then, run the development server:

npm run dev
# or
yarn dev

Deploy on Vercel

The easiest way to deploy your Next.js app is to use the Vercel Platform from the creators of Next.js.

Check out our Next.js deployment documentation for more details.

❤️ Developer Community

For additional resources, please join the OneSignal Developer Community.

Get in touch with us or learn more about OneSignal through the channels below.

Show your support

Give a ⭐️ if this project helped you, and watch this repo to stay up to date.