Skip to content

Instantly share code, notes, and snippets.

@alferz
Created February 5, 2018 23:08
Show Gist options
  • Save alferz/4ff1433ae43d15b2b4a26c51a3ec569c to your computer and use it in GitHub Desktop.
Save alferz/4ff1433ae43d15b2b4a26c51a3ec569c to your computer and use it in GitHub Desktop.
Python Coinbase API with API-Key Auth Example (SHA256 HMAC Signature)
def getCoinbaseData(path):
import urllib2, json
import hmac
import hashlib
import base64
import time
apiKey = '<YOUR API KEY>'
apiSecret = '<YOUR API SECRET>'
url = "https://api.coinbase.com%s" % path
headers = {}
timestamp = int(time.time())
headers['CB-ACCESS-KEY'] = apiKey
headers['CB-ACCESS-TIMESTAMP'] = timestamp
headers['CB-VERSION'] = '2018-02-01'
toSign = str(timestamp) + 'GET' + path
signature = hmac.new(apiSecret, toSign, hashlib.sha256).hexdigest()
headers['CB-ACCESS-SIGN'] = signature
try:
request = urllib2.Request(url, headers=headers)
response = urllib2.urlopen(request, timeout=2.5)
data = json.loads(response.read())
print data
except Exception as e:
print "ERROR: %s" % str(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment