Made in Builder.io

Announcing Visual Copilot - Figma to live in half the time

Builder.io logo
Talk to Us
Platform
Developers
Talk to Us

Blog

Home

Resources

Blog

Forum

Github

Login

Signup

×

Visual CMS

Drag-and-drop visual editor and headless CMS for any tech stack

Theme Studio for Shopify

Build and optimize your Shopify-hosted storefront, no coding required

Resources

Blog

Get StartedLogin

Global webhooks are essentially notifications sent over the web through HTTP POST requests to an external system or service you control, such as your application's backend server whenever a significant event occurs within your Builder.io account.

Unlike model-specific webhooks that only trigger based on activities within a particular model, global webhooks are Space-wide and can trigger from events across all models and content within your account.

To get the most out of this document, you need:

Often, reasons for using global webhooks include:

  • Workflow automation: Automating tasks that should occur in response to various events in Builder.io, such as content publishing or updating.
  • Integration with other systems: Syncing content changes with external databases, CMS, personalization engines, or even cache invalidation in CDNs, so that changes in Builder reflect immediately wherever needed.
  • Efficiency and scalability: Reducing the need for manual checks or API polling to detect changes, which can be inefficient and not scalable.

To set up a global webhook:

  1. Go to the Space Settings.
  2. On the Space tab, click on the Edit button.
  3. In the dialogue that opens, click + Webhook.
  4. Expand the new Webhook that displays and add the endpoint (URL) for Builder to send the POST request.
  5. Optionally add any custom headers as needed, providing a Name and Value; for example, a specific header required by your server, such as an Authorization header for security.
  6. Optionally prevent actual data from being included in the payload by expanding the advanced section by clicking Show Advanced and toggle on Disable Payload.
  7. Click the Save button.

Tip: Enabling the Disable Payload Toggle option means the POST request from Builder does not include the actual data, such asnewValue and previousValue, related to the event. This can be useful if you're only interested in knowing that an event occurred — such as a content update — but don't need the details about what changed. This might be the case if you're using the webhook to trigger a generic task, like clearing a cache, where the specifics of the change are not relevant.

The video below shows where to set up a global webhook:

When Builder sends data to your specified endpoint, your server needs to be prepared to process it. Below is an example of how you might handle incoming webhook data in a Next.js application where you can handle webhooks on the API routes.

Create a file for your webhook handler inside the pages/api directory of your Next.js project. You might name it webhook.js for clarity. The path to the file defines the URL path. For example, a file at pages/api/webhook.js handles requests at /api/webhook.

Implement the POST handler in your webhook.js file. Here's how you might code it:

// pages/api/webhook.js

export default async function handler(req, res) {
  // Ensure method is POST
  if (req.method === 'POST') {
    const { newValue, previousValue } = req.body;

    console.log('New Value:', newValue);
    console.log('Previous Value:', previousValue);

    // Here you can add the logic to process the webhook data
    // For example, updating your local database, invalidating cache, etc.

    // Respond to the request indicating success
    return res.status(200).json({ message: 'Webhook received and processed' });
  } else {
    // Handle any other HTTP methods
    res.setHeader('Allow', ['POST']);
    return res.status(405).end(`Method ${req.method} Not Allowed`);
  }
}

Make sure that your webhook endpoint is secured:

  • Validate incoming requests to make they originate from Builder.io.
  • Use HTTPS to protect the data in transit.
  • Consider rate limiting to protect against abuse. This can sometimes be handled at the infrastructure level, depending on your hosting provider.

When you've configured the webhook URL in Settings, Builder sends HTTP POST requests to the specified URL whenever the defined events occur; for example, content updates.

Your endpoint should be prepared to parse and handle the incoming data, which typically includes fields like newValue and previousValue, representing the new and previous states of the content.

This document covered global webhooks. For information on webhooks for models, visit Webhooks.

Was this article helpful?

Product

Visual CMS

Theme Studio for Shopify

Sign up

Login

Featured Integrations

React

Angular

Next.js

Gatsby

Get In Touch

Chat With Us

Twitter

Linkedin

Careers

© 2020 Builder.io, Inc.

Security

Privacy Policy

Terms of Service

Newsletter

Get the latest from Builder.io

By submitting, you agree to our Privacy Policy