Memokit
API Reference

Webhooks

Receive real-time notifications for memory events

Webhooks

Webhooks allow you to receive real-time HTTP notifications when events occur in your Memokit account.

Overview

When you configure a webhook, Memokit will send HTTP POST requests to your specified URL whenever subscribed events occur. This enables real-time integrations without polling the API.

Webhook Events

EventDescription
memory.createdA new memory was created
memory.updatedA memory was updated
memory.deletedA memory was deleted

Webhook Payload

All webhook payloads follow this structure:

{
  "id": "wh_delivery_abc123",
  "event": "memory.created",
  "timestamp": "2024-01-15T10:30:00Z",
  "data": {
    "id": "mem_abc123",
    "content": "The user mentioned...",
    "userId": "user_123",
    "createdAt": "2024-01-15T10:30:00Z"
  }
}

Setting Up Webhooks

1. Create a Webhook Endpoint

First, create an endpoint on your server that can receive POST requests:

// Express.js example
app.post('/webhooks/memokit', (req, res) => {
  const signature = req.headers['x-memokit-signature'];
  const payload = req.body;

  // Verify signature
  if (!verifySignature(payload, signature, webhookSecret)) {
    return res.status(401).send('Invalid signature');
  }

  // Process the event
  switch (payload.event) {
    case 'memory.created':
      handleMemoryCreated(payload.data);
      break;
    case 'memory.updated':
      handleMemoryUpdated(payload.data);
      break;
    case 'memory.deleted':
      handleMemoryDeleted(payload.data);
      break;
  }

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

2. Register the Webhook

In the Memokit Console:

  1. Go to Webhooks in the sidebar
  2. Click Create Webhook
  3. Enter your endpoint URL
  4. Select the events you want to receive
  5. Save the webhook

3. Verify Signatures

Each webhook request includes a signature header for verification:

X-Memokit-Signature: sha256=abc123...

Verify the signature using HMAC-SHA256:

const crypto = require('crypto');

function verifySignature(payload, signature, secret) {
  const expectedSignature = 'sha256=' + crypto
    .createHmac('sha256', secret)
    .update(JSON.stringify(payload))
    .digest('hex');

  return crypto.timingSafeEqual(
    Buffer.from(signature),
    Buffer.from(expectedSignature)
  );
}

Webhook Retries

If your endpoint returns an error (non-2xx status code), Memokit will retry the delivery:

AttemptDelay
1Immediate
21 minute
35 minutes
430 minutes
52 hours

After 5 failed attempts, the webhook delivery is marked as failed.

Best Practices

Return Quickly

Return a 200 response as soon as possible. Process webhook data asynchronously:

app.post('/webhooks/memokit', async (req, res) => {
  // Verify signature
  if (!verifySignature(req.body, req.headers['x-memokit-signature'], secret)) {
    return res.status(401).send('Invalid signature');
  }

  // Queue for processing
  await queue.add('process-webhook', req.body);

  // Return immediately
  res.status(200).send('OK');
});

Handle Duplicates

Webhook deliveries may be sent more than once. Use the delivery ID to deduplicate:

async function processWebhook(payload) {
  const deliveryId = payload.id;

  // Check if already processed
  if (await isDeliveryProcessed(deliveryId)) {
    return;
  }

  // Process the webhook
  await handleWebhookEvent(payload);

  // Mark as processed
  await markDeliveryProcessed(deliveryId);
}

Use HTTPS

Always use HTTPS endpoints to ensure webhook data is encrypted in transit.

Viewing Webhook Logs

In the Memokit Console, you can view webhook delivery logs:

  1. Go to Webhooks
  2. Click on a webhook
  3. View the Delivery Logs tab

Each log entry shows:

  • Delivery timestamp
  • Event type
  • Response status code
  • Response time
  • Request/response bodies (for debugging)

Error Codes

CodeDescription
WEBHOOK_NOT_FOUNDThe specified webhook does not exist
INVALID_WEBHOOK_URLThe webhook URL is not valid
WEBHOOK_DELIVERY_FAILEDThe webhook delivery failed after all retries

On this page