Go (golang) Client for the Coinbase GDAX API https://www.gdax.com
Go
Latest commit 255cfc1 Oct 12, 2016 @preichenberger committed on GitHub Merge pull request #10 from petesavitsky/patch-1
Update Client base api url to GDAX
Permalink
Failed to load latest commit information.
.gitignore first commit Mar 29, 2015
.travis.yml updated travis to prevent pull request tests Sep 9, 2015
LICENSE Added license Mar 29, 2015
README.md updated order to prepare for market orders, updated readme example Jun 28, 2015
account.go go fmt Sep 9, 2015
account_test.go go fmt Sep 9, 2015
client.go Update Client base api url to GDAX Sep 8, 2016
client_test.go go fmt Sep 9, 2015
currency.go go fmt Sep 9, 2015
currency_test.go go fmt Sep 9, 2015
cursor.go go fmt Sep 9, 2015
error.go go fmt Sep 9, 2015
fill.go go fmt Sep 9, 2015
fill_test.go go fmt Sep 9, 2015
message.go go fmt Sep 9, 2015
order.go post only Sep 8, 2015
order_test.go go fmt Sep 9, 2015
pagination.go go fmt Sep 9, 2015
product.go Added bid,ask, vol to ticker Jun 18, 2016
product_test.go go fmt Sep 9, 2015
report.go go fmt Sep 9, 2015
report_test.go go fmt Sep 9, 2015
test_helper.go go fmt Sep 9, 2015
time.go go fmt Sep 9, 2015
time_test.go go fmt Sep 9, 2015
transfer.go go fmt Sep 9, 2015
transfer_test.go go fmt Sep 9, 2015

README.md

Go Coinbase Exchange GoDoc Build Status

Summary

Go client for Coinbase Exchange

Installation

go get github.com/preichenberger/go-coinbase-exchange

Documentation

For full details on functionality, see GoDoc documentation.

Setup

How to create a client:

import (
  "os"
  exchange "github.com/preichenberger/go-coinbase-exchange"
)

secret := os.Getenv("COINBASE_SECRET") 
key := os.Getenv("COINBASE_KEY") 
passphrase := os.Getenv("COINBASE_PASSPHRASE") 

// or unsafe hardcode way
secret = "exposedsecret"
key = "exposedkey"
passphrase = "exposedpassphrase"

client := exchange.NewClient(secret, key, passphrase)

Cursor

This library uses a cursor pattern so you don't have to keep track of pagination.

var orders []exchange.Order
cursor = client.ListOrders()

for cursor.HasMore {
  if err := cursor.NextPage(&orders); err != nil {
    println(err.Error())
    return
  }

  for _, o := range orders {
    println(o.Id) 
  }
}

Websockets

Listen for websocket messages

  import(
    ws "github.com/gorilla/websocket"
  )

  var wsDialer ws.Dialer
  wsConn, _, err := wsDialer.Dial("wss://ws-feed.exchange.coinbase.com", nil)
  if err != nil {
    println(err.Error())
  }

  subscribe := map[string]string{
    "type": "subscribe",
    "product_id": "BTC-USD",
  }
  if err := wsConn.WriteJSON(subscribe); err != nil {
    println(err.Error())
  }

  message:= exchange.Message{}
  for true {
    if err := wsConn.ReadJSON(&message); err != nil {
      println(err.Error())
      break
    }

    if message.Type == "match" {
      println("Got a match")
    }
  }

Examples

This library supports all public and private endpoints

Get Accounts:

  accounts, err := client.GetAccounts()
  if err != nil {
    println(err.Error())
  }

  for _, a := range accounts {
    println(a.Balance)
  }

List Account Ledger:

  var ledger []exchange.LedgerEntry

  accounts, err := client.GetAccounts()
  if err != nil {
    println(err.Error())
  }

  for _, a := range accounts {
    cursor := client.ListAccountLedger(a.Id)
    for cursor.HasMore {
      if err := cursor.NextPage(&ledger); err != nil {
        println(err.Error())
      }

      for _, e := range ledger {
        println(e.Amount)
      }
  }

Create an Order:

  order := exchange.Order{
    Price: 1.00,   
    Size: 1.00,   
    Side: "buy",   
    ProductId: "BTC-USD",   
  }

  savedOrder, err := client.CreateOrder(&order)
  if err != nil {
    println(err.Error())
  }

  println(savedOrder.Id)

Transfer funds:

  transfer := exchange.Transfer {
    Type: "deposit",
    Amount: 1.00,   
  }

  savedTransfer, err := client.CreateTransfer(&transfer)
  if err != nil {
    println(err.Error())
  }

Get Trade history:

  var trades []exchange.Trade
  cursor := client.ListTrades("BTC-USD")

  for cursor.HasMore {
    if err := cursor.NextPage(&trades); err != nil {
      for _, t := range trades {
        println(trade.CoinbaseId)
      }
    } 
  }

Testing

To test with Coinbase's public sandbox set the following environment variables:

  • TEST_COINBASE_SECRET
  • TEST_COINBASE_KEY
  • TEST_COINBASE_PASSPHRASE

Then run go test

TEST_COINBASE_SECRET=secret TEST_COINBASE_KEY=key TEST_COINBASE_PASSPHRASE=passphrase go test