Webhooks

Mantle provides a range of webhooks to deliver notifications about important activity within your app. From plan changes to customer events, webhooks help you keep your systems in sync with what's happening in Mantle.

Setting up webhooks

To subscribe to a webhook:

  1. Navigate to your app in Mantle's left navigation
  2. Go to Settings in the sidebar
  3. Select API keys
  4. Click Add webhook
  5. Choose your webhook and enter your endpoint URL

Available webhooks

Subscription plan events

Webhook EventDescription
plans/createTriggered when a new subscription plan is created.
plans/updateTriggered when an existing subscription plan is modified.

Subscription lifecycle events

Webhook EventDescription
subscriptions/activateTriggered when a customer activates a subscription.
subscriptions/cancelTriggered when a customer cancels their subscription.
subscriptions/upgradeTriggered when a customer upgrades to a higher-tier plan.
subscriptions/downgradeTriggered when a customer downgrades to a lower-tier plan.
subscriptions/billing_cycle_startedTriggered when a new billing cycle begins.
subscriptions/usage_approaching_capTriggered when usage approaches the capped limit.

Customer events

Webhook EventDescription
customers/installedTriggered when a customer installs your application.
customers/uninstalledTriggered when a customer uninstalls your application.
customers/reinstalledTriggered when a customer reinstalls your application.
customers/deactivatedTriggered when a customer's subscription is deactivated.
customers/reactivatedTriggered when a deactivated subscription is reactivated.
customers/trial_expiredTriggered when a customer's trial period ends without conversion.
customers/first_identifyTriggered when a customer is first identified.
customers/features_updatedTriggered when subscription features are updated.
customers/trial_extendedTriggered when a trial period is extended.
custom_fields/updatedTriggered when a custom field value is updated.
one_time_charges/activateTriggered when a one-time charge is activated.

Verifying webhooks

All webhooks are signed using HMAC SHA256 in the X-Mantle-Hmac-SHA256 header. The signing data consists of the X-Timestamp header concatenated with the stringified JSON payload: timestamp.payload

The secret will be the api key for app-specific webhooks, or the secret for notification webhooks.

Example

const crypto = require('crypto');

const verifySignature = (secret, data, expectedSignature) => {
  const hmac = crypto.createHmac('sha256', secret);
  hmac.update(data, 'utf8');
  const calculatedSignature = hmac.digest('hex');
  return crypto.timingSafeEqual(
    Buffer.from(calculatedSignature),
    Buffer.from(expectedSignature)
  );
}

const secret = // api key or secret
const timestamp = // X-Timestamp header
const expectedSignature = // X-Mantle-Hmac-SHA256 header
const body = // raw body of the webhook

const data = `${timestamp}.${body}`;
const isValid = verifySignature(secret, data, expectedSignature);
function verifySignature($secret, $data, $expectedSignature) {
    $calculatedSignature = hash_hmac('sha256', $data, $secret);
    return hash_equals($calculatedSignature, $expectedSignature);
}

$secret = // api key or secret
$timestamp = // X-Timestamp header
$expectedSignature = // X-Mantle-Hmac-SHA256 header
$body = // raw body of the webhook

$data = $timeStamp . "." . $body;
$isValid = verifySignature($secret, $data, $expectedSignature);

Did this page help you?