Skip to content

Instantly share code, notes, and snippets.

@phillijw
Forked from nottug/sample.py
Last active April 13, 2021 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillijw/1f78c8bafdce3a71a0b2ef9d4f5942a1 to your computer and use it in GitHub Desktop.
Save phillijw/1f78c8bafdce3a71a0b2ef9d4f5942a1 to your computer and use it in GitHub Desktop.
Coinigy API v2 Signature and Request Example in Python 3
import time
import hmac
import hashlib
import requests
BASE_URL = 'https://api.coinigy.com'
ENDPOINT = '/api/v2/private/exchanges'
X_API_KEY = 'your_api_v2_key_goes_here'
SECRET = 'your_corresponding_api_v2_secret_goes_here'
METHOD = 'GET'
UNIXTIME = time.time()
PARAMS = {} # = {'StartDate':'2018-07-30T00:00:00.000Z','EndDate':'2018-07-30T23:59:59.000Z'}
BODY = ''
X_API_TIMESTAMP = str(int(UNIXTIME))
query_string = "?" + "&".join( [ key + '=' + urllib.parse.quote_plus(PARAMS[key]) for key in PARAMS.keys() ] )
msg = X_API_KEY + X_API_TIMESTAMP + METHOD + ENDPOINT + (query_string if len(query_string) > 1 else '') + BODY
signature_bytes = hmac.new(SECRET.encode("ascii"), msg.encode("ascii"), digestmod=hashlib.sha256).digest()
signature_hex = map("{:02X}".format, signature_bytes)
X_API_SIGN = ''.join(signature_hex)
headers = {'Accept': 'application/json', 'Content-Type': 'application/json', 'X-API-SIGN': X_API_SIGN, 'X-API-TIMESTAMP' : X_API_TIMESTAMP, 'X-API-KEY': X_API_KEY}
r = requests.get(BASE_URL + ENDPOINT, headers=headers, params=PARAMS, data=BODY)
print(r.status_code, r.reason, r.content, BASE_URL + ENDPOINT)
@illi4
Copy link

illi4 commented Oct 26, 2018

'X-API-KEY': KEY

should be

'X-API-KEY': X_API_KEY

@phillijw
Copy link
Author

phillijw commented Aug 28, 2019

Recent update: The endpoint will need to include the properly encoded query parameters going forward. This code doesn't include that at the moment

@dddragos
Copy link

Recent update: The endpoint will need to include the properly encoded query parameters going forward. This code doesn't include that at the moment

I'm not sure what this means. Can you please clarify what are the 'properly encoded query parameters' or update the documentation? I'm just getting the same 401 Unauthorized b'' error using v2 creds.

I tested using your parameters from Stackoverflow and I'm not getting the same signature you posted there: F9D2192675BA4D7BDB6D768DF077CEEF2A3FB72481B54E37E93CF750EF59892B

Thanks

@phillijw
Copy link
Author

The signature I had on stackoverflow was wrong. I forgot to update it the last time I made an edit to that answer. I've now updated it to be correct.

Also here is an example of python3 code generating the correct signature. I will update the code on this gist as well:

import time
import hmac
import hashlib
import requests
import urllib.parse
from datetime import datetime, timezone

BASE_URL = 'https://api.coinigy.com'
ENDPOINT = '/api/v2/private/exchanges'
X_API_KEY = 'keykeykeykeykeykeykeykeykeykeyke'
SECRET = 'secretsecretsecretsecretsecretse'
METHOD = 'GET'
UNIXTIME = datetime.fromtimestamp(1532718830, timezone.utc)
PARAMS = {'pythagoreanTheorem':'a^2+b^2=c^2'}
BODY = ''

X_API_TIMESTAMP = str(int(datetime.timestamp(UNIXTIME)))
query_string = "?" + "&".join( [ key + '=' + urllib.parse.quote_plus(PARAMS[key]) for key in PARAMS.keys() ] )
msg = X_API_KEY + X_API_TIMESTAMP + METHOD + ENDPOINT + (query_string if len(query_string) > 1 else '') + BODY
signature_bytes = hmac.new(SECRET.encode("ascii"), msg.encode("ascii"), digestmod=hashlib.sha256).digest()
signature_hex = map("{:02X}".format, signature_bytes)
X_API_SIGN = ''.join(signature_hex)

print(X_API_SIGN)

#headers = {'Accept': 'application/json', 'Content-Type': 'application/json', 'X-API-SIGN': X_API_SIGN, 'X-API-TIMESTAMP' : X_API_TIMESTAMP, 'X-API-KEY': X_API_KEY}
#r = requests.get(BASE_URL + ENDPOINT, headers=headers, params=PARAMS, data=BODY)

#print(r.status_code, r.reason, r.content, BASE_URL + ENDPOINT)

@phillijw
Copy link
Author

phillijw commented Apr 9, 2021

Here is a working example. Just put in your key and secret and it should return hourly candles between the two dates as well as other request info:

import time
import hmac
import hashlib
import requests
import urllib.parse
from datetime import datetime, timezone

BASE_URL = 'https://api.coinigy.com'
ENDPOINT = '/api/v2/private/exchanges/BINA/markets/BTC/USDT/ohlc/h'
X_API_KEY = 'keykeykey'
SECRET = 'secretsecretsecret'
METHOD = 'GET'
UNIXTIME = datetime.fromtimestamp(time.time(), timezone.utc)
PARAMS = {'StartDate':'2019-01-01T00:00:00.000Z', 'EndDate':'2019-01-02T00:00:00.000Z'}
BODY = ''

X_API_TIMESTAMP = str(int(datetime.timestamp(UNIXTIME)))
query_string = "?" + "&".join( [ key + '=' + urllib.parse.quote_plus(PARAMS[key]) for key in PARAMS.keys() ] )
msg = X_API_KEY + X_API_TIMESTAMP + METHOD + ENDPOINT + (query_string if len(query_string) > 1 else '') + BODY
signature_bytes = hmac.new(SECRET.encode("ascii"), msg.encode("ascii"), digestmod=hashlib.sha256).digest()
signature_hex = map("{:02X}".format, signature_bytes)
X_API_SIGN = ''.join(signature_hex)

print(X_API_SIGN)

headers = {'Accept': 'application/json', 'Content-Type': 'application/json', 'X-API-SIGN': X_API_SIGN, 'X-API-TIMESTAMP' : X_API_TIMESTAMP, 'X-API-KEY': X_API_KEY}
r = requests.get(BASE_URL + ENDPOINT, headers=headers, params=PARAMS, data=BODY)
print(r.status_code, r.reason, r.content, BASE_URL + ENDPOINT)

@dddragos
Copy link

dddragos commented Apr 12, 2021 via email

@phillijw
Copy link
Author

Any ideas what might cause this issue?

There are a few reasons:

  • The clock on your computer may be out of sync. The request needs to occur within 30sec of Coinigy server time otherwise it is denied
  • Make sure you're using a v2 api key, not a v1 key
  • Make sure you have a Crypto Feed subscription

That's about it

@dddragos
Copy link

dddragos commented Apr 13, 2021 via email

@phillijw
Copy link
Author

Any other things I could try on my end?

I'm out of ideas. If you want to contact me through the Coinigy support email and ask for me, I can continue helping you there. Next thing I would try is looking into your account to make sure everything is set up properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment