coinbase

package module
v0.0.0-...-a974795 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2021 License: MIT Imports: 15 Imported by: 0

README

GoDoc

Notice

This is a work in progress fork of https://github.com/fabioberger/coinbase-go. The point of this fork is to add V2 support.

Coinbase Go Client Library

An easy way to buy, send, and accept bitcoin through the Coinbase API.

This library supports the API key authentication method. The below examples use an API key.

A detailed step-by-step tutorial on how to use this library for V1 can be found at this blog. Currently no step-by-step tutorial exists for V2.

Installation

Make sure you have set the environment variable $GOPATH

export GOPATH="path/to/your/go/folder"

Obtain the latest version of the Coinbase Go library with:

go get github.com/dant89/coinbase-go-v2

Then, add the following to your Go project:

import (
	"github.com/dant89/coinbase-go-v2"
)

Usage

Start by enabling an API Key on your account.

Next, create an instance of the client using the ApiKeyClient method:

c := coinbase.ApiKeyClient(os.Getenv("COINBASE_KEY"), os.Getenv("COINBASE_SECRET"))

Notice here that we did not hard code the API key into our codebase, but set it in an environment variable instead. This is just one example, but keeping your credentials separate from your code base is a good security practice. Here is a step-by-step guide on how to add these environment variables to your shell config file.

Now you can call methods on c similar to the ones described in the API reference. For example:

user, err := c.GetUser()
if err != nil {
	log.Fatal(err)
}
fmt.Printf("User name is:", user.Name)

A working API key example is available in example/ApiKeyExample.go. To run it, execute:

go run ./example/ApiKeyExample.go

Error Handling

All errors generated at runtime will be returned to the calling client method. Any API request for which Coinbase returns an error encoded in a JSON response will be parsed and returned by the client method as a Golang error struct. Lastly, it is important to note that for HTTP requests, if the response code returned is not '200 OK', an error will be returned to the client method detailing the response code that was received.

Examples

Get user information
user, err := c.GetUser()
if err != nil {
	log.Fatal(err)
}
fmt.Println(user.Name)
// 'User One'
fmt.Println(user.Email)
// 'user1@example.com'

userAuth, err := c.GetUserAuth()
if err != nil {
	log.Fatal(err)
}
fmt.Println(userAuth.Scopes)
// 'Auth scopes'
Update user information
params := &UserParams{Name:"new username", TimeZone:"EST", NativeCurrency:"USD"}
user, err := c.UpdateUser(params)
if err != nil {
	log.Fatal(err)
}
fmt.Println(user.Names)
// 'new username'
Get accounts information
accounts, pagination, err := c.GetAccounts()
if err != nil {
	log.Fatal(err)
}
fmt.Println((*accounts)[0].Currency.Code)
// `First account currency code'
Update account information
params := &UserParams{Name:"new account name"}
account, err := c.UpdateAccount(accountId, params)
if err != nil {
	log.Fatal(err)
}
fmt.Println(account.Name)
// 'new account name'
Get addresses information
addresses, pagination, err := c.GetAdresses(accountId)
if err != nil {
	log.Fatal(err)
}
fmt.Println((*addresses)[0].Name)
// `First address name`
Get address transactions information
addressTransactions, pagination, err := c.GetAddressTransactions(accountId, addressId)
if err != nil {
	log.Fatal(err)
}
fmt.Println((*addressTransactions)[0].Type)
// `First transaction type`
Get transactions information
transactions, pagination, err := c.GetTransactions(accountId)
if err != nil {
	log.Fatal(err)
}
fmt.Println((*transactions)[0].Type)
// `First transaction type`
transaction, err := c.GetTransaction(accountId, transactionId)
if err != nil {
	log.Fatal(err)
}
fmt.Println(transaction.Type)
// `First transaction type`
Get buys information
buys, pagination, err := c.GetBuys(accountId)
if err != nil {
	log.Fatal(err)
}
fmt.Println((*buys)[0].Status)
// `First buys status`
buy, err := c.GetBuy(accountId, buyId)
if err != nil {
	log.Fatal(err)
}
fmt.Println(buy.Status)
// `Buy status`
Get sells information
sells, pagination, err := c.GetSells(accountId)
if err != nil {
	log.Fatal(err)
}
fmt.Println((*sells)[0].Status)
// `First sells status`
sell, err := c.GetSell(accountId, sellId)
if err != nil {
	log.Fatal(err)
}
fmt.Println(sell.Status)
// `Sell status`
Get deposits information
deposits, pagination, err := c.GetDeposits(accountId)
if err != nil {
	log.Fatal(err)
}
fmt.Println((*deposits)[0].Status)
// `First deposits status`
deposit, err := c.GetDeposit(accountId, depositId)
if err != nil {
	log.Fatal(err)
}
fmt.Println(deposit.Status)
// `Deposit status`
Get withdrawals information
withdrawals, pagination, err := c.GetWithdrawals(accountId)
if err != nil {
	log.Fatal(err)
}
fmt.Println((*withdrawals)[0].Status)
// `First withdrawals status`
withdrawal, err := c.GetWithdrawal(accountId, withdrawalId)
if err != nil {
	log.Fatal(err)
}
fmt.Println(withdrawal.Status)
// `Withdrawal status`
Get payment methods information
paymentMethods, pagination, err := c.GetPaymentMethods()
if err != nil {
	log.Fatal(err)
}
fmt.Println((*paymentMethods)[0].Type)
// `First payment methods type`
paymentMethod, err := c.GetPaymentMethod(paymentMethodId)
if err != nil {
	log.Fatal(err)
}
fmt.Println(paymentMethod.Type)
// `PaymentMethod type`

Adding new methods

You can see a list of method calls here and how they are implemented. They are all wrappers around the Coinbase JSON API.

If there are any methods listed in the API Reference that don't have an explicit function name in the library, you can also call Get, Post, Put, or Delete with a path, params and holder struct for a quick implementation. Holder should be a pointer to some data structure that correctly reflects the structure of the returned JSON response. The library will attempt to unmarshal the response from the server into holder. For example:

user := map[string]string{} // Holder struct depends on JSON format returned from API
if err := c.Get("user", nil, &user); err != nil {
	log.Fatal(err)
}
fmt.Println(user)
// map[name:Test User email:test@test.com]

Or feel free to add a new wrapper method and submit a pull request.

Security notes

If someone gains access to your API Key they will have complete control of your Coinbase account. This includes the abillity to send all of your bitcoins elsewhere.

For this reason, API access is disabled on all Coinbase accounts by default. If you decide to enable API key access you should take precautions to store your API key securely in your application. How to do this is application specific, but it's something you should research if you have never done this before.

Testing

In order to run the tests for this library, you will first need to install the Testify/Assert dependency with the following command:

go get github.com/stretchr/testify/assert

Then run all tests by executing the following in your command line:

go test . -v

To run either only the endpoint or mock tests, use the below commands:

Endpoint(Live) :

go test . -v -test.run=TestEndpoint

Mock :

go test . -v -test.run=TestMock

Documentation

Overview

Coinbase-go is a convenient Go wrapper for the Coinbase API

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AccountParams

type AccountParams struct {
	Name string `json:"name,omitempty"`
}

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is the struct from which all API requests are made

func ApiKeyClient

func ApiKeyClient(key string, secret string) Client

ApiKeyClient instantiates the client with ApiKey Authentication

func (Client) Delete

func (c Client) Delete(path string, params interface{}, holder interface{}) error

Delete sends a DELETE request and marshals response data into holder

func (Client) Get

func (c Client) Get(path string, params interface{}, holder interface{}) error

Get sends a GET request and marshals response data into holder

func (Client) GetAccount

func (c Client) GetAccount(accountId string) (*account, error)

GetAccount gets an account assosciated to a account_id

func (Client) GetAccounts

func (c Client) GetAccounts(params map[string]string) (*[]account, *pagination, error)

GetAccounts gets the accounts associated with the authentication

func (Client) GetAddressTransactions

func (c Client) GetAddressTransactions(accountId string, addressId string, params map[string]string) (*[]transaction, *pagination, error)

GetAddressTransactions gets the address transactions associated with the authentication

func (Client) GetAddresses

func (c Client) GetAddresses(accountId string, params map[string]string) (*[]address, *pagination, error)

GetAddresses gets the addresses associated with the authentication

func (Client) GetBuy

func (c Client) GetBuy(accountId string, buyId string, params map[string]string) (*buy, error)

GetBuy get a buy by id of a buy associated with the authentication

func (Client) GetBuys

func (c Client) GetBuys(accountId string, params map[string]string) (*[]buy, *pagination, error)

GetBuys gets the buys associated with the authentication

func (Client) GetDeposit

func (c Client) GetDeposit(accountId string, depositId string, params map[string]string) (*deposit, error)

GetDeposit get a deposit by id of a deposit associated with the authentication

func (Client) GetDeposits

func (c Client) GetDeposits(accountId string, params map[string]string) (*[]deposit, *pagination, error)

GetDeposits gets the deposits associated with the authentication

func (Client) GetPaymentMethod

func (c Client) GetPaymentMethod(paymentMethodId string, params map[string]string) (*paymentMethod, error)

GetPaymentMethod get a payment method by id

func (Client) GetPaymentMethods

func (c Client) GetPaymentMethods(params map[string]string) (*[]paymentMethod, *pagination, error)

GetPaymentMethods gets the payment methods

func (Client) GetSell

func (c Client) GetSell(accountId string, sellId string, params map[string]string) (*sell, error)

GetSell get a sell by id of a sell associated with the authentication

func (Client) GetSells

func (c Client) GetSells(accountId string, params map[string]string) (*[]sell, *pagination, error)

GetSells gets the sells associated with the authentication

func (Client) GetTransaction

func (c Client) GetTransaction(accountId string, transactionId string, params map[string]string) (*transaction, error)

GetTransaction get a transaction by id of a transaction associated with the authentication

func (Client) GetTransactions

func (c Client) GetTransactions(accountId string, params map[string]string) (*[]transaction, *pagination, error)

GetTransactions gets the transactions associated with the authentication

func (Client) GetUser

func (c Client) GetUser() (*user, error)

GetUser gets the user associated with the authentication

func (Client) GetUserAuth

func (c Client) GetUserAuth() (*userAuth, error)

GetUserAuth gets the user auth associated with the authentication

func (Client) GetUsers

func (c Client) GetUsers(userId string) (*user, error)

GetUsers gets a user assosciated to a user_id

func (Client) GetWithdrawal

func (c Client) GetWithdrawal(accountId string, withdrawalId string, params map[string]string) (*withdrawal, error)

GetWithdrawal get a withdrawal by id of a withdrawal associated with the authentication

func (Client) GetWithdrawals

func (c Client) GetWithdrawals(accountId string, params map[string]string) (*[]withdrawal, *pagination, error)

GetWithdrawals gets the withdrawals associated with the authentication

func (Client) Post

func (c Client) Post(path string, params interface{}, holder interface{}) error

Post sends a POST request and marshals response data into holder

func (Client) Put

func (c Client) Put(path string, params interface{}, holder interface{}) error

Put sends a PUT request and marshals response data into holder

func (Client) UpdateAccount

func (c Client) UpdateAccount(accountId string, params *AccountParams) (*account, error)

UpdateAccount updates the account associated with the authentication

func (Client) UpdateUser

func (c Client) UpdateUser(params *UserParams) (*user, error)

UpdateUser updates the user associated with the authentication

type UserParams

type UserParams struct {
	Name           string `json:"name,omitempty"`
	TimeZone       string `json:"time_zone,omitempty"`
	NativeCurrency string `json:"native_currency,omitempty"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL