Accessing Stripe's Python API

What I’m trying to do:
Access the stripe python api directly, as opposed to anvil’s stripe module, to create a connected account for users of the app can accept payments from their clients.

What I’ve tried and what’s not working:

When I try to call the stripe api, I seem to be accessing anvil’s native library.

code -

def init_stripe(self):
stripe.api_key=anvil.server.call(‘stripe_params’)
print(stripe.get_config())
print(dir(stripe))
connected_acct=stripe.Account.create(type=‘standard’)

output -
{‘publishable_key’: {‘test’:‘pk_test_51N…’, ‘live’:‘pk_live_51N…’}}
[‘doc’, ‘file’, ‘loader’, ‘name’, ‘package’, ‘path’, ‘spec’, ‘_config’, ‘anvil’, ‘api_key’, ‘checkout’, ‘get_config’]
AttributeError: module 'stripe' has no attribute 'Account'

when I run this on my local IDE, this is the stripe directory:

[‘Account’,
‘AccountLink’,
‘ApplePayDomain’,
‘ApplicationFee’,

‘version’,
‘webhook’,
‘webhook_endpoint’]

Any ideas what I am doing wrong?

Are you using the regular anvil ‘Full Python 3’ environment? It looks like the library for stripe installed on it is stripe 2.62.0

On your local IDE, type pip show stripe in your cli of choice running python to find out the version you have locally installed.
(it’s probably newer)

If this is the problem, you can install the newer version by:

As of August 16th, 2023 the latest version is 6.0
image

Thanks ianbuywise, initially I had installed the “latest” version of stripe running full python 3.10. I removed that, force-installed version 6.0.0 and still facing the same problem.

What you’re doing looks nothing like what’s described in the docs for using the Python Stripe API: Anvil Docs | Using Stripe's Python API

Whatever’s happening in yours is happening in a server call that you haven’t shown us, so we can’t really say what might be happening. But the idea is you use Stripe Javascript to take a payment and then pass the token to the server to use the Python Stripe library there to finalize it.

I followed the configuration as the docs said. Then I confirmed the configuration. For some reason the stripe library which I imported is not accessible.

This works with uplink, but would rather not have to use it.

The docs are intended for someone using Anvil’s Stripe Javascript integration with raw=True and then passing tokens to the server to use the Stripe Python API on the server.

You don’t seem to be doing that, hence my question. I can’t tell what you are doing, because you aren’t showing us anything that is actually using the Stripe API. That’s all hidden in a server call.

It may be that the docs linked to don’t work for your use case.

Thank you - makes sense.
The server simply retrieves the stripe.api_key secret, the goal is to create a connected account for the user (not to take a payment, that’s later).

Is there a workaround to get access to the full api? As mentioned, I am running fine with uplink but if I can run everything within the app that would be ideal.

Can you create a minimal clone that shows what you’re trying to do? Having something for people to experiment with usually helps.

You should be able to use the Stripe Python library in any server function as long as it’s installed and imported.

From the stripe docs it looks like you can just set:
stripe.api_key

maybe declaring it as global and changing it in a server function will do what you want?

Came back to this today and figured out the issue. When importing stripe from the client side, the library is limited:

[‘doc’, ‘file’, ‘loader’, ‘name’, ‘package’, ‘path’, ‘spec’, ‘_config’, ‘anvil’, ‘checkout’, ‘get_config’]

when importing the same library from the server, I see the full directory:

[‘Account’, ‘AccountLink’, ‘ApplePayDomain’, ‘ApplicationFee’, ‘ApplicationFeeRefund’, ‘Balance’, ‘BalanceTransaction’, ‘BankAccount’, ‘Capability’, ‘Card’, ‘CashBalance’, ‘Charge’, ‘CountrySpec’, ‘Coupon’, ‘CreditNote’, ‘CreditNoteLineItem’, ‘Customer’, ‘CustomerBalanceTransaction’, ‘CustomerCashBalanceTransaction’, ‘Dispute’, ‘EphemeralKey’, ‘ErrorObject’, ‘Event’, ‘ExchangeRate’, ‘File’, ‘FileLink’, ‘FileUpload’, ‘FundingInstructions’, ‘Invoice’, ‘InvoiceItem’, ‘InvoiceLineItem’, ‘LineItem’, ‘ListObject’, ‘LoginLink’, ‘Mandate’, ‘OAuth’, ‘OAuthErrorObject’, ‘PaymentIntent’, ‘PaymentLink’, ‘PaymentMethod’, ‘Payout’, ‘Person’, ‘Plan’, ‘Price’, ‘Product’, ‘PromotionCode’, ‘Quote’, ‘Refund’, ‘Reversal’, ‘Review’, ‘SearchResultObject’, ‘SetupAttempt’, ‘SetupIntent’, ‘ShippingRate’, ‘Source’, ‘SourceTransaction’, ‘Subscription’, ‘SubscriptionItem’, ‘SubscriptionSchedule’, ‘TaxCode’, ‘TaxId’, ‘TaxRate’, ‘Token’, ‘Topup’, ‘Transfer’, ‘UsageRecord’, ‘UsageRecordSummary’, ‘Webhook’, ‘WebhookEndpoint’, ‘WebhookSignature’, ‘_ApiVersion’, ‘builtins’, ‘cached’, ‘doc’, ‘file’, ‘loader’, ‘name’, ‘package’, ‘path’, ‘spec’, ‘absolute_import’, ‘abstract’, ‘account’, ‘account_link’, ‘api_base’, ‘api_key’, ‘api_requestor’, ‘api_resources’, ‘api_version’, ‘app_info’, ‘apple_pay_domain’, ‘application_fee’, ‘application_fee_refund’, ‘apps’, ‘balance’, ‘balance_transaction’, ‘bank_account’, ‘billing_portal’, ‘ca_bundle_path’, ‘capability’, ‘card’, ‘cash_balance’, ‘charge’, ‘checkout’, ‘client_id’, ‘connect_api_base’, ‘country_spec’, ‘coupon’, ‘credit_note’, ‘credit_note_line_item’, ‘customer’, ‘customer_balance_transaction’, ‘customer_cash_balance_transaction’, ‘default_http_client’, ‘dispute’, ‘division’, ‘enable_telemetry’, ‘ephemeral_key’, ‘error’, ‘error_object’, ‘event’, ‘exchange_rate’, ‘file’, ‘file_link’, ‘financial_connections’, ‘funding_instructions’, ‘http_client’, ‘identity’, ‘invoice’, ‘invoice_item’, ‘invoice_line_item’, ‘issuing’, ‘line_item’, ‘list_object’, ‘log’, ‘login_link’, ‘mandate’, ‘max_network_retries’, ‘multipart_data_generator’, ‘oauth’, ‘oauth_error’, ‘os’, ‘payment_intent’, ‘payment_link’, ‘payment_method’, ‘payout’, ‘person’, ‘plan’, ‘price’, ‘print_function’, ‘product’, ‘promotion_code’, ‘proxy’, ‘quote’, ‘radar’, ‘refund’, ‘reporting’, ‘request_metrics’, ‘reversal’, ‘review’, ‘search_result_object’, ‘set_app_info’, ‘setup_attempt’, ‘setup_intent’, ‘shipping_rate’, ‘sigma’, ‘source’, ‘source_transaction’, ‘stripe_object’, ‘stripe_response’, ‘subscription’, ‘subscription_item’, ‘subscription_schedule’, ‘tax’, ‘tax_code’, ‘tax_id’, ‘tax_rate’, ‘terminal’, ‘test_helpers’, ‘token’, ‘topup’, ‘transfer’, ‘treasury’, ‘upload_api_base’, ‘usage_record’, ‘usage_record_summary’, ‘util’, ‘verify_ssl_certs’, ‘version’, ‘webhook’, ‘webhook_endpoint’]