The unofficial Go library for the Coinbase Commerce API.
For more details visit Coinbase API docs.
To start using this library register an account on Coinbase Commerce.
You will find your API_KEY
from User Settings.
Next initialize a HttpCoinbaseClient
for interacting with the API. The only required parameter to initialize a client is API_KEY
Default timeout for the http client is 3 seconds*, you can change that value like so:
import "github.com/opaolini/coinbase-commerce-go"
func main() {
c := coinbase.NewHttpClient("API_KEY").WithClientTimeout(3000) // Optional timeout specification in milliseconds
}
Client supports the handling of common API errors and warnings.
Error | Status Code |
---|---|
APIError | * |
InvalidRequestError | 400 |
ParamRequiredError | 400 |
ValidationError | 400 |
AuthenticationError | 401 |
ResourceNotFoundError | 404 |
RateLimitExceededError | 429 |
InternalServerError | 500 |
ServiceUnavailableError | 503 |
Example of handling errors from the API:
checkoutID := "random-id"
result, err := c.RetrieveCheckout(checkoutID)
if err != nil {
switch err.(type) {
case ResponseError:
responseError, _ := err.(ResponseError)
if responseError.IsValidationError() {
log.Printf("Provided checkout ID: %s is invalid: %s \n", checkoutID, responseError.ReturnedError.Message)
}
}
}
Install with go
:
go get github.com/opaolini/coinbase-commerce-go
import "github.com/opaolini/coinbase-commerce-go"
func main() {
c := coinbase.NewHttpClient("API_KEY")
}
checkout, err := c.RetrieveCheckout(<checkout_id>)
if err != nil {
panic(err)
}
checkoutData := Checkout{
Name: "The Sovereign Individual",
Description: "Mastering the Transition to the Information Age",
PricingType: FixedPrice,
LocalPrice: LocalPrice{
Amount: "100.00",
Currency: "USD",
},
RequestedInfo: []string{"name", "email"},
}
checkout, err := c.CreateCheckout(checkoutData)
if err != nil {
panic(err)
}
fmt.Println(checkout)
checkoutData := Checkout{
Name: "The Sovereign Individual",
Description: "Mastering the Transition to the Information Age",
PricingType: FixedPrice,
LocalPrice: LocalPrice{
Amount: "100.00",
Currency: "USD",
},
RequestedInfo: []string{"name", "email"},
}
checkout, err := c.CreateCheckout(checkoutData)
if err != nil {
panic(err)
}
checkout.LocalPrice = LocalPrice{
Amount: "200",
Currency: "USD",
}
updatedCheckout, err := c.UpdateCheckout(checkout)
if err != nil {
panic(err)
}
fmt.Println(updatedCheckout)
checkoutData := Checkout{
Name: "The Sovereign Individual",
Description: "Mastering the Transition to the Information Age",
PricingType: FixedPrice,
LocalPrice: LocalPrice{
Amount: "100.00",
Currency: "USD",
},
RequestedInfo: []string{"name", "email"},
}
checkout, err := c.CreateCheckout(checkoutData)
if err != nil {
panic(err)
}
err = c.DeleteCheckout(checkout.ID)
if err != nil {
panic(err)
}
checkouts, err := c.ListCheckouts(nil) // grabs all
if err != nil {
panic("unexpected error: ", err)
}
fmt.Println(len(checkouts))
p := &Pagination{
Limit: 50,
}
checkouts, err := c.ListCheckouts(p)
if err != nil {
panic("unexpected error: ", err)
}
fmt.Println(len(checkouts))
charge, err := c.RetrieveCharge("76936406-dac7-45f9-8efd-b7a5f8efa7ee")
if err == nil {
panic("we should not end up here")
}
fmt.Println(charge)
chargeRequest := ChargeRequest{
Name: "The Sovereign Individual",
Description: "Mastering the Transition to the Information Age",
PricingType: FixedPrice,
LocalPrice: LocalPrice{
Amount: "100.00",
Currency: "USD",
},
}
charge, err := c.CreateCharge(chargeRequest)
if err != nil {
panic(err)
}
fmt.Println(charge)
charges, err := c.ListCharges(nil)
if err != nil {
panic(err)
}
fmt.Println(len(charges))
p := &Pagination{
Limit: 50,
}
charges, err := c.ListCharges(p)
if err != nil {
panic(err)
}
fmt.Println(len(charges))
func HandleWebhook(w http.ResponseWriter, r *http.Request) {
sharedKey := "your-shared-key"
ok, err := VerifyWebhookSignatureFromRequest(sharedKey, r)
if err != nil {
// handle error
panic(err)
}
if !ok {
// handle invalid signature
}
}
sharedKey := "your-shared-key"
signature := ""
body := []byte("{}")
ok, err := VerifyWebhookSignature(sharedKey, signature, body)
if err != nil {
// handle error
panic(err)
}
if !ok {
// handle invalid signature
}
- Add some mock tests, as coinbase-commerce does not currently have a sandbox
- Handle pagination properly
- Add Webhook signature verification
MIT