8000 Allow passing a callable for `api_key` · Issue #1465 · stripe/stripe-python · GitHub
[go: up one dir, main page]

Skip to content

Allow passing a callable for api_key #1465

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ryancausey opened this issue Mar 5, 2025 · 1 comment
Open

Allow passing a callable for api_key #1465

ryancausey opened this issue Mar 5, 2025 · 1 comment

Comments

@ryancausey
Copy link

Is your feature request related to a problem? Please describe.

I store my Stripe API keys in a secrets store service and retrieve them from this secrets store when I need to call the Stripe API. Given that I need to retrieve the keys from this secrets store, that the keys can be rotated, and that the secrets store access can be revoked as needed, I can't use a one time retrieval of the secret upon instantiation of the StripeClient. This leads me to a pattern where I instantiate the StripeClient with a fake key, and then use the per request options dictionary to specify the API key in each request.

Describe the solution you'd like

I would prefer to be able to pass a callable that returns a string to StripeClient in lieu of passing a string argument to api_key. This callable would then be invoked by the StripeClient on each call that needs it, allowing me to run any custom code I need such as fetching the API key from the secret store.

Describe alternatives you've considered

My current workaround is to instantiate the StripeClient with a fake key, and then specify api_key in the options dictionary on each request.

Additional context

stripe_client = StripeClient(
    api_key="fake_key_on_purpose",
    max_network_retries=5,
    http_client=HTTPXClient(allow_sync_methods=True),
)

# sometime later

stripe_client.products.create(
    params={
        "name": f"{permit_type_name} permit renewal fee",
        "default_price_data": {
            "currency": "USD",
            "recurring": {
                "interval": interval,
                "interval_count": renewal_configuration.interval,
            },
        },
        "unit_amount": renewal_configuration.renewal_fee,
    },
    options={
        "api_key": get_secret_value(secret_id=config("STRIPE_SECRET_ID")),
        "idempotency_key": f"{parsed_input.idempotency_key}-renewal"
    ),
)
@xavdid-stripe
Copy link
Member

That's an interesting use case! I've filed internally as DEVSDK-2419.

I think your current approach makes sense, but if there's enough demand this is something we can look into.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants
0