To use Stripe’s Python API, you’ll need to do two things:
Configure your Stripe Secret Key in your Anvil app. Use the App Secrets service to store it, rather than leaving it in your source code!
Enter your Publishable Keys into Anvil’s Stripe configuration page.
The steps below show you how to do this in Anvil
To use the official API, you will need to provide Anvil with your Secret Key and your Publishable Keys.
The steps below will walk you through how to access your keys and how to configure them inside Anvil.
Accessing your Stripe API Keys
First, navigate to your Stripe Dashboard
In the navigation pane on the left, click on ‘Developers’, then click on ‘API keys’:
You’ll see a list of ‘Standard Keys’ which includes both your Publishable Key and your Secret key:
You also have the option to view either your live keys or your test keys using the toggle:
Adding your publishable keys to Anvil
Select ‘Stripe’ from the App Browser, and enter your Test and Live publishable keys in the boxes provided:
Adding your secret key to Anvil
Use the App Secrets service to store your Secret key, and then add the key to your app.
For example, if you store your secret key using Anvil’s App Secrets service as ‘stripe_key’, add these lines to provide Stripe with your secret key:
import stripe stripe.api_key = anvil.secrets.get_secret('stripe_key')
Once you have configured both your publishable and secret keys, you can use Stripe’s Python API.
To use the official API, you will still need to collect users’ card details and generate Stripe tokens. In your Form code, you can call
stripe.checkout.get_token() with the
raw=True parameter. This will generate a token that you can use with the Stripe Python API.
For example, to take a payment:
# Use Stripe Checkout to generate a raw token token, user_info = stripe.checkout.get_token( currency='GBP', amount=999, title='Payment for your Anvil', raw=True ) anvil.server.call('charge', token)
import stripe stripe.api_key = anvil.secrets.get_secret('stripe_key') @anvil.server.callable def charge(token): charge = stripe.Charge.create( currency='GBP', amount=999, description='Payment for your Anvil', source=token )