-
-
Save phillijw/1f78c8bafdce3a71a0b2ef9d4f5942a1 to your computer and use it in GitHub Desktop.
Coinigy API v2 Signature and Request Example in Python 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
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)
Hi Phillip
I'm still getting the same 401 error. I replaced the API and SECRET keys
and it doesn't work. Any ideas what might cause this issue?
Thanks
…On Fri, 9 Apr 2021 at 17:49, Joe Phillips ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
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)
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/1f78c8bafdce3a71a0b2ef9d4f5942a1#gistcomment-3699919>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKG3F7USOMX7QS6LWMIQJHDTH4VZVANCNFSM4IRPY4RA>
.
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
Thanks Joe. I checked and points 2 and 3 are definitely correct. I also
made sure my time is current and I double-checked against
unixtimestamp .com and my output definitely matches the universal time.
Still no luck. Any other things I could try on my end?
…On Mon, 12 Apr 2021 at 21:43, Joe Phillips ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
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
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/1f78c8bafdce3a71a0b2ef9d4f5942a1#gistcomment-3703303>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKG3F7SJIAPXQGSPTDPCSLLTINLNJANCNFSM4IRPY4RA>
.
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
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: