Best Practices

circle-check

1. Endpoint requirements

  • HTTPS Only: Webhook endpoints must use HTTPS for security

  • Response Time: Respond within 5 seconds to avoid timeouts

  • Status Codes: Return 2xx status codes to acknowledge receipt

  • Error Handling: Return 4xx/5xx status codes to trigger retry mechanism

2. Idempotency

Webhooks may be resent if no acknowledgment is received. Implement idempotency using:

// Use webhookId to prevent duplicate processing
const processedWebhooks = new Set();

app.post('/webhook', (req, res) => {
  const {webhookId} = req.body;
  
  if (processedWebhooks.has(webhookId)) {
    return res.status(200).send('Already processed');
  }
  
  // Process webhook...
  processedWebhooks.add(webhookId);
  res.status(200).send('OK');
});

3. Async processing

For long-running processes, respond immediately and queue for background processing:

4. Error monitoring

Implement proper logging and monitoring:

5. Testing

Test your webhook endpoints thoroughly:

Last updated