Go Stripe
The official [Stripe][stripe] Go client library.
Requirements
- Go 1.15 or later
Documentation
For a comprehensive list of examples, check out the [API documentation][api-docs].
See [video demonstrations][youtube-playlist] covering how to use the library.
For details on all the functionality in this library, see the [Go documentation][goref].
Below are a few simple examples:
Customers
params := &stripe.CustomerParams{
Description: stripe.String("Stripe Developer"),
Email: stripe.String("gostripe@stripe.com"),
PreferredLocales: stripe.StringSlice([]string{"en", "es"}),
}
c, err := customer.New(params)
PaymentIntents
params := &stripe.PaymentIntentListParams{
Customer: stripe.String(customer.ID),
}
i := paymentintent.List(params)
for i.Next() {
pi := i.PaymentIntent()
}
if err := i.Err(); err != nil {
// handle
}
Events
i := event.List(nil)
for i.Next() {
e := i.Event()
// access event data via e.GetObjectValue("resource_name_based_on_type", "resource_property_name")
// alternatively you can access values via e.Data.Object["resource_name_based_on_type"].(map[string]interface{})["resource_property_name"]
// access previous attributes via e.GetPreviousValue("resource_name_based_on_type", "resource_property_name")
// alternatively you can access values via e.Data.PrevPreviousAttributes["resource_name_based_on_type"].(map[string]interface{})["resource_property_name"]
}
Alternatively, you can use the event.Data.Raw
property to unmarshal to the
appropriate struct.
Authentication with Connect
There are two ways of authenticating requests when performing actions on behalf
of a connected account, one that uses the Stripe-Account
header containing an
account’s ID, and one that uses the account’s keys. Usually the former is the
recommended approach. [See the documentation for more information][connect].
To use the Stripe-Account
approach, use SetStripeAccount()
on a ListParams
or Params
class. For example:
// For a list request
listParams := &stripe.CustomerListParams{}
listParams.SetStripeAccount("acct_123")
// For any other kind of request
params := &stripe.CustomerParams{}
params.SetStripeAccount("acct_123")
To use a key, pass it to API
’s Init
function:
import (
"github.com/stripe/stripe-go/v79"
"github.com/stripe/stripe-go/v79/client"
)
stripe := &client.API{}
stripe.Init("access_token", nil)
Google AppEngine
If you’re running the client in a Google AppEngine environment, you’ll need to
create a per-request Stripe client since the http.DefaultClient
is not
available. Here’s a sample handler:
import (
"fmt"
"net/http"
"google.golang.org/appengine"
"google.golang.org/appengine/urlfetch"
"github.com/stripe/stripe-go/v79"
"github.com/stripe/stripe-go/v79/client"
)
func handler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
httpClient := urlfetch.Client(c)
sc := client.New("sk_test_123", stripe.NewBackends(httpClient))
params := &stripe.CustomerParams{
Description: stripe.String("Stripe Developer"),
Email: stripe.String("gostripe@stripe.com"),
}
customer, err := sc.Customers.New(params)
if err != nil {
fmt.Fprintf(w, "Could not create customer: %v", err)
}
fmt.Fprintf(w, "Customer created: %v", customer.ID)
}
Without a Client
If you’re only dealing with a single key, you can simply import the packages required for the resources you’re interacting with without the need to create a client.
import (
"github.com/stripe/stripe-go/v79"
"github.com/stripe/stripe-go/v79/$resource$"
)
// Setup
stripe.Key = "sk_key"
// Set backend (optional, useful for mocking)
// stripe.SetBackend("api", backend)
// Create
resource, err := $resource$.New(&stripe.$Resource$Params{})
// Get
resource, err = $resource$.Get(id, &stripe.$Resource$Params{})
// Update
resource, err = $resource$.Update(id, &stripe.$Resource$Params{})
// Delete
resourceDeleted, err := $resource$.Del(id, &stripe.$Resource$Params{})
// List
i := $resource$.List(&stripe.$Resource$ListParams{})
for i.Next() {
resource := i.$Resource$()
}
if err := i.Err(); err != nil {
// handle
}
With a Client
If you’re dealing with multiple keys, it is recommended you use client.API
.
This allows you to create as many clients as needed, each with their own
individual key.
import (
"github.com/stripe/stripe-go/v79"
"github.com/stripe/stripe-go/v79/client"
)
// Setup
sc := &client.API{}
sc.Init("sk_key", nil) // the second parameter overrides the backends used if needed for mocking
// Create
$resource$, err := sc.$Resource$s.New(&stripe.$Resource$Params{})
// Get
$resource$, err = sc.$Resource$s.Get(id, &stripe.$Resource$Params{})
// Update
$resource$, err = sc.$Resource$s.Update(id, &stripe.$Resource$Params{})
// Delete
$resource$Deleted, err := sc.$Resource$s.Del(id, &stripe.$Resource$Params{})
// List
i := sc.$Resource$s.List(&stripe.$Resource$ListParams{})
for i.Next() {
$resource$ := i.$Resource$()
}
if err := i.Err(); err != nil {
// handle
}
Accessing the Last Response
Use LastResponse
on any APIResource
to look at the API response that
generated the current object:
c, err := coupon.New(...)
requestID := coupon.LastResponse.RequestID
Similarly, for List
operations, the last response is available on the list
object attached to the iterator:
it := coupon.List(...)
for it.Next() {
// Last response *NOT* on the individual iterator object
// it.Coupon().LastResponse // wrong
// But rather on the list object, also accessible through the iterator
requestID := it.CouponList().LastResponse.RequestID
}
See the definition of [APIResponse
][apiresponse] for available fields.
Note that where API resources are nested in other API resources, only
LastResponse
on the top-level resource is set.
Automatic Retries
The library automatically retries requests on intermittent failures like on a
connection error, timeout, or on certain API responses like a status 409
Conflict
. [Idempotency keys][idempotency-keys] are always added to requests to
make any such subsequent retries safe.
By default, it will perform up to two retries. That number can be configured
with MaxNetworkRetries
:
import (
"github.com/stripe/stripe-go/v79"
"github.com/stripe/stripe-go/v79/client"
)
config := &stripe.BackendConfig{
MaxNetworkRetries: stripe.Int64(0), // Zero retries
}
sc := &client.API{}
sc.Init("sk_key", &stripe.Backends{
API: stripe.GetBackendWithConfig(stripe.APIBackend, config),
Uploads: stripe.GetBackendWithConfig(stripe.UploadsBackend, config),
})
coupon, err := sc.Coupons.New(...)
Expanding Objects
All [expandable objects][expandableobjects] in stripe-go take the form of a
full resource struct, but unless expansion is requested, only the ID
field of
that struct is populated. Expansion is requested by calling AddExpand
on
parameter structs. For example:
//
// *Without* expansion
//
c, _ := charge.Get("ch_123", nil)
c.Customer.ID // Only ID is populated
c.Customer.Name // All other fields are always empty
//
// With expansion
//
p := &stripe.ChargeParams{}
p.AddExpand("customer")
c, _ = charge.Get("ch_123", p)
c.Customer.ID // ID is still available
c.Customer.Name // Name is now also available (if it had a value)
Parameters
To pass undocumented parameters to Stripe using stripe-go you need to use the AddExtra()
method, as shown below:
params := &stripe.CustomerParams{
Email: stripe.String("jenny.rosen@example.com")
}
params.AddExtra("secret_feature_enabled", "true")
params.AddExtra("secret_parameter[primary]","primary value")
params.AddExtra("secret_parameter[secondary]","secondary value")
customer, err := customer.Create(params)
Properties
You can access undocumented properties returned by Stripe by querying the raw response JSON object. An example of this is shown below:
customer, _ = customer.Get("cus_1234", nil);
var rawData map[string]interface{}
_ = json.Unmarshal(customer.LastResponse.RawJSON, &rawData)
secret_feature_enabled, _ := string(rawData["secret_feature_enabled"].(bool))
secret_parameter, ok := rawData["secret_parameter"].(map[string]interface{})
if ok {
primary := secret_parameter["primary"].(string)
secondary := secret_parameter["secondary"].(string)
}
Webhook signing
Stripe can optionally sign the webhook events it sends to your endpoint, allowing you to validate that they were not sent by a third-party. You can read more about it here.
Testing Webhook signing
You can use stripe.webhook.GenerateTestSignedPayload
to mock webhook events that come from Stripe:
payload := map[string]interface{}{
"id": "evt_test_webhook",
"object": "event",
"api_version": stripe.APIVersion,
}
testSecret := "whsec_test_secret"
payloadBytes, err := json.Marshal(payload)
signedPayload := webhook.GenerateTestSignedPayload(&webhook.UnsignedPayload{Payload: payloadBytes, Secret: testSecret})
event, err := webhook.ConstructEvent(signedPayload.Payload, signedPayload.Header, signedPayload.Secret)
if event.ID == payload["id"] {
// Do something with the mocked signed event
} else {
// Handle invalid event payload
}
Writing a Plugin
If you’re writing a plugin that uses the library, we’d appreciate it if you
identified using stripe.SetAppInfo
:
stripe.SetAppInfo(&stripe.AppInfo{
Name: "MyAwesomePlugin",
URL: "https://myawesomeplugin.info",
Version: "1.2.34",
})
This information is passed along when the library makes calls to the Stripe
API. Note that while Name
is always required, URL
and Version
are
optional.
Mocking clients for unit tests
To mock a Stripe client for a unit tests using GoMock:
- Generate a
Backend
type mock.
mockgen -destination=mocks/backend.go -package=mocks github.com/stripe/stripe-go/v79 Backend
- Use the
Backend
mock to initialize and call methods on the client.
import (
"example/hello/mocks"
"testing"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stripe/stripe-go/v79"
"github.com/stripe/stripe-go/v79/account"
)
func UseMockedStripeClient(t *testing.T) {
// Create a mock controller
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
// Create a mock stripe backend
mockBackend := mocks.NewMockBackend(mockCtrl)
client := account.Client{B: mockBackend, Key: "key_123"}
// Set up a mock call
mockBackend.EXPECT().Call("GET", "/v1/accounts/acc_123", gomock.Any(), gomock.Any(), gomock.Any()).
// Return nil error
Return(nil).
Do(func(method string, path string, key string, params stripe.ParamsContainer, v *stripe.Account) {
// Set the return value for the method
*v = stripe.Account{
ID: "acc_123",
}
}).Times(1)
// Call the client method
acc, _ := client.GetByID("acc_123", nil)
// Asset the result
assert.Equal(t, acc.ID, "acc_123")
}
Beta SDKs
Stripe has features in the beta phase that can be accessed via the beta version of this package.
We would love for you to try these and share feedback with us before these features reach the stable phase.
To install a beta version of stripe-go use the commit notation of the go get
command to point to a beta tag:
go get -u github.com/stripe/stripe-go/v79@v77.1.0-beta.1
Note There can be breaking changes between beta versions.
We highly recommend keeping an eye on when the beta feature you are interested in goes from beta to stable so that you can move from using a beta version of the SDK to the stable version.
If your beta feature requires a Stripe-Version
header to be sent, set the stripe.APIVersion
field using the stripe.AddBetaVersion
function to set it:
Note The
APIVersion
can only be set in beta versions of the library.
stripe.AddBetaVersion("feature_beta", "v3")
Development
Pull requests from the community are welcome. If you submit one, please keep the following guidelines in mind:
- Code must be
go fmt
compliant. - All types, structs and funcs should be documented.
- Ensure that
make test
succeeds.
vim: set tw=79:
–>