Skip to content

Stripe

See every call your app makes to Stripe (the payment platform’s API) as a span (one timed step, with a name and a duration) in Logfire, with how long it took, its status, and any errors.

The Stripe Python client sends these calls over HTTP, so you instrument it by instrumenting the HTTP library it uses under the hood: there’s no separate Stripe extra to install. By default the client uses the requests package for synchronous calls and the httpx package for asynchronous calls, so which Logfire function you call depends on which style you use.

What you’ll capture

  • Each Stripe API call as a span, with its duration and status
  • Any errors returned by Stripe
  • Optionally, Stripe’s own log messages (see Advanced)

Before you start

You’ll need a Logfire project. Open Add data in your project (top navigation) and follow the setup for your language: it signs your machine in with logfire auth (a browser sign-in, no token to copy) and, for production or other languages, creates a write token (the credential your app uses to send data). New to Logfire? Start with Getting Started.

You’ll also need a Stripe secret key, from your Stripe dashboard. The examples read it from the STRIPE_SECRET_KEY environment variable.

Installation

Install logfire. No Stripe-specific extra is needed:

Terminal
pip install logfire

This works with your existing stripe package. If you don’t have it yet, pip install stripe.

Usage

Call logfire.configure() to connect to your project, then instrument the HTTP library the Stripe client uses.

For synchronous calls (the default), the client uses requests, so call logfire.instrument_requests():

import os

from stripe import StripeClient

import logfire

logfire.configure()
logfire.instrument_requests()

client = StripeClient(api_key=os.getenv('STRIPE_SECRET_KEY'))

client.customers.list()

For asynchronous calls (methods ending in _async), the client uses httpx, so call logfire.instrument_httpx():

import asyncio
import os

from stripe import StripeClient

import logfire

logfire.configure()
logfire.instrument_httpx()  # for asynchronous requests

client = StripeClient(api_key=os.getenv('STRIPE_SECRET_KEY'))


async def main():
    with logfire.span('list async'):
        await client.customers.list_async()


if __name__ == '__main__':
    asyncio.run(main())

Verify it worked

Run your program, then open the Live view. Within a few seconds you’ll see a span for the HTTP call to Stripe, with its duration and status.

Troubleshooting

Not seeing your Stripe calls in Logfire? Check that logfire.configure() ran before the instrument_* call, that your write token is set, that you instrumented the HTTP library your calls actually use (requests for synchronous, httpx for asynchronous), and that your STRIPE_SECRET_KEY is set so the call succeeds.

Advanced

Add Stripe’s log messages

Stripe has its own logger (getLogger('stripe')) that you can route to Logfire. This adds Stripe’s internal log lines alongside the request spans:

import os
from logging import basicConfig

from stripe import StripeClient

import logfire

logfire.configure()
basicConfig(handlers=[logfire.LogfireLoggingHandler()], level='INFO')

client = StripeClient(api_key=os.getenv('STRIPE_SECRET_KEY'))

client.customers.list()

Change level='INFO' to level='DEBUG' to see more detail, including the response body. Note that DEBUG level can include sensitive data, so use it with care.

Reference