WebhookModel

View as Markdown

WebhookModel

WebhookModel holds the details of a webhook: its name, target URLs, the event channels it listens to, and its delivery options. Pass it to the Create or Update method of Webhook.

When to Use This Class: Use this class to build the request body when you create or change a webhook.

Conceptual Role: Acts as the data model that Webhook serializes under the webhook key of a request.

Returns: WebhookModel. The details of a webhook.

Properties

NameTypeRequiredDefaultDescription
Namestring?NonullName of the webhook. Serialized as name.
destinationsList<WebhookTarget>?NonullTarget URLs and their credentials, as WebhookTarget items. Serialized as destinations.
ChannelsList<string>?NonullEvents that trigger the webhook. Serialized as channels.
BranchesList<string>?NonullUIDs of the branches the webhook listens to. Serialized as branches.
RetryPolicystring?NonullRetry policy for failed requests. Serialized as retry_policy.
DisabledboolNofalsetrue disables the webhook. Serialized as disabled.
ConcisePayloadboolNotruetrue sends a concise JSON payload to the target URL. Serialized as concise_payload.

Class-Level Notes

What the SDK sends.

  • The SDK leaves every null property out of the request body.
  • Disabled and ConcisePayload are bool properties, so the SDK always sends them. A new model sends disabled: false and concise_payload: true.
  • The destinations property starts with a lowercase letter, unlike the other properties. Write it that way in your code.

Additional Resource Refer to Create a webhook for the request body.

Method Index

WebhookModel has no methods. Set its properties and pass it to the Create or Update method of Webhook.

Class-Level Snippet

This snippet builds a webhook that calls one target URL with basic authentication and a custom header, and creates it.

using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;

ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");

WebhookModel model = new WebhookModel()
{
    Name = "Notify Search Indexer",
    destinations = new List<WebhookTarget>
    {
        new WebhookTarget()
        {
            TargetUrl = "https://example.com/hooks/contentstack",
            HttpBasicAuth = "<BASIC_AUTH_USERNAME>",
            HttpBasicPassword = "<BASIC_AUTH_PASSWORD>",
            CustomHeader = new List<Dictionary<string, object>>
            {
                new Dictionary<string, object> { ["header_name"] = "X-Source", ["value"] = "contentstack" }
            }
        }
    },
    Channels = new List<string> { "content_types.entries.publish" },
    Branches = new List<string> { "main" },
    RetryPolicy = "manual",
    Disabled = false,
    ConcisePayload = true
};

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").Webhook().Create(model);
    Console.WriteLine(response.OpenJsonObjectResponse().ToJsonString());
}
catch (ContentstackErrorException e)
{
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}