Skip to content

Webhooks

Igor Balos edited this page Jul 31, 2023 · 6 revisions

For these API requests you will need to use a server API token. Once you obtain it, you will need to use server API client.

let postmark = require("postmark")
const serverToken = "xxxx-xxxxx-xxxx-xxxxx-xxxxxx"
let client = new postmark.ServerClient(serverToken);

Get list of available webhooks

Get the list of all available webhooks with triggers enabled on them and other details.

client.getWebhooks().then(result => {
    console.log(result);
    console.log(result.Webhooks[0].ID);
    console.log(result.Webhooks[0].Url);
    console.log(result.Webhooks[0].Triggers.Bounce.Enabled);
    console.log(result.Webhooks[0].Triggers.Delivery.Enabled);
    console.log(result.Webhooks[0].Triggers.Open.Enabled);
});

Get webhook by ID

client.getWebhook(12345).then(result => {
    console.log(result.ID);
    console.log(result.Url);
});

Create a new webhook

client.createWebhook({
    Url: 'https://example.com',
    HttpAuth: {
        Username: 'test',
        Password: 'test'
    },
    Triggers: {
        Open: {
            Enabled: true,
            PostFirstOpenOnly: true
        },
        Click: {
            Enabled: true
        },
        SpamComplaint: {
            Enabled: true
        }
    }
}).then(result => {
    console.log(result);
});

If MessageStream field is provided, webhook will be created on specific stream. By default webhook is created on default Transactioal stream.

When retrieving webhooks, in case you want to use types, check out the following article.

Update webhook by ID

client.editWebhook(12345, {
    Url: 'https://tester.com',
    HttpAuth: { Username: 'test', Password: 'test'},
    Triggers: { Open: { Enabled: true }
}}).then(result => {
    console.log(result);
});

Delete webhook by ID

client.deleteWebhook(123456).then(result => {
    console.log(result.Message);
});