Webhooks

Use webhooks to get notified about events in your Drupal site and trigger external workflows.

Creating a Webhook

You can create a webhook in the DCloud.dev dashboard. You'll need to provide a URL for your webhook endpoint and select the events you want to subscribe to.

Event Payloads

When an event is triggered, DCloud.dev will send a POST request to your webhook URL with a JSON payload. Here's an example of a payload for a `node.update` event:

{
  "event": "node.update",
  "entity": {
    "type": "article",
    "id": "your-article-uuid",
    "attributes": {
      "title": "My Updated Article"
    }
  }
}

Securing Your Webhooks

It's important to secure your webhook endpoints to prevent unauthorized access. DCloud.dev includes a secret token with each webhook request that you can use to verify the request's authenticity.

const DCLOUD_SECRET_TOKEN = process.env.DCLOUD_SECRET_TOKEN;

export default async function handler(req, res) {
  const signature = req.headers['x-dcloud-signature'];

  // Verify the signature

  res.status(200).json({ message: 'Webhook received' });
}

Next.js Example

Here's how you can create a Next.js API route to handle incoming webhooks:

// pages/api/webhook.js

export default async function handler(req, res) {
  if (req.method !== 'POST') {
    return res.status(405).json({ message: 'Method Not Allowed' });
  }

  const { event, entity } = req.body;

  if (event === 'node.update') {
    console.log("Node " + entity.id + " of type " + entity.type + " was updated.");
    // Trigger a rebuild or other action
  }

  res.status(200).json({ message: 'Webhook received' });
}