Logo

Threshold Email

OpenMeter Notifications enables you to send usage threshold emails to your team and customers.

Configuring Notification

To send usage threshold email notifications, you need to setup Notifications. Check the Notifications guide to learn how to set up Notifications.

Configure your webhook on the OpenMeter Cloud UI with the desired interval and query period. For example, to send daily usage threshold email notifications about current month usage, you can set the interval to daily and period to CURRENT_MONTH.

Sending Threshold Email

To send out emails when a customer reaches the defined threshold, we are listening on the OpenMeter Notificaiton webhooks. When we receive the webhook we use Resend to send out email notifications. Resend is a transactional email service that allows you to send emails using a simple API and React.

Let's see the following Next.js example that consumes webhooks and sends out emails.

import { Resend } from 'resend';
import { EmailTemplate } from '../../components/EmailTemplate';
 
const resend = new Resend(process.env.RESEND_API_KEY);
 
// Listening on the webhook endpoint
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  if (req.method !== "POST") {
    res.status(405).json({});
    return;
  }
  const msg = (await buffer(req)).toString();
 
  // See webhooks docs about how to verify signatures
  if (msg.type === 'entitlements.balance.threshold') {
    const emailTo = getEmailBySubject(msg.data.subject.key)
    const threshold = msg.data.threshold.value
    const featureName = msg.data.feature.name
    const data = await resend.emails.send({
      from: 'Acme <[email protected]>',
      to: [emailTo],
      subject: `You reached ${threshold}% of your ${featureName} limit`,
      react: EmailTemplate({
        feature: msg.data.feature,
        threshold: msg.data.threshold,
        entitlement: msg.data.entitlement,
      }),
    });
 
    res.status(200).json(data);
  }
 
  res.status(400).json({ message: "Unknown event type" });
  return;
}