Git Product home page Git Product logo

commerce-sdk-go's Introduction

Commerce Go SDK README

GoDoc Go Report Card

Overview

The Commerce Go SDK is a sample library that demonstrates the structure of a Coinbase Commerce driver for the REST APIs.

License

The Commerce Go SDK sample library is free and open source and released under the Apache License, Version 2.0.

The application and code are only available for demonstration purposes.

Usage

To use the Commerce Go SDK, initialize the Credentials struct and create a new client. The Credentials struct is JSON enabled. Ensure that Commerce API credentials are stored in a secure manner.

Example

This code snippet reads the value of the environment variable COMMERCE_API_KEY to initiate a new Commerce client.

Copy Commerce SDK repository

git clone [email protected]:coinbase-samples/commerce-sdk-go.git

Set the API key as an environment variables in a terminal application

export COMMERCE_API_KEY=<YOUR-COMMERCE-API-KEY>

Initialize the Commerce client with credentials

creds, err := commerce.ReadEnvCredentials("COMMERCE_API_KEY")
if err != nil {
	fmt.Printf("Error reading environmental variable: %s", err)
}

client := commerce.NewClient(creds, http.Client{},)

Once a client is initialized, you may call any of the functions. For example, to create a charge,

func main() {
	// Initialize credentials struct
	creds, err := commerce.ReadEnvCredentials("COMMERCE_API_KEY")
	if err != nil {
		fmt.Printf("Error reading environmental variable: %s", err)
	}

	// Initialize client
	client := commerce.NewClient(creds, http.Client{})

	// Add desired context
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	charge, err := client.CreateCharge(ctx, &commerce.ChargeRequest{
		PricingType: "fixed_price",
		LocalPrice: &commerce.LocalPrice{
			Amount:   "1.00",
			Currency: "USD",
		},
	})

	if err != nil {
		fmt.Printf("error: %s\n", err)
	}

	// Print the hosted url
	fmt.Printf("hosted url: %s\n", charge.Data.HostedURL)
}

Quickstart example

The "example" folder will contain the logic to create a charge in the amount of $1.00. To see this in action:

  1. Change into the 'example' directory by running: cd example
  2. Run go run example.go

Expected output:

created a charge ID: 64e05b38-a938-4620-a9ca-e3b806b3757b
got a hosted url: https://commerce.coinbase.com/charges/EGTHQJXZ

Obtaining Coinbase Commerce credentials

Coinbase Commerce API keys can be created in the Commerce UI under Settings --> Security.

commerce-sdk-go's People

Contributors

rcbgr avatar hughescoin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.