Webhook Notifications

Overview

Webhooks allow you to receive real-time notifications when events occur in your Unter account, such as successful payments or payment failures. Instead of constantly polling our API for updates, webhooks push notifications directly to your server when something important happens.

How Webhooks Work

  1. Configure your webhook URL - Tell Unter where to send notifications

  2. Event occurs - A payment succeeds, fails, etc.

  3. Unter sends HTTP POST - We POST event data to your URL

  4. Your server responds - Return 200 OK to acknowledge receipt

  5. Retry if needed - We retry failed deliveries with exponential backoff

Webhook Events

payment.succeeded

Fired when a payment is successfully confirmed on the blockchain.

Event Type: payment.succeeded

Payload:

{
  "id": "evt_1a2b3c4d5e6f7g8h",
  "type": "payment.succeeded",
  "created": 1690876543,
  "data": {
    "object": {
      "id": "pay_9z8y7x6w5v4u3t2s",
      "amount": {
        "raw": "5000000",
        "formatted": "5.00",
        "decimals": 6
      },
      "currency": "USDC",
      "status": "succeeded",
      "reference": "order_12345",
      "source_chain": "Ethereum",
      "destination_chain": "Polygon",
      "source_tx_hash": "0xabcd1234...",
      "destination_tx_hash": "0xefgh5678...",
      "payer_address": "0x742C4B8C2515Ad1D8e1C71e8e24c8D5F9A1a8B5A",
      "created_at": "2024-08-14T13:45:00+00:00",
      "completed_at": "2024-08-14T13:47:00+00:00"
    }
  }
}

payment.failed

Fired when a payment fails to process or confirm.

Event Type: payment.failed

Payload:

Webhook Security

Unter signs all webhook payloads using HMAC-SHA256 with your webhook secret. This ensures the webhook is actually from Unter and hasn't been tampered with.

Getting Your Webhook Secret: You can find your webhook secret in the Unter merchant portal under the webhook settings section. The secret is automatically generated when you first configure your webhook URL.

Signature Verification

Each webhook includes a Unter-Signature header with the following format:

Where:

  • t = Unix timestamp when the signature was generated

  • v1 = HMAC-SHA256 hex digest of the signed payload

Verifying Signatures

Step 1: Extract timestamp and signature

Step 2: Create signed payload Concatenate the timestamp, a period, and the raw request body:

Step 3: Generate expected signature

Step 4: Compare signatures Use constant-time comparison to prevent timing attacks:

Step 5: Check timestamp tolerance Reject webhooks older than 5 minutes (300 seconds) to prevent replay attacks:

Retry Logic

Unter automatically retries failed webhook deliveries using exponential backoff:

  • Attempt 1: Immediately

  • Attempt 2: After 1 minute

  • Attempt 3: After 3 minutes

  • Attempt 4: After 5 minutes

  • Attempt 5: After 10 minutes

  • Attempt 6: After 30 minutes

  • Attempt 7: After 2 hours

A webhook is considered failed if:

  • Your server doesn't respond within 10 seconds

  • Your server returns a non-2xx HTTP status code

  • Network connection fails

After 7 failed attempts, we stop retrying and mark the webhook as permanently failed.

Implementation Examples

JavaScript (Node.js with Express)

Best Practices

1. Always Verify Signatures

Never process webhooks without verifying the signature. This prevents malicious actors from sending fake webhooks to your server.

2. Handle Idempotency

The same webhook may be delivered multiple times. Use the event ID to ensure you only process each event once:

3. Respond Quickly

Respond with 200 OK as quickly as possible. Do heavy processing (database updates, email sending) asynchronously:

4. Handle Failures Gracefully

Your webhook endpoint should be fault-tolerant and handle errors gracefully:

Common Issues

1. Signature Verification Failures

Causes:

  • Wrong webhook secret

  • Modifying request body before verification

  • Clock synchronization issues

Solutions:

  • Verify you're using the correct secret from the webhook settings

  • Verify signature before parsing/modifying the request body

  • Ensure server time is synchronized with NTP

2. Webhook Timeouts

Causes:

  • Slow database queries in webhook handler

  • Synchronous external API calls

  • Heavy processing in webhook handler

Solutions:

  • Respond with 200 OK immediately

  • Process webhooks asynchronously

  • Optimize database queries

3. Duplicate Processing

Causes:

  • Not implementing idempotency

  • Multiple server instances processing same webhook

Solutions:

  • Use event ID to track processed webhooks

  • Implement proper database locking/transactions

Security Considerations

  1. Always verify signatures - Never trust unverified webhooks

  2. Validate timestamp - Reject old webhooks to prevent replay attacks

Last updated