2

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?

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58
thebfftim
  • 109
  • 1
  • 6

1 Answers1

2

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
       }
   }
}
Robban
  • 6,729
  • 2
  • 39
  • 47
  • That all makes complete sense, however how can I know which parameters/record are being sent to the webhook? – thebfftim Jun 07 '17 at 04:38
  • The published entry is sent as a payload to the webhook. Updating my answer with an example. – Robban Jun 07 '17 at 05:05
  • Is there a way to extract the payload with PHP? – thebfftim Jun 07 '17 at 07:01
  • 1
    Absolutely, you could do something like `$payload = file_get_contents('php://input');` and that would read the json string into the payload variable. You could then decode it using `$jsonObject = json_decode($string, true);` and proceed to read a certain key like this `$jsonObject['fields']['publishedThroughAPI']['en-US']` – Robban Jun 07 '17 at 07:14
  • great, this works when an entry is published via the API. However, is there a similar trick I could use to unpublish an entry via the API, without triggering the webhook? With the contentful-management api, it seems I can only provide entry id as payload – Manube Oct 29 '17 at 16:12
  • How do you add this kind of custom code to a Webhook in contentful - I mean the part where I can inspect the published entry and return out of the hook. Any help appreciated here! – Christian Jan 11 '22 at 16:01
  • Accepted answer doesn't seem useful. Once you set a hidden field to true the webhook would never run again on future content updates – chrisd08 Jun 29 '23 at 14:17