AWS Lambda から Stripe API を呼び出す

Stripe API 入門ということで、特定の時間に作られた顧客(Customer)のメアドを取ってくるサンプルを作った。 AWS Lambda 上で毎時実行させてみる。

stripe モジュール使いたいので zip にしてアップロードする。

mkdir stripe_api_test_function
cd stripe_api_test_function/
pip3 install -U pip
pip install stripe -t ./
touch lambda_function.py
zip -r stripe_api_test_function.zip ./
aws lambda update-function-code --function-name stripe_api_test_function --zip-file fileb://stripe_api_test_function.zip

Python コード

import stripe

# 本当はこんなところに書くべきではないが記事の本質ではないのでお許しを
stripe.api_key = "***"

def lambda_handler(event, context):
    # 2025/03/12 20:00-21:00 に作られた Customer 情報を取得
    customers = stripe.Customer.search(
       query="created>1741780800 AND created<1741784400"
    )
    for customer in customers:
        email = customer.email
        print(email)

Stripe API 初めて触りましたが、Doc がしっかりしていてとても好みでした。 docs.stripe.com

ちゃんと調べれば、Customer 情報が作られたことをフックに何かする、を Stripe だけで完結できそうな気もする。

Private のコーチング業でも Stripe 使いたいなと思っていたので、もう少し触ってみよう。