> ## Documentation Index
> Fetch the complete documentation index at: https://developer.box.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Create webhooks (v2)

export const MultiRelatedLinks = ({sections = []}) => {
  if (!sections || sections.length === 0) {
    return null;
  }
  return <div className="space-y-8">
      {sections.map((section, index) => <RelatedLinks key={index} title={section.title} items={section.items} />)}
    </div>;
};

export const RelatedLinks = ({title, items = []}) => {
  const getBadgeClass = badge => {
    if (!badge) return "badge-default";
    const badgeType = badge.toLowerCase().replace(/\s+/g, "-");
    return `badge-${badge === "ガイド" ? "guide" : badgeType}`;
  };
  if (!items || items.length === 0) {
    return null;
  }
  return <div className="my-8">
      {}
      <h3 className="text-sm font-bold uppercase tracking-wider mb-4">{title}</h3>

      {}
      <div className="flex flex-col gap-3">
        {items.map((item, index) => <a key={index} href={item.href} className="py-2 px-3 rounded related_link hover:bg-[#f2f2f2] dark:hover:bg-[#111827] flex items-center gap-3 group no-underline hover:no-underline border-b-0">
            {}
            <span className={`px-2 py-1 rounded-full text-xs font-semibold uppercase tracking-wide flex-shrink-0 ${getBadgeClass(item.badge)}`}>
              {item.badge}
            </span>

            {}
            <span className="text-base">{item.label}</span>
          </a>)}
      </div>
    </div>;
};

export const Link = ({href, children, className, ...props}) => {
  const localizedHref = href;
  return <a href={localizedHref} className={className} {...props}>
      {children}
    </a>;
};

V2 webhooks can monitor specific files or folders. They can be
created in the [Developer Console][console] and with API.

## Developer console

<Warning>
  V2 webhooks can be created only when the scope **Manage Webhooks**
  is selected and the application is authorized. See more about
  <Link href="/guides/applications">Required Access Scopes</Link> and <Link href="/guides/authorization">authorization</Link>.
</Warning>

To create a webhook follow the steps below.

1. Navigate to your application in the [Developer Console][console].
2. Select the **Webhooks** tab.
3. Click the **Create webhook** button.
4. Select **V2** from the drop-down list.
5. Fill in the form.
6. Click **Create webhook** button to save your changes.

### Required fields

| Field name   | Description                                                  | Required |
| ------------ | ------------------------------------------------------------ | -------- |
| URL Address  | URL address to be notified by the webhook.                   | Yes      |
| Content type | Type of content (file/folder) the webhook is configured for. | Yes      |
| Triggers     | Different triggers that activate the webhook.                | Yes      |

## API

<Warning>
  This API requires the application to have the **Manage Webhooks** scope enabled.
</Warning>

To attach a webhook to a folder, call the <Link href="/reference/post-webhooks">create webhook</Link> endpoint with the
type of `folder`, the ID of the folder, a URL to send webhook notifications to,
and a list of <Link href="/guides/webhooks/triggers">triggers</Link>.

<CodeGroup>
  ```sh cURL theme={null}
  curl -i -X POST "https://api.box.com/2.0/webhooks" \
       -H "authorization: Bearer <ACCESS_TOKEN>" \
       -H "content-type: application/json" \
       -d '{
         "target": {
           "id": "21322",
           "type": "file"
         },
         "address": "https://example.com/webhooks",
         "triggers": [
           "FILE.PREVIEWED"
         ]
       }'
  ```

  ```typescript Node/TypeScript v10 theme={null}
  await client.webhooks.createWebhook({
    target: {
      id: folder.id,
      type: 'folder' as CreateWebhookRequestBodyTargetTypeField,
    } satisfies CreateWebhookRequestBodyTargetField,
    address: 'https://example.com/new-webhook',
    triggers: ['FILE.UPLOADED' as CreateWebhookRequestBodyTriggersField],
  } satisfies CreateWebhookRequestBody);
  ```

  ```python Python v10 theme={null}
  client.webhooks.create_webhook(
      CreateWebhookTarget(id=folder.id, type=CreateWebhookTargetTypeField.FOLDER),
      "https://example.com/new-webhook",
      [CreateWebhookTriggers.FILE_UPLOADED],
  )
  ```

  ```csharp .NET v10 theme={null}
  await client.Webhooks.CreateWebhookAsync(requestBody: new CreateWebhookRequestBody(target: new CreateWebhookRequestBodyTargetField() { Id = folder.Id, Type = CreateWebhookRequestBodyTargetTypeField.Folder }, address: "https://example.com/new-webhook", triggers: Array.AsReadOnly(new [] {new StringEnum<CreateWebhookRequestBodyTriggersField>(CreateWebhookRequestBodyTriggersField.FileUploaded)})));
  ```

  ```swift Swift v10 theme={null}
  try await client.webhooks.createWebhook(requestBody: CreateWebhookRequestBody(target: CreateWebhookRequestBodyTargetField(id: folder.id, type: CreateWebhookRequestBodyTargetTypeField.folder), address: "https://example.com/new-webhook", triggers: [CreateWebhookRequestBodyTriggersField.fileUploaded]))
  ```

  ```java Java v10 theme={null}
  client.getWebhooks().createWebhook(new CreateWebhookRequestBody(new CreateWebhookRequestBodyTargetField.Builder().id(folder.getId()).type(CreateWebhookRequestBodyTargetTypeField.FOLDER).build(), "https://example.com/new-webhook", Arrays.asList(CreateWebhookRequestBodyTriggersField.FILE_UPLOADED)))
  ```

  ```java Java v5 theme={null}
  // Listen for preview events for a file
  BoxFile file = new BoxFile(api, id);
  BoxWebHook.Info webhookInfo = BoxWebHook.create(file, url, BoxWebHook.Trigger.FILE.PREVIEWED);
  ```

  ```py Python v4 theme={null}
  file = client.file(file_id='12345')
  webhook = client.create_webhook(file, ['FILE.PREVIEWED'], 'https://example.com')
  print(f'Webhook ID is {webhook.id} and the address is {webhook.address}')
  ```

  ```csharp .NET v6 theme={null}
  var webhookParams = new BoxWebhookRequest()
  {
      Target = new BoxRequestEntity()
      {
          Type = BoxType.file,
          Id = "22222"
      },
      Triggers = new List<string>()
      {
          "FILE.PREVIEWED"
      },
      Address = "https://example.com/webhook"
  };
  BoxWebhook webhook = await client.WebhooksManager.CreateWebhookAsync(webhookParams);
  ```

  ```js Node v4 theme={null}
  // Attach a webhook that sends a notification to https://example.com/webhook when
  //   file 11111 is renamed or downloaded.
  client.webhooks.create(
   '11111',
   client.itemTypes.FILE,
   'https://example.com/webhook',
   [
    client.webhooks.triggerTypes.FILE.RENAMED,
    client.webhooks.triggerTypes.FILE.DOWNLOADED
   ])
   .then(webhook => {
    /* webhook -> {
     id: '12345',
     type: 'webhook',
     target: { id: '11111', type: 'file' },
     created_by: 
     { type: 'user',
      id: '33333',
      name: 'Example User',
      login: 'user@example.com' },
     created_at: '2016-05-09T17:41:27-07:00',
     address: 'https://example.com/webhook',
     triggers: [ 'FILE.RENAMED', 'FILE.UPLOADED' ] }
    */
   });
  ```
</CodeGroup>

<Note>
  Webhooks do cascade, so if a webhook is set on a parent folder,
  it will also monitor sub-folders for the selected triggers.
</Note>

## Ownership

It is best practice and strongly recommended to create webhooks with a
<Link href="/platform/user-types/#service-account">Service Account</Link>, or user that will not be deleted, to avoid potential
issues with webhook delivery due to loss of access to content.

Similar to files and folders, webhooks are owned by a user. If a user who owns a
webhook is deleted, they will lose access to all files and folders that they
previously had access to. Their webhooks will begin to fail validation, but the
webhook service will continue to send events and require retries.

## Webhook address

The notification URL specified in the `address` parameter must be a valid URL
that you specify when you create a webhook. Every time one of the triggers is
activated, this URL is called.

The notification URL must use standard port `443` and should return
an HTTP status in the range of `200` to `299` within 30 seconds of receiving
the webhook payload.

## Webhook triggers

The triggers are a list of strings that specify the events which cause the
webhook to fire. For example, if you want the webhook to be triggered
when a user uploads a file, use `FILE.UPLOADED`.

You can find a list of available triggers <Link href="/guides/webhooks/triggers">in this guide</Link>.

[console]: https://app.box.com/developers/console

<RelatedLinks
  title="RELATED APIS"
  items={[
{ label: translate("Create webhook"), href: "/reference/post-webhooks", badge: "POST" }
]}
/>

<RelatedLinks
  title="RELATED GUIDES"
  items={[
{ label: translate("Webhook Event Triggers"), href: "/guides/webhooks/triggers", badge: "GUIDE" },
{ label: translate("V2 Webhooks"), href: "/guides/webhooks/v2/index", badge: "GUIDE" },
{ label: translate("Delete Webhooks"), href: "/guides/webhooks/v2/delete-v2", badge: "GUIDE" }
]}
/>
