Development environment quickstart in Go
Follow this quickstart to give Stripe a try and understand what you can and can’t do with Stripe APIs. Stripe’s SDKs provide high-level API abstractions that simplify interacting with Stripe’s platform by reducing the boilerplate code required. Install the Stripe CLI and Go SDK to setup a local development environment in Go.
Initial setup
- Click the Sign in button (in the upper right) to login to the Stripe docs site, and start using your test mode API keys for authenticated requests.
Setup the Stripe CLI
The Stripe CLI provides command-line access to many common tasks like calling Stripe APIs, testing your webhooks integration, and creating an application.
- From the command-line, use an install script or download and extract a versioned archive file for your operating system to install the CLI.
- Login and authenticate your Stripe user Account to generate a set of restricted keys. To learn more, see Stripe CLI keys and permissions.
stripe login
- Press the
Enter
key on your keyboard to complete the authentication process in your browser.
Your pairing code is: enjoy-enough-outwit-win This pairing code verifies your authentication with Stripe. Press Enter to open the browser or visit https://dashboard.stripe.com/stripecli/confirm_auth?t=THQdJfL3x12udFkNorJL8OF1iFlN8Az1 (^C to quit)
- Now that you’ve installed the CLI, you can make a single API request to Create a product.
stripe products create \ --name="My First Product" \ --description="Created with the Stripe CLI"
- Look for the product identifier (in
id
) in the response object. Save it for the next step.
If everything worked, the command-line displays the following response.
{ "id":
, "object": "product","prod_LTenIrmp8Q67sa"
- Next, call Create a price to attach a price of 30 USD. Swap the placeholder in
product
with your product identifier (for example,prod_LTenIrmp8Q67sa
).
stripe prices create \ --unit-amount=3000 \ --currency=usd \ --product=
"{{PRODUCT_ID}}"
If everything worked, the command-line displays the following response.
{ "id":
, "object": "price","price_1KzlAMJJDeE9fu01WMJJr79o"
Manage third-party dependencies
We recommend managing third-party dependencies using Go modules, which allows you to add new libraries and include them in your Go projects. To initialize a project and create a go.mod
file for your dependencies:
Initialize Go
go mod init
Install the Go Server-side SDK
The Stripe Go SDK provides access to Stripe APIs from applications written in Go. The SDK supports accessing Stripe APIs in a way that significantly reduces the boilerplate code you have to write. Stripe’s Go server-side SDK supports all Go versions.
Install the library
- We recommend installing the library with Go modules, a package manager for Go::
go get github.com/stripe/stripe-go/v72
Run your first SDK request
Now that you have the Go SDK installed, you can create a subscription Product and attach a Price with a couple API requests. We’re using the product identifier returned in the response to create the price in this example.
package main import ( "fmt" "github.com/stripe/stripe-go/v72" "github.com/stripe/stripe-go/v72/product" "github.com/stripe/stripe-go/v72/price" ) func main() { stripe.Key = "sk_test_Hrs6SAopgFPF0bZXSN3f6ELN" product_params := &stripe.ProductParams{ Name: stripe.String("Starter Subscription"), Description: stripe.String("$12/Month subscription"), } starter_product, _ := product.New(product_params) price_params := &stripe.PriceParams{ Currency: stripe.String(string(stripe.CurrencyUSD)), Product: stripe.String(starter_product.ID), Recurring: &stripe.PriceRecurringParams{ Interval: stripe.String(string(stripe.PriceRecurringIntervalMonth)), }, UnitAmount: stripe.Int64(12000), } starter_price, _ := price.New(price_params) fmt.Println("Success! Here is your starter subscription product id: " + starter_product.ID) fmt.Println("Success! Here is your starter subscription price id: " + starter_price.ID) }
- Save the file as
create_price.go
. From the command-line,cd
to the directory containing the file you just saved then run:
go run create_price.go
If everything worked, the command-line shows the following response. Save these identifiers so you can use them while building your integration.
Success! Here is your starter subscription product id: price_0KxBDl589O8KAxCG1alJgiA6 Success! Here is your starter subscription price id: price_0KxBDm589O8KAxCGMgG7scjb
This wraps up the quickstart. See the links below for a few different ways to process a payment for the product you just created.