> ## 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.

# Webhookの作成 (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 = localizeLink(href);
  return <a href={localizedHref} className={className} {...props}>
      {children}
    </a>;
};

V2 Webhookは、特定のファイルまたはフォルダを監視でき、[開発者コンソール][console]でもAPIでも作成できます。

## 開発者コンソール

<Warning>
  V2 Webhookを作成できるのは、\[**Webhookを管理する**] というスコープが選択され、アプリケーションが承認されている場合のみです。<Link href="/guides/applications">必須のアクセススコープ</Link>と<Link href="/guides/authorization">承認</Link>の詳細を参照してください。
</Warning>

Webhookを作成するには、以下の手順に従います。

1. [開発者コンソール][console]で、目的のアプリケーションに移動します。
2. \[**Webhook**] タブを選択します。
3. \[**Webhookを作成**] ボタンをクリックします。
4. ドロップダウンリストで \[**V2**] を選択します。
5. フォームに入力します。
6. \[**Webhookを作成**] ボタンをクリックして変更を保存します。

### 必須フィールド

| フィールド名   | 説明                                    | 必須 |
| -------- | ------------------------------------- | -- |
| URLアドレス  | Webhookによって通知されるURLアドレス。              | はい |
| コンテンツタイプ | Webhookが構成されているコンテンツのタイプ (ファイル/フォルダ)。 | はい |
| トリガー     | Webhookをアクティブ化するさまざまなトリガー。            | はい |

## API

<Warning>
  このAPIを使用するには、アプリケーションの \[**Webhookを管理する**] スコープが有効になっている必要があります。
</Warning>

フォルダにWebhookを追加するには、`folder`の種類、フォルダのID、Webhook通知の送信先URL、および<Link href="/guides/webhooks/triggers">トリガー</Link>のリストを指定して<Link href="/reference/post-webhooks">Webhookを作成</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>
  Webhookはカスケードで適用されるため、Webhookを親フォルダに設定すると、サブフォルダでも選択されたトリガーが監視されます。
</Note>

## 所有権

コンテンツにアクセスできなくなることでWebhookの配信に生じる可能性のある問題を回避するために、<Link href="/platform/user-types/#service-account">サービスアカウント</Link> (つまり削除されることのないユーザー) を使用してWebhookを作成することを強くお勧めします。

ファイルやフォルダと同様、Webhookを所有するのはユーザーです。Webhookを所有するユーザーが削除されると、以前アクセスできていたすべてのファイルとフォルダにアクセスできなくなります。ユーザーのWebhookでは検証が失敗するようになりますが、Webhookサービスは引き続きイベントを送信し、再試行を要求します。

## Webhookアドレス

`address`パラメータで指定する通知URLは、Webhookの作成時に指定した有効なURLである必要があります。このURLは、いずれかのトリガーがアクティブになるたびに呼び出されます。

通知URLは標準ポート`443`を使用する必要があり、Webhookペイロードの受信から30秒以内に`200`～`299`の範囲のHTTPステータスを返す必要があります。

## Webhookトリガー

トリガーのリストでは、Webhookによって発生するイベントを表す文字列を指定します。たとえば、ユーザーがファイルをアップロードしたときにWebhookをトリガーするには`FILE.UPLOADED`を使用します。

使用可能なトリガーのリストは、<Link href="/guides/webhooks/triggers">こちらのガイド</Link>を参照してください。

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

<RelatedLinks
  title="関連するAPI"
  items={[
{ label: translate("Create webhook"), href: "/reference/post-webhooks", badge: "POST" }
]}
/>

<RelatedLinks
  title="関連するガイド"
  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" }
]}
/>
