Michael W
Michael W

Reputation: 343

How can I properly call this API with authentication? [Python/JSON]

my python is alright but I am terrible when it comes to understanding APIs, and I'm really stuck on understanding how to authenticate my call to this certain library. I know how to handle JSON data when I receive it, but I cannot even access JSON properly because I'm failing to authenticate the call to this library and there is nothing written online besides the actual documentation.

I've tried using HTTPDigestAuth and OAuth1 and 2 to no avail. This is the documentation: https://coinigy.docs.apiary.io/#reference/market-data/market-data

At the top of the docs it says the REST URI is https://api.coinigy.com/api/v1/ And I have been inputting my api key and secret key but the json returns failed authentication attempt, which I know means I am not authenticating correctly. My attempt is:

import requests

url = 'https://api.coinigy.com/api/v1/'
payload = {'X-API-KEY': '', 'X-API-SECRET': ''}


r = requests.get(url, params=payload)
print(r.json())

What type of authentication does this actually require, and how can I properly authenticate it with URL, key and secret key?

Upvotes: 1

Views: 3368

Answers (1)

M Doe
M Doe

Reputation: 31

I don't have experience with your API, but I did notice that there is a github repo with python examples at https://github.com/coinigy/api. The python_ws_example.py was updated recently and looks like you can plug in your keys easily.

If it were me (and you're not on Windows or have it setup to use curl on Windows), I would start by trying to verify that my account credentials are valid. The easiest way to do that would be to try one of the curl examples shown at https://github.com/coinigy/api/blob/master/curl_examples.sh , as executing a curl command is pretty straightforward. You don't have to bother with the whole shell script, just grab one line out such as:

curl -H "X-API-KEY:yourapikeyhere" -H "X-API-SECRET:yoursecrethere" -X POST "https://api.coinigy.com/api/v1/exchanges"

Edit for more info:

An example API call using the data endpoint. You will have to execute with your own valid key as I've not registered one, but the Request Response even without a valid key was 200 OK and a standard error message about invalid key, so it should work for you.

import requests
api_url = 'https://api.coinigy.com/api/v1/data'
headers = {
  'Content-Type': 'application/json',
  'X-API-KEY': '202a087985b7f55eed4cd8b79ae0b',
  'X-API-SECRET': '105e1c8f5eb63670920b90f2aee73'
}
params=  {
    "exchange_code": "GDAX",
    "exchange_market": "BTC/USD",
    "type": "history"
  }
r = requests.get(url=api_url, headers=headers)
print r.content

This snippet was put together using the information such as sample parameters found at at https://coinigy.docs.apiary.io/#reference/market-data/market-data/data-{type:history} as a reference. It demonstrates the 3 things you need for an the API request: (1) Authentication information passed in the headers, (2) a restful endpoint (the api_url value above) which identifies where you can find the kind of information you are looking for (3) some parameters that communicate exactly what data you want to get out of that endpoint.

It sounds like the endpoints are a concept that it would help to clarify for you. Typically a restful api is going to have a base url such as https://api.coinigy.com/api/v1 in this case. Each kind of information that is exposed through the API will have its own endpoint, which is essentially where you can go to input parameters for requesting that information. You glance over all the kinds of information exposed via your Coinigy API in the left menu under the REFERENCE heading at the API docs. "Account Functions" and "Market Data" for example.

When you click on Market Data or any of the other major categories on that api docs menu, you'll see specific API calls that are supported. Under Market Data, there is an entry for data{type:history}, and when you click on it, you can see the endpoint for that kind of information which is https://api.coinigy.com/api/v1/data . In fact, the documentation here is really great and shows you a lot of information such as how to use the parameters. (There are really horribly documented APIs out there, and it is fortunate that this is not one of them.)

Upvotes: 1

Related Questions