Webhooks

Real-time event notifications for your applications

Setting Up Webhooks

curl -X POST "https://rebusai.com/api/v1/affiliate/webhooks/" \
  -H "Authorization: Bearer sk_live_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://your-app.com/webhooks/affiliate",
    "events": ["commission.approved", "payout.processed", "affiliate.approved"],
    "secret": "your_webhook_secret",
    "active": true
  }'

Webhook Events

Event Description Trigger
affiliate.created New affiliate registered Affiliate signs up
affiliate.approved Affiliate application approved Admin approves affiliate
affiliate.rejected Affiliate application rejected Admin rejects affiliate
commission.created New commission recorded Sale attributed to affiliate
commission.approved Commission approved for payout Admin approves commission
commission.rejected Commission rejected Admin rejects commission
payout.created Payout batch created Payout batch generated
payout.processed Payout successfully processed Payment sent to affiliate
payout.failed Payout failed Payment failed
link.clicked Affiliate link clicked Customer clicks link
conversion.recorded Sale conversion recorded Customer makes purchase
code.redeemed Affiliate code redeemed Customer redeems code

Webhook Payload Example

{
  "event": "commission.approved",
  "timestamp": 1640995200,
  "data": {
    "commission_id": 123,
    "affiliate_id": 456,
    "affiliate_name": "John Smith",
    "amount": 85.50,
    "commission_rate": 15.0,
    "order_id": 789,
    "product_name": "Advanced Course",
    "approved_by": "admin@rebusai.com",
    "approved_date": "2024-01-20T14:30:00Z"
  },
  "signature": "sha256=1234567890abcdef..."
}

Webhook Verification

Verify webhook signatures to ensure authenticity:

import hmac
import hashlib

def verify_webhook(payload, signature, secret):
    expected_signature = hmac.new(
        secret.encode('utf-8'),
        payload.encode('utf-8'),
        hashlib.sha256
    ).hexdigest()
    
    received_signature = signature.replace('sha256=', '')
    return hmac.compare_digest(expected_signature, received_signature)

# Usage
payload = request.body  # Raw request body
signature = request.headers.get('X-Webhook-Signature')
secret = 'your_webhook_secret'

if verify_webhook(payload, signature, secret):
    # Process webhook
    webhook_data = json.loads(payload)
    handle_webhook_event(webhook_data)
else:
    # Invalid signature
    return 401