Skip to content

Setting up Webhooks

Vlad-Cosmin Sandu edited this page Jan 9, 2020 · 2 revisions

The PostmarkClient allows you to manage webhooks for a specific Server and Message Stream, allowing Postmark to be able to immediately notify you when an event occurs.

Required Version: >=2.9.0

Create a Webhook Configuration:

use Postmark\PostmarkClient;
use Postmark\Models\Webhooks\WebhookConfigurationTriggers;
use Postmark\Models\Webhooks\WebhookConfigurationOpenTrigger;
use Postmark\Models\Webhooks\WebhookConfigurationClickTrigger;
use Postmark\Models\Webhooks\WebhookConfigurationDeliveryTrigger;
use Postmark\Models\Webhooks\WebhookConfigurationBounceTrigger;
use Postmark\Models\Webhooks\WebhookConfigurationSpamComplaintTrigger;
use Postmark\Models\Webhooks\HttpAuth;

$client = new PostmarkClient("<server token>");

$openTrigger = new WebhookConfigurationOpenTrigger(true, true);
$clickTrigger = new WebhookConfigurationClickTrigger(true);
$deliveryTrigger = new WebhookConfigurationDeliveryTrigger(true);
$bounceTrigger = new WebhookConfigurationBounceTrigger(true, true);
$spamComplaintTrigger = new WebhookConfigurationSpamComplaintTrigger(true, true);

// All triggers are optional (can be passed as null). You can choose to import and use only the ones you wish.
$triggers = new WebhookConfigurationTriggers($openTrigger, $clickTrigger, $deliveryTrigger, $bounceTrigger, $spamComplaintTrigger);

$url = "<webhook URL>";			
$httpAuth = new HttpAuth("testUser", "testPass");
$headers = [ "X-CUSTOM-HEADER" => "Header content"];

// If the MessageStream is not provided, the default transactional stream will be used ("outbound").
$messageStream = "outbound";

// Only the Webhook URL is required. Other parameters are optional.
$createdWebhook = $client->createWebhookConfiguration($url, $messageStream, $httpAuth, $headers, $triggers);

Edit a Webhook Configuration:

$client = new PostmarkClient("<server token>");

// We are only enabling the Open trigger. Other triggers will not be modified.
$openTrigger = new WebhookConfigurationOpenTrigger(true);
$newTriggers = new WebhookConfigurationTriggers($openTrigger);

$webhookId = 123;
$newUrl = "<modified webhook URL>";
$httpAuth = new HttpAuth("modifiedUser", "modifiedPass");
$headers = [ "X-Customer-HEADER" => "Modified content"];

// Any parameters passed as NULL will be ignored (their existing values will not be modified).
$editedWebhook = $client->editWebhookConfiguration($webhookId, $newUrl, $httpAuth, $headers, $newTriggers);

Get a Single Webhook Configuration:

$client = new PostmarkClient("<server token>");

$webhookId = 123;
$configuration = $client->getWebhookConfiguration($webhookId);

Get a List of Webhook Configurations:

$client = new PostmarkClient("<server token>");

// If the MessageStream is passed as NULL, configurations for all streams on the server will be returned.
$messageStream = "outbound";

$configurations = $client->getWebhookConfigurations($messageStream);

Delete a Webhook Configuration:

$client = new PostmarkClient("<server token>");

$webhookId = 123;
$response = $client->deleteWebhookConfiguration($webhookId);