-
Notifications
You must be signed in to change notification settings - Fork 477
Migration guide for Stripe Client
Starting with v82.1, the new stripe.Client
type is replacing client.API
to provide a more ergonomic, consistent, and less error-prone experience. You create the former using stripe.NewClient(stripeKey)
. It’s almost a drop-in replacement, except for the differences listed below.
- Service method names now align with Stripe API docs. The
stripe.Client
usesCreate
,Retrieve
,Update
, andDelete
(instead ofNew
,Get
,Update
, andDel
). - The first argument of each service method is a
context.Context
. - Parameter objects are now method-specific. For example,
CustomerCreateParams
andCustomerDeleteParams
instead of simplyCustomerParams
. This allows us to put the right fields in the right methods at compile time. - Services are all version-namespaced for symmetry. E.g.
stripeClient.V1Accounts
andstripeClient.V2Accounts
. -
List
methods return aniter.Seq2
, so they can be ranged over without explicit calls toNext
,Current
, andErr
.
The new stripe.Client
is almost, but not quite, a drop-in replacement for client.API
. You create a new client using stripe.NewClient(apiKey)
. You can replace client.API
construction with stripe.Client
using this regex:
/client.New\((.*), nil\)/stripe.NewClient($1)/
Before
sc := client.New(env().StripeKey, nil)
After
sc := stripe.Client(env().StripeKey)
If you are passing in your own stripe.Backends
into client.New
, you pass them into the stripe.Client
as follows
sc := stripe.NewClient(stripeKey, stripe.WithBackends(backends))
CRUD methods are now called Create
, Retrieve
, Update
, and Delete
Old | New |
---|---|
|
|
|
|
|
|
|
|
OLD
i := sc.Customers.List(&stripe.CustomerListParams{})
for i.Next() {
cust := i.Customer()
// handle cust
}
if err := i.Err(); err != nil {
// handle err
}
NEW
params := &stripe.CustomerListParams{}
for cust, err := range sc.V1Customers.List(context.TODO(), params) {
// handle err
// handle cust
}
All params are now method-specific. This prevents runtime errors where you pass the wrong params to the Stripe API.
Old
params := &stripe.CustomerParams{Name: stripe.String("Jenny Rosen")}
params.Context = context.TODO()
sc.Customers.Del("cus_123", params) // ❌ -- runtime error from Stripe: Name param not allowed
The general pattern is:
- Old:
{resource_name}{subresource_names?}Params
- New:
{resource_name}{method_name}{subresource_names?}Params
Old
params := &stripe.AppsSecretParams{
Name: stripe.String("sec_123"),
Payload: stripe.String("very secret string"),
Scope: &stripe.AppsSecretScopeParams{
Type: stripe.String(stripe.AppsSecretScopeTypeAccount),
},
}
New
params := &stripe.AppsSecretCreateParams{
Name: stripe.String("sec_123"),
Payload: stripe.String("very secret string"),
Scope: &stripe.AppsSecretCreateScopeParams{
Type: stripe.String(stripe.AppsSecretScopeTypeAccount),
},
}