> 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 Webhooks

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

Aikido から届く webhook が実際に Aikido 由来であり、かつそのペイロードが改ざんされていないことを検証するために、Aikido は共有したシークレットで署名したペイロードのハッシュを使用します。

{% hint style="success" %}
Aikido は、常に次の IP アドレスから webhook リクエストを送信します: 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」をクリックしてシークレットを作成します。<br>

   ![新しい 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 シークレットをコピーして安全に保管できるモーダルが表示されます。このシークレットは安全に保管し、コードリポジトリのいずれにもコミットしないことが重要です

### 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(`サーバーはポート ${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.
