Prevent Webhook Trigger for certain actions

I currently have a webhook setup that calls an HTTP endpoint when a record is published.

Within the endpoint, I am processing the data and generating a set of keywords, which then needs to be written back to the same record. Essentially, this is a blog post where the API generates keywords related to the blog and then writes them back to the record so it can be fetched our at a later point. It’s done this way since generating the keywords takes up quite sometime and it cannot be done on the fly.

My assumption here is that I will likely run into an endless loop problem once the data is written back to the record as that will trigger another webhook call which will write the data again and so on.

Is there a way I can write the data and also specify that the trigger shouldn’t activate for a given write or an alternativce way in which I can write back data without triggering the webhook?

If I could specify certain field within a record that should trigger a webhook on publish, that should work too as I can exclude the filed that is updated withint the API that would in turn trigger a webhook.

Hey @jude,

This is an interesting question! I think you can do this if you can modify the endpoint and add some conditional logic there, and have it do some basic diffing on the keywords field. For example, first you’d start by modifying the webhook and adding the create and update events (because this will let you better capture changes between versions):

Then, in the webhook body, do a custom payload similar to this:

  "message": "{{event_type}} event triggered on {{entity_type}}!",
  "entity_id": "{{#entity}}{{id}}{{/entity}}",
  "event": "{{event_type}}",
  "status": "{{entity.meta.status}}",
  "current_keywords": "{{entity.attributes.keywords}}",
  "previous_keywords": "{{previous_entity.attributes.keywords}}",
  "entity": "{{entity}}",
  "previous_entity": "{{previous_entity}}"

(Those are “Mustache” variables that you can learn about in our docs: Webhooks — DatoCMS)

Those variables get expanded by us, and your actual webhook payload would look like this:

  "message": "update event triggered on item!",
  "entity_id": "KOS3cnXEQ6KK46jMag04Mg",
  "event": "update",
  "status": "updated",
  "current_keywords": "new keywords",
  "previous_keywords": "alpha, beta, charlie, delta",
  "entity": "{"id":"KOS3cnXEQ6KK46jMag04Mg","type":"item","attributes":{"title":"This is a test post 7 - keywords changed","keywords":"new keywords"},"relationships":{"item_type":{"data":{"id":"fLNw2cmTTLOqE9lbInQPnA","type":"item_type"}},"creator":{"data":{"id":"627975","type":"organization"}}},"meta":{"created_at":"2024-01-02T20:30:51.280+00:00","updated_at":"2024-01-02T20:38:51.683+00:00","published_at":"2024-01-02T20:38:51.709+00:00","publication_scheduled_at":null,"unpublishing_scheduled_at":null,"first_published_at":"2024-01-02T20:30:51.306+00:00","is_valid":true,"is_current_version_valid":true,"is_published_version_valid":true,"status":"published","current_version":"IoHkZjh0S4-QKKnFGqJDHA","stage":null}}",
  "previous_entity": "{"id":"KOS3cnXEQ6KK46jMag04Mg","type":"item","attributes":{"title":"This is a test post 6 - keywords added","keywords":"alpha, beta, charlie, delta"},"relationships":{"item_type":{"data":{"id":"fLNw2cmTTLOqE9lbInQPnA","type":"item_type"}},"creator":{"data":{"id":"627975","type":"organization"}}},"meta":{"created_at":"2024-01-02T20:30:51.280+00:00","updated_at":"2024-01-02T20:37:27.841+00:00","published_at":"2024-01-02T20:37:27.869+00:00","publication_scheduled_at":null,"unpublishing_scheduled_at":null,"first_published_at":"2024-01-02T20:30:51.306+00:00","is_valid":true,"is_current_version_valid":true,"is_published_version_valid":true,"status":"published","current_version":"AFzmw21IQ7CmoBaEUrKxDQ","stage":null}}"

In this example, in your webhook handler you could then do some conditional checks:

  • if (event === 'create' || (event === 'update' && current_keywords !== previous_keywords)) then generate keywords

  • if (event === 'publish') then actually publish

You can modify the logic as needed, but basically, the webhook payload lets you do these conditionals in your handler as you see fit.

Does that help?

Thank you, that definitely did the trick.

1 Like