Registry
Aalexbit-codemod avatar

stripe-client-migration

Stripe Legacy to StripeClient Migration Codemod

alexbit-codemod17 executions

  • transformation
  • migration
Connect a repo
npx codemod stripe-client-migration

stripe-client-migration

Stripe Legacy to StripeClient Migration Codemod

This codemod migrates Python codebases from the legacy Stripe SDK usage patterns to the modern StripeClient pattern. It performs the following transformations:

Key Features:
Legacy Pattern Migration: Converts direct stripe.* calls (e.g., stripe.Customer.create()) to client-based calls (e.g., client.customers.create())

Resource Class Migration: Transforms standalone resource class calls (e.g., Customer.create()) to use the client pattern

Import Optimization: Updates import statements to remove the legacy stripe module and ensure StripeClient is imported

Smart Client Management:

Detects existing StripeClient instances and reuses them
Creates a default client when none exists
Handles getter-based client patterns by hoisting them to variables
Getter Pattern Optimization: Transforms repeated getter calls like get_stripe_client().customers.create() into hoisted variables to avoid redundant function calls

Example Transformation:

Before

from stripe import stripe, StripeClient, Customer
customer = stripe.Customer.create(name="John Doe")

After

from stripe import StripeClient, Customer
client = StripeClient('sk_test')
customer = client.customers.create(name="John Doe")

This codemod ensures a clean migration to the recommended StripeClient pattern while maintaining code functionality and improving performance by reducing redundant client instantiations.

Ready to evolve your codebase?

Build tailored, compiler-aware skills to assess, plan, and automate complex codebase changes.