Send emails from Lemon Squeezy Webhooks
Lemon Squeezy is one of the most popular merchant of records in the world. It allows you to accept payments and manage your business with ease. Lemon Squeezy's developer experience is also top-notch. With webhooks, you can automate a lot of your business. In this post, we'll teach you how to use Lemon Squeezy webhooks and Plunk to automatically send emails to your customers.
What is Plunk?
Plunk is the all-in-one email platform. It allows you to send transactional emails, marketing emails, and broadcasts from a single platform. Plunk contacts are shared across all of your emails. This means that you get a single view of your contacts through one single platform.
What are webhooks?
A webhook is a way for an application to communicate with another application. It's often used to notify other applications of important events. For example, Lemon Squeezy uses webhooks to notify your application when a new subscription is made or a payment fails.
We can use these webhooks to automatically send emails to your customers. For example, we can send an email to new customers when they sign up or an email to customers when they cancel their subscription.
Setting up a webhook in Lemon Squeezy
To set up a webhook in Lemon Squeezy, you'll need to go to the Lemon Squeezy webhook settings. From there, you can create a new webhook. Webhooks require a callback URL. This is the URL that Lemon Squeezy will send the events to. This means that you will either need to host your own server or use a service like Zapier to catch the events and process them.
Create a signing secret, this is used to verify that events are actually coming from Lemon Squeezy. Once you have your callback URL and signing secret, you can go ahead an select the events you want to receive.
In this example, we'll be using the subscription_created event to send an email to our customers when they start a new subscription.
Setting up Plunk
To send emails with Plunk, you'll need to create a Plunk project. You can do this by going to the Plunk dashboard.
To send the emails, we will be using the marketing actions, these are automatic emails that are sent when Plunk receives an event. When first creating a project, you will be asked to complete an onboarding that will guide you through the process of setting up your first marketing action.
Sending an event to Plunk
Marketing automations start from events. You can send events to Plunk using the Plunk API. To do this, you'll need to copy your API key in the Plunk dashboard. Once you have your API key, you can use it to send an event like this.
We've gone ahead and wrote a little Node.js example that will catch the webhook from Lemon Squeezy and send an event to Plunk.
const crypto = require('crypto');
const express = require('express');
const app = express();
const endpointSecret = "<WEBHOOK_SECRET>";
app.post('/webhook', express.raw({type: 'application/json'}), async (request, response) => {
const hmac = crypto.createHmac('sha256', secret);
const digest = Buffer.from(hmac.update(request.rawBody).digest('hex'), 'utf8');
const signature = Buffer.from(request.get('X-Signature') || '', 'utf8');
if (!crypto.timingSafeEqual(digest, signature)) {
throw new Error('Invalid signature.');
}
const {meta, data} = JSON.parse(request.body);
switch (meta.event_name) {
case 'subscription_created':
// Send an event to Plunk
await fetch('https://api.useplunk.com/v1/track', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer <API_KEY>',
},
body: JSON.stringify({
event: 'subscription.created',
email: data.attributes.customer_email,
})
});
break;
}
response.send();
});
The code above will send an event to Plunk when a new subscription is created through Lemon Squeezy. Now that we have the event in Plunk we can use it to set up our marketing action.
Creating a template
The next step of the onboarding will take you through the process of creating a template. Templates are blueprints for your emails. You can add dynamic content to templates and easily edit them to create different emails.
You can try out Plunk's email editor without creating an account. You can find the editor on the markdown to html page.
Creating a marketing action
With the event and template ready, we can create our marketing action. The action will be triggered when the event is received and will automatically send the email to the customer. Next to the event and template you can also optionally link more events or set up a delay.
That's it!
Now that we have everything set up, we can test it out. To test the webhook, you can go ahead and make an order for your product through Lemon Squeezy. You can even do this in test mode, no need to verify your account!
It's as easy as that. You can now use Plunk to send emails to your customers on any event. You can also use the webhook you created to keep your own database in sync with Lemon Squeezy!