Webhooks API Reference

Receive real-time notifications about anomalies, alerts, and system events

Real-time Event Notifications

Webhooks allow your application to receive real-time notifications when events occur in your Perimi account. Instead of polling our API, webhooks push data to your application as soon as events happen.

Real-time delivery
Automatic retries
Secure signatures

Webhook Events

anomaly.detected

Triggered when an anomaly is detected in a video stream

Example Payload:

{
  "event": "anomaly.detected",
  "timestamp": "2024-09-14T15:30:00Z",
  "stream_id": "stream_123",
  "anomaly": {
    "id": "anomaly_456",
    "type": "motion_detection",
    "confidence": 0.92,
    "bounding_box": {
      "x": 150, "y": 200,
      "width": 100, "height": 120
    },
    "image_url": "https://storage.perimi.ai/anomalies/456.jpg"
  }
}

stream.status_changed

Triggered when a stream goes online, offline, or encounters an error

Example Payload:

{
  "event": "stream.status_changed",
  "timestamp": "2024-09-14T15:30:00Z",
  "stream_id": "stream_123",
  "status": {
    "previous": "online",
    "current": "offline",
    "reason": "connection_timeout",
    "error_message": "RTSP timeout after 30 seconds"
  }
}

alert.triggered

Triggered when an alert rule matches detected activity

Example Payload:

{
  "event": "alert.triggered",
  "timestamp": "2024-09-14T15:30:00Z",
  "stream_id": "stream_123",
  "alert": {
    "id": "alert_789",
    "rule_name": "After Hours Motion",
    "severity": "high",
    "zone": "entrance",
    "anomaly_count": 3
  }
}

Setup Guide

1

Configure Webhook URL

Set up your endpoint to receive webhook notifications

curl -X POST "https://api.perimi.ai/v1/webhooks" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "url": "https://your-app.com/webhook",
    "events": ["anomaly.detected", "stream.status_changed"],
    "secret": "your_webhook_secret"
  }'
2

Verify Webhook Signature

Validate incoming webhook requests for security

// Example verification (Node.js)
const crypto = require('crypto');

function verifyWebhook(payload, signature, secret) {
  const expectedSignature = crypto
    .createHmac('sha256', secret)
    .update(payload, 'utf8')
    .digest('hex');

  return signature === 'sha256=' + expectedSignature;
}
3

Handle Webhook Events

Process different event types in your application

// Example webhook handler (Node.js/Express)
app.post('/webhook', (req, res) => {
  const event = req.body;

  switch(event.event) {
    case 'anomaly.detected':
      handleAnomalyDetected(event);
      break;
    case 'stream.status_changed':
      handleStreamStatusChanged(event);
      break;
    case 'alert.triggered':
      handleAlertTriggered(event);
      break;
  }

  res.status(200).send('OK');
});

Best Practices

Endpoint Requirements

Must respond with HTTP 2xx status code
Should respond within 10 seconds
Must accept POST requests with JSON payload
Should use HTTPS for security

Error Handling

Implement exponential backoff for retries
Log webhook failures for debugging
Return appropriate HTTP status codes
Handle duplicate events gracefully

Security

Always verify webhook signatures
Use HTTPS endpoints only
Implement rate limiting
Validate payload structure

Reliability

Make webhook processing idempotent
Store webhook events for replay if needed
Monitor webhook delivery success rates
Set up alerting for failed deliveries

Testing & Debugging

Tips for testing webhook integration and troubleshooting issues

Testing Tools

ngrok

Expose local development server for testing

RequestBin

Capture and inspect webhook payloads

Test Webhooks

Use API to trigger test events

Common Issues

Timeout errors

Endpoint must respond within 10 seconds

SSL certificate issues

Ensure valid HTTPS certificate

Signature verification fails

Check webhook secret and signature calculation

Ready to Set Up Webhooks?

Start receiving real-time notifications in your application

Perim - AI-Powered Anomaly Detection for Critical Infrastructure