What are webhooks?

Postack uses webhooks to instantly notify you about events around SMS messages. We send these events as JSON payload to your registered HTTPS endpoint using a POST request. You can use these webhooks for various tasks such as:

  • Respond to customer messages.
  • Handle errors like undelivered messages.
  • Produce data reports and perform analytics.

Steps to receive webhooks

You can start receiving webhook events using the following steps:

  • Create a local endpoint to receive events.
  • Register your local endpoint with Postack.
  • Send a test event to your endpoint.
  • Deploy your endpoint to production.
  • Register your production endpoint with Postack.

1. Create a local endpoint to receive events

In your application, create a POST endpoint to receive events.

We will provide instructions on how to obtain POSTACK_WEBHOOK_SECRET in the following step.
// pages/api/webhooks.ts
import { Webhook } from "svix";
import { buffer } from "micro";
import { NextApiRequest, NextApiResponse } from "next";

export const config = {
    api: {
        bodyParser: false,
    },
};

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
    const payload = (await buffer(req)).toString();
    const headers = req.headers;

    const wh = new Webhook(process.env.POSTACK_WEBHOOK_SECRET);
    try {
        const msg = wh.verify(payload, headers);
    } catch (err) {
        res.status(400).json({});
    }

    // Do something with the message...
    res.json({});
}

You must return a 2xx (status code 200-299) response to let Postack know that you have successfully processed the webhook. Otherwise, Postack will try sending it again.

2. Register your local endpoint with Postack

You can register your publicly accessible HTTPs endpoint on the Postack dashboard.

You can use tools like ngrok to expose your local endpoint to internet. For example: https://6b17-172-98-156-152.ngrok-free.app/api/v1/webhooks

Once you have registered your endpoint, get the POSTACK_WEBHOOK_SECRET from the registered endpoint’s details page.

3. Send a test event to your endpoint

You can either send an outbound test SMS to a known number or send an inbound SMS to your Postack number. Your endpoint should receive a webhook event within a second.

4. Deploy your endpoint to production

Once you have tested your endpoint locally, you can deploy it to production.

5. Register your production endpoint with Postack

Register your production endpoint on the Postack dashboard. You can either edit the existing endpoint which you just registered above or register a new one. Please make sure to rotate the POSTACK_WEBHOOK_SECRET before using it in production.