Using Checkout and Go

    Quickly integrate Checkout into your Go-based site to provide your users with a streamlined, mobile-ready payment experience.

    This tutorial demonstrates how to accept payments with Stripe Checkout in a web application built with Go. The application uses Checkout to accept credit cards from the end user and send tokens to a back-end API. The back-end route uses the Stripe Go library to create a charge. There are four steps:

    1. Install dependencies
    2. Create the view template
    3. Create the routes
    4. Run the application

    Step 1: Install and configure dependencies

    To follow along, you need a working Go environment. Use the go command-line tool to install the Stripe library:

    go get

    Create a file named main.go and add the necessary imports and configuration values:

    package main
    import (
    func main() {
      publishableKey := os.Getenv("PUBLISHABLE_KEY")
      stripe.Key = os.Getenv("SECRET_KEY")

    The file includes two values, the secret and publishable keys. These keys identify your account when you communicate with Stripe. In this example, the application extracts the values from local environment variables in order to cleanly separate configuration from code. Avoid hard-coding API access keys and other sensitive data in your application code.

    Assign the secret key to the Key property of the stripe package. Assign the publishable key to a new variable called publishableKey so that it can be used later.

    Step 2: Create the view template

    This example uses Go’s html/template package for server-side templating. Create a file named views/index.html for the index template:

      <title>Checkout Example</title>
    <form action="/charge" method="post" class="payment">
        <label class="amount">
          <span>Amount: $5.00</span>
      <script src="" class="stripe-button"
        data-key="{{ .Key }}"
        data-description="A month's subscription"

    To integrate the form, load Checkout in an HTML script tag. It adds a button to the form that the user can click to display the credit card overlay. The overlay automatically performs validation and error handling. The action attribute specifies the path of the charge route. In the next step, you will see how the .Key attribute is populated with the publishable key for your Stripe account.

    Add the following code to the main function in your main.go file so that it will load the template when the application runs:

    tmpls, _ := template.ParseFiles(filepath.Join("views", "index.html"))

    Step 2: Create routes

    The server exposes two routes:

    1. A GET route that displays the payment form
    2. A POST route that receives the payment token and creates the charge

    Add the route handlers to the main function of the main.go file:

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
      tmpl := tmpls.Lookup("index.html")
      tmpl.Execute(w, map[string]string{"Key": publishableKey})
    http.HandleFunc("/charge", func(w http.ResponseWriter, r *http.Request) {
      customerParams := &stripe.CustomerParams{Email: r.Form.Get("stripeEmail")}
      newCustomer, err := customer.New(customerParams)
      if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
      chargeParams := &stripe.ChargeParams{
        Amount:   500,
        Currency: "usd",
        Desc:     "Sample Charge",
        Customer: newCustomer.ID,
      if _, err := charge.New(chargeParams); err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
      fmt.Fprintf(w, "Charge completed successfully!")
    http.ListenAndServe(":4567", nil)

    The index route renders the Checkout form and displays it to the user. Pass the publishable key into the render function via a map literal so that the template can embed it in the Checkout form markup.

    The charge route retrieves the email address and card token from the POST request body. It uses those parameters to create a Stripe customer. Next, it invokes the charge.New function, providing the Customer ID as an option.

    In this example, the application charges the user $5. Stripe expects the developer to describe charges in cents, so compute the value of the amount parameter by multiplying the desired number of dollars by one hundred. Stripe charges also take an optional Desc parameter, which lets you describe the charge.

    When the charge completes successfully, the application displays a message to the user. You could optionally use a second template in the charge route instead of a plain string.

    That’s it, a complete Stripe integration in about 60 lines of Go code. You can see the full example in this gist on GitHub.

    Step 4: Run the application

    Run the application from the command line:

    PUBLISHABLE_KEY=pk_test_6pRNASCoBOKtIshFeQd4XMUh SECRET_KEY=sk_test_BQokikJOvBiI2HlWgH4olfQ2 go run main.go

    Specify values for the publishable and secret key environment variables.

    Navigate to the running application in your browser and click the button to launch the payment form. If you’re using Stripe test keys, you can test it with some dummy data. Enter the test number 4242 4242 4242 4242, a three digit CVC, and a future expiry date. Submit the form and see if the application correctly displays the successful charge page.