Blog
Stripe CLIで接続したStripeアカウントの情報(APIキーなど)を確認する方法
Stripe CLIを利用すると、Stripe内の情報の取得や操作だけでなく、WebhookイベントをローカルのAPIにProxyしたりもできます。
ただし、複数のStripeアカウントを運用していると、「あれ、このproject-nameってどこのアカウントだっけ?」とか「そもそもどんなproject-name設定していたっけ?」となることがあります。
ということで、今回はStripe CLIが参照しているAPIキーなどの情報を見る方法を紹介します。
stripe loginでStripeアカウントと接続する
Stripe CLIからStripeのデータ(API)へアクセスするために、stripe loginコマンドを実行します。
--project-nameオプションをつけることで、複数のStripeアカウントを持っている場合の使い分けが可能になります。
% stripe login --project-name project-1
Your pairing code is: defeat-nicer-catchy-top
This pairing code verifies your authentication with Stripe.
Press Enter to open the browser (^C to quit)
ブラウザを開いて、アクセス許可の設定を行えば完了です。
作成されたアクセス情報は$HOME/.config/stripe/config.tomlで確認できる
Stripe CLIが利用するAPIキーなどは、$HOME/.config/stripe/config.tomlに追加されます。
$ cat $HOME/.config/stripe/config.toml
color = ""
[project-1]
device_name = "okamotoakanombp.lan"
display_name = "project-1"
live_mode_api_key = "rk_live_xxxxxxxxxxxxx"
live_mode_publishable_key = "pk_live_xxxxxxxxxxxxx"
test_mode_api_key = "rk_test_xxxxxxxxxxxxx"
test_mode_publishable_key = "pk_test_exxxxxxxxxxxxx"
config.tomlを個別で用意することも可能
Stripe CLIには--configオプションがあり、TOMLのパスを指定することができます。
これを利用して、stripe loginせずにStripe CLIを利用することも可能です。
$ stripe logs tail --project-name project-1 --config ./custom.toml
とはいえ、rk_から始まるAPIキーはほぼ全てのデータに対してREADの権限を持っています。
そのためGitやSVNにでコミットしてpublicな場に出してしまうと、そのキーを利用して顧客情報が漏洩する恐れがありますので要注意です。
もしCircle CIなどのCIやAWS Batchなどで利用したい場合は、利用するリソースに限定したAPIキーを別途用意するなど、本番の顧客情報にアクセスできる可能性を潰すようにしましょう。
環境変数STRIPE_API_KEYに注意
ローカル開発時に環境変数でSTRIPE_API_KEYを設定している場合があります。この記事を書いている時点のバージョン(1.5.9)では、stripe loginで作成されたデータよりも、環境変数の値を優先する挙動がある様子でした。
% stripe logs tail --project-name project-1
[Thu, 25 Feb 2021 16:10:47 JST] FATAL Error while authenticating with Stripe: Authorization failed, status=403, body={
"error": {
"message": "The provided key 'rk_test_v4ulUb' does not have the required permissions for this endpoint on account 'acct_xxxxxxxx'. Having the 'rak_stripecli_session_write' permission would allow this request to continue.",
"type": "invalid_request_error"
}
}
Tools to Support Stripe Development
We provide helpful tools to extend the Stripe Dashboard and streamline development and testing.
View All ToolsRelated Articles
Support This Project
If you find this content helpful, consider supporting the project through GitHub Sponsors. Your support helps maintain and improve these tools.
