> 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/zen-firewall/zen-installation-instructions/zen-firewall-for-python/fastapi.md).

# Python（FastAPI）

このガイドでは、アプリケーション向けに Aikido の Zen Firewall をインストールして設定する方法を順を追って説明します。アプリケーションを保護するために、以下の手順に従ってください。

問題や不具合が発生した場合は、サポートチャットまたは GitHub の issue でお気軽にご連絡ください

<https://github.com/AikidoSec/firewall-python>

## 要件

* Python 3.8以上（3.14までサポート）。
* FastAPI（Zen Firewall でサポート）。
* [Aikidoアカウント](/docs/docs-ja/hajimeni/setting-up-your-account.md) & [Zen Firewallトークン](/docs/docs-ja/zen-firewall/zen-installation-instructions/creating-an-aikido-zen-firewall-token.md)

## インストールと設定

{% stepper %}
{% step %}
**AikidoによるZen Firewallをインストール**

pip を使って Zen をインストールします:

```bash
pip install aikido_zen
```

トークンの環境変数を設定します:

```bash
export AIKIDO_TOKEN=AIK_RUNTIME_YOUR_TOKEN_HERE
```

可能であれば、エントリポイントの先頭で、他の import より前に Zen を有効にします:

```python
import aikido_zen

aikido_zen.protect()
```

アプリ初期化の例:

```python
import aikido_zen
aikido_zen.protect()

from fastapi import FastAPI
app = FastAPI()
```

{% endstep %}

{% step %}
**リクエストブロックとユーザー識別を有効にする**

このミドルウェアを使用すると、レート制限、ユーザー識別、およびブロック機能を有効にできます。

Zen Firewall は攻撃をブロックするためにこのミドルウェアを必要としません。コアの攻撃防御はこれがなくても機能します。このミドルウェアは、Zen が次のような保護に使用する追加のリクエストコンテキストを提供します。 [レート制限](/docs/docs-ja/zen-firewall/zen-features/setting-up-rate-limiting-for-routes.md), [ユーザーブロック](/docs/docs-ja/zen-firewall/zen-features/blocking-users-with-zen-firewall.md), [ボットのブロック](/docs/docs-ja/zen-firewall/zen-features/blocking-bot-traffic-with-zen-firewall.md), [国別ブロック](/docs/docs-ja/zen-firewall/zen-features/blocking-traffic-by-country-with-zen-firewall.md)、および [脅威アクター](/docs/docs-ja/zen-firewall/zen-features/blocking-known-threat-actors-with-zen-firewall.md) ブロック。

例を、アプリケーションがユーザーを識別し、リクエストを処理する方法に合わせて調整してください。

Aikido ミドルウェアを追加:

```python
from aikido_zen.middleware import AikidoFastAPIMiddleware

app.add_middleware(AikidoFastAPIMiddleware)
```

認証ミドルウェアでユーザーを設定:

```python
from aikido_zen import set_user

set_user({"id": "123", "name": "John Doe"})
```

{% endstep %}

{% step %}
**Zen Firewall をドライ / 検出のみモードで開始する**

```bash
AIKIDO_BLOCK=false AIKIDO_TOKEN=YOUR_SECRET_TOKEN uvicorn app:app --reload
```

トークンを環境変数として設定し、Aikido Zen エージェントがそれを取得できるようにしてください。まだトークンがない場合は、次に従ってください [こちらの手順](/docs/docs-ja/zen-firewall/zen-installation-instructions/creating-an-aikido-zen-firewall-token.md).

```bash
AIKIDO_TOKEN=AIK_RUNTIME_
```

アプリが期待どおりに動作し、いかなるリクエストもブロックしないことを確認するため、まずは dry モードでアプリを起動することを推奨します。誤検知を避けるため、Zen Firewall はステージング環境で 2 週間実行することをお勧めします。

```bash
AIKIDO_BLOCK=false
```

{% hint style="info" %}
次を使用できます `AIKIDO_DEBUG=true` エージェントの動作について、より詳細な情報を得るためにデバッグモードを有効にするためのものです。環境変数の詳細については: [環境変数による設定](/docs/docs-ja/zen-firewall/zen-installation-instructions/configuration-via-environment-variables.md)
{% endhint %}
{% endstep %}

{% step %}
**アプリをテスト**

アプリケーションを開いて、いくつかの操作を行うか、いくつかのページを開いてください。Zen がアプリケーション内のルートを自動的に検出します。

{% hint style="info" %}
Zen は10分ごとに Aikido にデータを送信します
{% endhint %}

Zen アプリケーションの次のページを見ることで、エージェントが正常に動作しているか確認できます:

* **イベント**: 「Application started」イベントが表示されるはずです。
* **ルート**: しばらくすると、アプリケーションのルートがここにメソッド、ルート、リクエストとともに表示され始めます。
* **インスタンス**: Zen がインストールされているアプリケーションのアクティブなインスタンス数が表示されるはずです。

<figure><img src="https://715870456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyKbzcQGrx7UtrG0nPZZ7%2Fuploads%2Fgit-blob-79af8470fd5feef0b378588fc5de01c3ea89d9ae%2FScreenshot%202025-06-23%20at%2010.08.35.png?alt=media" alt=""><figcaption></figcaption></figure>
{% endstep %}

{% step %}
**ダッシュボードでレート制限を設定**

Zen Firewall ミドルウェアを追加したら、Aikido ダッシュボードでレート制限を設定することで、ルートをブルートフォース攻撃から保護するテストができます。

1. 作成したアプリをクリックします。
2. へ移動 **ルート** タブを選択します。
3. 制限したいルートを見つけてクリック **レート制限の設定**.
4. 手順に従ってレート制限を設定します（例：1分あたり5リクエスト）。

![保護およびレート制限のオプション付きで認証ルートを表示する API ルート管理インターフェース。](https://715870456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyKbzcQGrx7UtrG0nPZZ7%2Fuploads%2Fgit-blob-4f588cb82dce7a932262f90046f280ee5115a13d%2FScreenshot%202025-10-30%20at%2010.38.17.png?alt=media)

![POST /auth/login に対して、1分あたり5リクエストのレート制限を設定します。](https://715870456-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FyKbzcQGrx7UtrG0nPZZ7%2Fuploads%2Fgit-blob-2633dcea71b485b0257ecf6b607323a9646dae06%2Fucarecdn-34b1af76-facb-4b6d-9c87-155236236b8f.png?alt=media)

**レート制限の確認**

アプリを起動し、レート制限を設定したルートに1分以内に5回アクセスしてみてください。5回目の試行後、レート制限エラーが表示されるはずです：

```
Aikido ファイアウォールによりレート制限されています。（あなたのIP: 1.2.3.4）
```

{% endstep %}

{% step %}
**次のステップ**

Zen Firewall のインストールに成功しました。おめでとうございます。問題が発生した場合、ご不明点や機能の要望がある場合は、遠慮なくサポートまでご連絡ください。

これで、Zen Firewall が提供する多彩な機能をぜひご確認ください：

* [Zen FirewallでBotトラフィックをブロックする](/docs/docs-ja/zen-firewall/zen-features/blocking-bot-traffic-with-zen-firewall.md)
* [Zen FirewallでTorトラフィックをブロックまたは監視する](/docs/docs-ja/zen-firewall/zen-features/blocking-tor-traffic-with-zen-firewall.md)
* [Zen Firewallでユーザーを追跡する](/docs/docs-ja/zen-firewall/zen-features/blocking-users-with-zen-firewall.md)
* [Zen Firewallで既知の脅威アクターをブロックする](/docs/docs-ja/zen-firewall/zen-features/blocking-known-threat-actors-with-zen-firewall.md)
* [Zen Firewall で国別にトラフィックをブロックする](/docs/docs-ja/zen-firewall/zen-features/blocking-traffic-by-country-with-zen-firewall.md)
* [ルートのレート制限を設定する](/docs/docs-ja/zen-firewall/zen-features/setting-up-rate-limiting-for-routes.md)
* [アウトバウンドドメインを監視する](/docs/docs-ja/zen-firewall/zen-features/monitor-outbound-domains.md)

追加情報：

* [Zen のパフォーマンスと信頼性](/docs/docs-ja/zen-firewall/miscellaneous/how-zen-works-performance-reliability.md)
* [Zen Firewall のブロックモードと検知モード](/docs/docs-ja/zen-firewall/zen-features/blocking-vs-detection-mode-in-zen-firewall.md)
* [Zen の統計を理解する](/docs/docs-ja/zen-firewall/zen-features/understanding-your-zen-statistics.md)
  {% endstep %}
  {% endstepper %}


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.aikido.dev/docs/docs-ja/zen-firewall/zen-installation-instructions/zen-firewall-for-python/fastapi.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
