> For the complete documentation index, see [llms.txt](https://help.aikido.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.aikido.dev/docs/docs-ja/sonono/aikido-webhooks.md).

# Aikido Webhook

## Webhook認証 <a href="#webhooks-authentication" id="webhooks-authentication"></a>

Aikido からの受信 Webhook が実際に Aikido に由来するものであり、かつそのペイロードが改ざんされていないことを確認するために、Aikido は、こちらから共有する秘密鍵で署名されたペイロードのハッシュを使用します。

{% hint style="success" %}
Aikido は、Webhook リクエストを常に次の IP アドレスから送信します: 52.18.113.172。
{% endhint %}

### Webhook スキーマ <a href="#webhook-schema" id="webhook-schema"></a>

Webhook スキーマは次にあります [弊社の API ドキュメント](https://apidocs.aikido.dev/reference/webhooks).

### Webhook シークレットの生成 <a href="#generating-a-webhook-secret" id="generating-a-webhook-secret"></a>

1. 次へ移動してください [webhooks 統合ページ](https://app.aikido.dev/settings/integrations/api/webhooks)
2. 右側の表のすぐ上にある「Add secret」をクリックして、シークレットを作成します。\
   ​

   ![新しい Webhook の登録を促す空の webhooks ダッシュボード。](https://715870456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyKbzcQGrx7UtrG0nPZZ7%2Fuploads%2Fgit-blob-8a7cd767433b87053481c72e5ef8e3e9501945a8%2Fucarecdn-d432b8a3-edad-4ecd-b9a4-2be5a86a4801.png?alt=media)
3. Webhook シークレットが作成されると、シークレットをコピーして安全に保管できるモーダルが表示されます。このシークレットは安全に保管し、コードリポジトリのいずれにもコミットしないことが重要です

### Webhook の検証 <a href="#verifying-a-webhook" id="verifying-a-webhook"></a>

Aikido が設定されたイベントの Webhook を送信するたびに、そのイベントのペイロードのハッシュを作成し、直前に生成したシークレットで署名します。この固有のハッシュは、以下を通じて含まれます `X-Aikido-Webhook-Signature` リクエストヘッダー。これにより、Webhook とそのペイロードが真正であることを確認できます。

リプレイ攻撃を防ぐため、HTTP リクエストを送信する直前の時刻のエポックタイムスタンプを Webhook ペイロードに含めます。このタイムスタンプは次の項目として含まれます `dispatched_at` プロパティであり、ペイロードを検証する際に 30 秒より古くてはいけません。

お使いのプログラミング言語に関係なく、受信した Webhook を検証する手順は次のとおりです:

1. ペイロードが有効な JSON 文字列であることを確認する
2. 次からシグネチャを取得する `X-Aikido-Webhook-Signature` リクエストヘッダー
3. リクエストボディを JSON 文字列に戻す
4. 次を使って、文字列化したリクエストボディから HMAC ダイジェストを作成する `sha256` アルゴリズムを使い、Aikido の Webhook シークレットで署名する
5. リクエストヘッダーのシグネチャが、今生成したダイジェストと一致することを検証する。
6. 次を検証する `dispatched_at` プロパティのエポックタイムスタンプが 30 秒より古くないこと

以下では、次を使用してハッシュを検証する方法を示す擬似 JavaScript コードを共有します `express` フレームワーク。これはミドルウェアとして組み込むべきであり、値が有効かどうかを確認するために、さらに検証を行う必要があります。

```
const { createHmac } = require('node:crypto');

const express = require('express');
const bodyParser = require('body-parser');

const PORT = 4000;

const app = express();

app.use(bodyParser.json());

const isIncomingWebhookValid = (payload, signature) => {
	// 環境変数から生の webhook シークレットを取得する
	const aikidoWebhookSecret = process.env.AIKIDO_WEBHOOK_SECRET;

	// HMAC インスタンスを作成する
	const hmac = createHmac('sha256', aikidoWebhookSecret);

	// ペイロードオブジェクトを JSON 文字列に変換する
	const rawPayload = JSON.stringify(payload);

	// 文字列化したペイロードで HMAC の内容を更新する
	hmac.update(rawPayload);

	// HMAC のダイジェストを計算し、16進値として返す
	const payloadDigest = hmac.digest('hex');

	// ダイジェストが与えられたシグネチャと一致しない場合、Webhook は無効です
	if (payloadDigest !== signature) return false;

	// 現在のエポックタイムスタンプを取得する
	const currentEpochTimestamp = Math.floor(new Date().getTime() / 1000);
	
	// ペイロードの dispatched_at エポックタイムスタンプが 30 秒より古い場合、Webhook は無効です
	if ((currentEpochTimestamp - (payload['dispatched_at'] ?? 0)) > 30) return false;

	// Webhook はすべてのチェックを通過し、有効です
	return true;
}

app.post('/webhooks/aikido/issue-created', async (req, res) => {
	const isValid = isIncomingWebhookValid(req.body, req.headers['X-Aikido-Webhook-Signature']);
	if (!isValid) {
		throw new Error(`リクエストのシグネチャが無効です`)
	}
    
	// あなたのビジネスコード

    res.status(204);
});

app.listen(PORT, () => {
	console.log(`server listening on port ${port}`);
});
```

***


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://help.aikido.dev/docs/docs-ja/sonono/aikido-webhooks.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
