# Stripe CLI を使用する コマンドラインから Stripe 統合をビルド、テスト、管理します。 詳細については、[Stripe CLI リファレンス](https://docs.stripe.com/cli.md)を参照してください。 Stripe CLI を使用すると、コマンドラインから Stripe 統合をビルド、テスト、管理できます。CLI を使用すると、Stripe リソースの作成と管理、Webhook イベントのトリガー、リアルタイム API リクエストログのストリーミング、ローカル開発環境へのイベントの転送を行うことができます。 ## リクエストの実行時に API バージョンを指定する CLI で Stripe API を呼び出すと、すべてのリクエストでデフォルトの API バージョンが使用されます。これは [Workbench で識別](https://docs.stripe.com/workbench/guides.md#view-api-versions)できます。CLI でさまざまな API バージョンを試すには、次のフラグを使用します。 | フラグ | Description | 例 | | ------------------------------------ | ------------------------------------------------------- | ------------------------------------------------------------------------------- | | `--stripe-version 2026-03-25.dahlia` | CLI リクエストで `--stripe-version` フラグを使用して、API バージョンを指定します。 | `stripe products create --name=“My Product” --stripe-version 2026-03-25.dahlia` | | `--latest` | CLI リクエストで `--latest` フラグを使用して、最新の API バージョンを指定します。 | `stripe products create --name="My Product" --latest` | また、[API versions のリスト](https://docs.stripe.com/upgrades.md#api-versions)を表示することもできます。 ## コマンドオプションについて 特定のコマンドのオプションを確認するには、コマンドに `--help` を追加します。 ```bash stripe listen --help ``` ## リクエストログをストリーミングする `stripe logs tail` コマンドを使用して API リクエストログをストリーミングします。このウィンドウは開いたままにしておいてください。API コールでエラーが発生すると、この端末から API エラーメッセージとエラーの理由が返されます。 ```bash stripe logs tail ``` ## ローカルの Webhook エンドポイントにイベントを転送する `--forward-to` フラグを使用して、**サンドボックス** 内のすべての [Stripe イベント](https://docs.stripe.com/cli/trigger#trigger-event) をローカル Webhook エンドポイントに送信します。HTTPS 証明書の検証を無効にするには、`--skip-verify` フラグを使用します。 ```bash stripe listen --forward-to localhost:4242/webhooks ``` ```bash Ready! Your webhook signing secret is '{{WEBHOOK_SIGNING_SECRET}}' (^C to quit) ``` 特定のイベントをカンマ区切りのリストで転送するには、`--events` フラグを使用します。 ```bash stripe listen --events payment_intent.created,customer.created,payment_intent.succeeded,charge.succeeded,checkout.session.completed,charge.failed \ --forward-to localhost:4242/webhook ``` すでに [Stripe にエンドポイントを登録している](https://docs.stripe.com/webhooks.md#register-webhook)場合は、`--load-from-webhooks-api` と `--forward-to` フラグを使用できます。 ```bash stripe listen --load-from-webhooks-api --forward-to localhost:4242 ``` このコマンドは、Stripe に登録された **public** Webhook エンドポイントに送信されたイベントを、**local** Webhook エンドポイントに転送します。登録されたエンドポイントを読み込み、パスとその登録イベントを解析し、次にそのパスを `--forward-to` パス内のローカル Webhook エンドポイントに追加します。Webhook 署名を確認している場合は、`listen` コマンドの初期出力から `{{WEBHOOK_SIGNING_SECRET}}` を使用します。 ## 利用可能なすべてのイベントを表示する [help flag](https://docs.stripe.com/cli/help) (`--help`) を使用して、イベントカテゴリで発生する可能性のあるすべてのイベントを一覧表示します。たとえば、[Stripe Checkout](https://docs.stripe.com/payments/checkout.md) の [事前構築済みチェックアウトページ](https://docs.stripe.com/checkout/quickstart.md) のすべての可能なイベントを一覧表示するには: ```bash stripe trigger checkout --help ``` ## 1 回限りの商品と価格を作成する 1. [Create a product](https://docs.stripe.com/api/products/create.md) への単一の API リクエストを作成します。 ```bash stripe products create \ --name="My First Product" \ --description="Created with the Stripe CLI" ``` 1. レスポンスオブジェクト内の商品識別子 (`id`) を探してください。次のステップのために保存してください。すべてが正常に機能した場合、コマンドラインには次のレスポンスが表示されます。 ```json { "id": "prod_LTenIrmp8Q67sa", // ID は次のようになります。 "object": "product", "active": true, "attributes": [], "created": 1668198126, "default_price": null, "description": "Created with the Stripe CLI", "identifiers": {}, "images": [], "livemode": false, "metadata": {}, "name": "My First Product", "package_dimensions": null, "price": null, "product_class": null, "shippable": null, "sku": "my-first-product-10", "statement_descriptor": null, "tax_code": null, "type": "service", "unit_label": null, "updated": 1668198126, "url": null } ``` 1. [Create a price](https://docs.stripe.com/api/prices/create.md) を呼び出して 30 USD の価格を設定します。`product` のプレースホルダーを商品 ID (例: `prod_LTenIrmp8Q67sa`) に置き換えてください。 ```bash stripe prices create \ --unit-amount=3000 \ --currency=usd \ --product="{{PRODUCT_ID}}" ``` 1. すべて正常に機能する場合、コマンドラインに以下のレスポンスが表示されます。 ```json { "id": "price_1KzlAMJJDeE9fu01WMJJr79o", // ID は次のようになります。 "object": "price", "active": true, "billing_scheme": "per_unit", "created": 1652636348, "currency": "usd", "livemode": false, "lookup_key": null, "metadata": {}, "nickname": null, "product": "prod_Lh9iTGZhb2mcBy", "recurring": null, "tax_behavior": "unspecified", "tiers_mode": null, "transform_quantity": null, "type": "one_time", "unit_amount": 3000, "unit_amount_decimal": "3000" } ``` ## テスト中に Webhook イベントをトリガーする `checkout.session.completed` イベントをトリガーし、チェックアウトセッションが正常に完了したときに生成される API オブジェクトを作成します。 ```bash stripe trigger checkout.session.completed ``` `stripe listen` 端末には、以下の出力が表示されます。 ```bash Setting up fixture for: checkout_session Running fixture for: checkout_session Setting up fixture for: payment_page Running fixture for: payment_page Setting up fixture for: payment_method Running fixture for: payment_method Setting up fixture for: payment_page_confirm Running fixture for: payment_page_confirm Trigger succeeded! ```