You are currently viewing the new Anvil Editor Docs.
Switch to the Classic Editor Docs
You are currently viewing the Classic Editor Docs.
Switch to the new Anvil Editor Docs

Using Stripe’s Python API

If you’re using the Full Python runtimes, you can choose not to use Anvil’s simplified APIs, and instead use the official Stripe Python API from your Server Modules.

To use Stripe’s Python API, you’ll need to do two things:

  1. 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!

  2. Enter your Publishable Keys into Anvil’s Stripe configuration page.

The steps below show you how to do this in Anvil

Configuring your Stripe API keys

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.

To learn more about Stripe’s API Keys, how to manage your keys and when you should use Test and Live modes, see the Stripe docs here

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:

After adding the ‘Stripe’ service by clicking the blue plus button +, select ‘Stripe’ from the Sidebar Menu. Add 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')

Taking a payment

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
  )

Do you still have questions?

Our Community Forum is full of helpful information and Anvil experts.