How to Use the Stripe API in Go

Web
Initialize the Stripe Go client with your secret key and call API methods like Charge.New to process payments securely.

Use the official Stripe Go client library to initialize a client with your secret key and call API methods like Charge.New to process payments.

package main

import (
	"fmt"
	"log"

	"github.com/stripe/stripe-go/v80"
	"github.com/stripe/stripe-go/v80/charge"
)

func main() {
	stripe.Key = "sk_test_YOUR_SECRET_KEY"

	params := &stripe.ChargeParams{
		Amount:   stripe.Int64(2000),
		Currency: stripe.String("usd"),
		Source:   stripe.String("tok_visa"),
	}

	c, err := charge.New(params)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Charge ID: %s\n", c.ID)
}