Create Webhook with javascript API ~ INVALID_FIELD

Hi, I’m having trouble deciphering the parameters to create a webhook via the API.

I’m finding that the parameter fields and naming used in the webbrowser don’t correlate with the parameters expected by the SDK.

For example, in the browser I get asked to add triggers but in the SDK I think this is equivelant to the below parameters, but i’m not 100% sure.

Additionally, there is no guidence on what you can pass into the filter:{} parameters.

events: [
            {
                entity_type: 'item',
                event_types: [
                    'publish'
                ],
                filter: {
                    entity_type: 'item_type',
                    entity_ids: [
                        'app_events_config'
                    ]
                }
            }
        ],

But i’m getting the error

triggerUncaughtException(err, true /* fromPromise */);
            ^

ApiError: POST https://site-api.datocms.com/webhooks: 422 Unprocessable Entity

[
  {
    "id": "05b4ed",
    "type": "api_error",
    "attributes": {
      "code": "INVALID_FIELD",
      "details": {
        "field": "name",
        "code": "VALIDATION_UNIQUENESS",
        "record_type": "Webhook"
      }
    }
  },
  {
    "id": "36416a",
    "type": "api_error",
    "attributes": {
      "code": "INVALID_FIELD",
      "details": {
        "field": "events",
        "code": "VALIDATION_INVALID",
        "record_type": "Webhook"
      }
    }
  }
]

Additionally, there doesn’t appear to be any documentation explaining the Events Object and what parameters you should provide, fill, or how they correlate to the webhook properties you create manually.

For example

  • what can you pass into entity_type ?

I found this document (seperate from the webhook example)

Whats the difference between item and item_type when to use each?

  • What can you pass into filters : {} ?
    I have as yet been unable to properly pass in a filter definition that doesn’t fail as per above errors.

Any help greatly appreciated.

I figured it out.

Used the webhook list API call to determine what kind of content was being stored and used that as the submission data in the filters section.

const { buildClient } = require('@datocms/cma-client-node');
const dotenv = require('dotenv').config()

async function run() {
    const client = buildClient(
        {
            apiToken: process.env.CMS_DATOCMS_API_KEY,
            //environment: process.env.CMS_DATOCMS_ENV
        });

    const webhooks = await client.webhooks.list();

    webhooks.forEach((webhook) => {
        console.log(JSON.stringify(webhook));
        //console.log(webhook.events.filters);
    });


    const webhookEventConfig = await client.webhooks.list({
        http_basic_user: '',
        http_basic_password: '',
        enabled: true,
        payload_api_version: '3',
        nested_items_in_payload: true
    });

    console.log(JSON.stringify(webhookEventConfig, null, 3));
}
run();

The Documentation could perhaps update the example reference code and or also list the above list method to help guide users down the right path