最初の Stripe API リクエストを送信する
Stripe API の使用を開始します。
Stripe API に対するすべてのコールには、API シークレットキーを含める必要があります。Stripe アカウントを作成すると、2 組の API キー (テスト環境用および本番環境用それぞれの、クライアント側の公開可能キーとサーバー側のシークレットキー) が生成されます。本番環境のキーを使用して実際に資金の移動を開始するには、本番環境の利用を申請する必要があります。
はじめに
このガイドでは、Stripe API のシンプルな操作で顧客を作成する方法について手順を追って説明します。Stripe API オブジェクトと、どのように組み合わせるかについては、API の概要をご覧になるか、API リファレンスをご覧ください。支払いの受け付けを開始する準備ができている場合は、クイックスタートをご覧ください。
最初の API リクエストを送信する
Stripe Shell を使用して、Stripe API の探索を開始します。Stripe Shell を使用すると、Stripe ドキュメントサイト内で直接 Stripe CLI コマンドを実行できます。これはサンドボックス環境でのみ動作するため、実際の資金移動取引が行われることについて心配する必要はありません。
Stripe Shell を使用して顧客を作成するには、次のコマンドを入力します。
Command Linestripe customers create --email=jane.smith@email.com --name="Jane Smith" --description="My First Stripe Customer"
すべて正常に機能する場合、コマンドラインに以下のレスポンスが表示されます。
{ "id":
, "object":"cus_LfctGLAICpokzr","customer"(オプション) サンドボックスで API シークレットキーを渡して、同じコマンドを実行します。
Command Linestripe customers create --email=jane.smith@email.com --name="Jane Smith" --description="My First Stripe Customer" --api-key sk_test_BQokikJOvBiI2HlWgH4olfQ2
すべて正常に機能する場合、コマンドラインに以下のレスポンスが表示されます。
{ "id":
, "object":"cus_LfdZgLFhah76qf","customer"
ログとイベントを表示する
API キーを保存する
By default, all accounts have a total of four API keys, two in a sandbox and two in live mode:
- Sandbox secret key: Use this key to authenticate requests on your server when you’re testing in a sandbox. By default, you can use this key to perform any API request without restriction. Reserve this key for testing and development to make sure that you don’t accidentally modify your live customers or charges.
- Sandbox publishable key: Use this key for testing purposes in your web or mobile app’s client-side code. Reserve this key for testing and development to make sure that you don’t accidentally modify your live customers or charges.
- 本番環境のシークレットキー: 本番環境でサーバー上のリクエストを認証するには、このキーを使用します。デフォルトでは、このキーを使用して、あらゆる API リクエストを制限なく実行できます。
- 本番環境の公開可能キー: アプリをリリースする準備が整ったら、ウェブやモバイルアプリのクライアント側コードでこのキーを使用します。
Your secret and publishable keys are in the Dashboard in the API keys tab. If you can’t view your API keys, ask the owner of your Stripe account to add you to their team with the proper permissions.
制限付き API キー
You can generate restricted API keys in the Dashboard to enable customizable and limited access to the API. However, Stripe doesn’t offer any restricted keys by default.
When you’re logged in to Stripe, our documentation automatically populates code examples with your test API keys. Only you can see these values. If you’re not logged in, our code examples include randomly generated API keys. You can replace them with your own test keys or log in to see the code examples populated with your test API keys.
次の表は、ランダムに生成されたシークレットキーと公開可能テスト API キーの例です。
タイプ | 値 | 使用するタイミング |
---|---|---|
シークレット | sk_ | サーバー側: Stripe API を呼び出すウェブやモバイルアプリのサーバー側コード (環境変数や認証情報管理システムなど) でこれを安全に保管し、秘密にする必要があります。このキーをウェブサイトに公開したり、モバイルアプリケーションに埋め込んだりしないでください。 |
公開可能 | pk_ | クライアント側: ウェブやモバイルアプリのクライアント側コード (checkout.js など) でパブリックアクセスが可能であり、Stripe Elements などを使用して決済情報を安全に収集します。デフォルトでは、Stripe Checkout が決済情報を安全に収集します。 |
制限付き | rk_ で始まる文字列 | マイクロサービス: Stripe API を呼び出すマイクロサービスコードでこれを安全に保管し、秘密にする必要があります。このキーをウェブサイトに公開したり、モバイルアプリケーションに埋め込んだりしないでください。 |