Webhooks — HTTP Event Callbacks & Delivery

Webhooks push HTTP notifications to external services when events occur. Link to mock API endpoints, set custom payloads, and test-fire from the dashboard.

// Slack Notification Webhook

Create this webhook in the dashboard. Get your Slack webhook URL fromapi.slack.com/apps → Incoming Webhooks.

create-slack-webhook.shbash
curl -X POST https://moqapi.dev/api/webhooks \
  -H "Content-Type: application/json" \
  -d '{
    "name": "New Order Alert",
    "target_url": "https://hooks.slack.com/services/T00/B00/xxxxxxxxxxxx",
    "method": "POST",
    "headers": { "Content-Type": "application/json" },
    "payload_template": {
      "blocks": [
        {
          "type": "header",
          "text": { "type": "plain_text", "text": "🛒 New Order Received" }
        },
        {
          "type": "section",
          "fields": [
            { "type": "mrkdwn", "text": "*Customer:*\nAlice Johnson" },
            { "type": "mrkdwn", "text": "*Amount:*\n$49.99" },
            { "type": "mrkdwn", "text": "*Status:*\nPending" },
            { "type": "mrkdwn", "text": "*Time:*\n2024-06-01 14:30" }
          ]
        }
      ]
    }
  }'

Test fire it:

terminalbash
curl -X POST https://moqapi.dev/api/webhooks/<webhook-id>/test

// Discord Notification Webhook

Get your Discord webhook URL from Server Settings → Integrations → Webhooks.

discord-webhook-payload.jsonjson
{
  "name": "Deploy Alert",
  "target_url": "https://discord.com/api/webhooks/1234567890/abcdef...",
  "method": "POST",
  "headers": { "Content-Type": "application/json" },
  "payload_template": {
    "username": "moqapi Bot",
    "avatar_url": "https://moqapi.dev/icon.png",
    "embeds": [
      {
        "title": "✅ Deployment Successful",
        "description": "Function **process-orders** was published.",
        "color": 5763719,
        "fields": [
          { "name": "Environment", "value": "Production", "inline": true },
          { "name": "Version", "value": "v2.3.1", "inline": true }
        ],
        "timestamp": "2024-06-01T14:30:00.000Z"
      }
    ]
  }
}

// Custom API Callback

Push data to any REST API. Link to a mock API endpoint to auto-include the latest resource data in the payload.

custom-webhook.jsonjson
{
  "name": "Sync to CRM",
  "target_url": "https://api.yourcrm.com/v1/contacts/sync",
  "method": "POST",
  "headers": {
    "Content-Type": "application/json",
    "Authorization": "Bearer sk_live_your_api_key_here",
    "X-Source": "moqapi"
  },
  "payload_template": {
    "source": "moqapi",
    "event": "user.created",
    "timestamp": "2024-06-01T14:30:00Z",
    "data": {
      "name": "Alice Johnson",
      "email": "alice@example.com",
      "plan": "pro"
    }
  }
}

// Receive Webhooks with a Function

Create a function that processes incoming webhooks. Connect it to a POST route in the API Gateway.

webhook-receiver.jstypescript
// Function to receive and process incoming webhooks
// Bind to: POST /webhooks/receive in API Gateway

export const handler = async (event) => {
  const { headers, body } = event;
  
  // Verify webhook source (optional)
  const secret = headers['x-webhook-secret'];
  if (secret !== process.env.WEBHOOK_SECRET) {
    return { statusCode: 401, body: { error: 'Invalid secret' } };
  }

  const payload = typeof body === 'string' ? JSON.parse(body) : body;

  // Route by event type
  switch (payload.event) {
    case 'order.created':
      console.log('New order:', payload.data.id, payload.data.total);
      // Process order...
      return { statusCode: 200, body: { processed: 'order.created' } };

    case 'user.signup':
      console.log('New user:', payload.data.email);
      // Send welcome email, create account, etc.
      return { statusCode: 200, body: { processed: 'user.signup' } };

    case 'payment.failed':
      console.log('Payment failed:', payload.data.invoice_id);
      // Notify support team...
      return { statusCode: 200, body: { processed: 'payment.failed' } };

    default:
      return { statusCode: 200, body: { received: true, event: payload.event } };
  }
};

Test payload:

test-webhook-payload.jsonjson
{
  "headers": { "x-webhook-secret": "whsec_test123" },
  "body": {
    "event": "order.created",
    "data": {
      "id": "ord_abc123",
      "total": 99.99,
      "customer": "alice@example.com",
      "items": [
        { "name": "Widget Pro", "qty": 2, "price": 49.99 }
      ]
    }
  }
}

// Scheduled Webhooks (Cron)

Add a cron expression to trigger webhooks automatically:

schedule-examples.txtplaintext
* * * * *      Every minute (health check ping)
*/5 * * * *    Every 5 minutes (status sync)
0 * * * *      Every hour (data refresh)
0 9 * * 1-5    Weekdays at 9 AM (daily report)
0 0 * * 0      Sunday midnight (weekly cleanup)

// API Reference

GET/api/webhooksList all webhooks
POST/api/webhooksCreate webhook
PUT/api/webhooks/:idUpdate webhook
DELETE/api/webhooks/:idDelete webhook
POST/api/webhooks/:id/testTest fire webhook