Search code examples
javascriptphpcontentfulcontentful-management

Contentful: Prevent webhook from triggering, when using Content Management API


So I've created a webhook within the Contentful dashboard, that triggers when an entry is either published or deleted, and it works as it should. However, I want to be able to publish entries via the Content Management JavaScript API, without triggering the webhook. Are there any measures I can take to make this possible?


Solution

  • Unfortunately there's no difference if an entry is published through the API or the web app directly. The web app simply calls the API under the hood.

    What might be possible is to inspect the published entry within the web hook and evaluate whether code should execute or not. Perhaps by setting a hidden field when publishing through the API directly.

    For example say you have a field publishedThroughAPI, you make sure this field is omitted from the delivery API and not editable: enter image description here

    Then set this field to true just before publishing through the JavaScript API and inspect this field in your webhook and simply return out of the hook if the field is set to true.

    The webhook will recieve a payload that contains your published entry. Basically the same payload as for a normal request with the difference that it will contain every locale. Below is a small example:

    {
       "sys": {
             //System meta data, created at, published at etc.
        },
       "fields": {
           //All fields of the entry.
           "title": {
                "en-US": "English title",
                "sv-SE": "Swedish title"
           },
           "publishedThroughAPI": {
                "en-US": true,
                "sv-SE": true
           }
       }
    }