Call compound.finance api with parameters

Question:

I’m trying to simply call the compound.finance api "https://api.compound.finance/api/v2/account" with the parameter max_health. the doc says

"If provided, should be given as { "value": "…string formatted
number…" }".

(https://compound.finance/docs/api#account-service)

So I tried 4 methods here below:

response = requests.get(
    'https://api.compound.finance/api/v2/account',
    params={
        "max_health": "1.0" # method 1
        "max_health": {"value":"1.0"} # method 2
        "max_health": json.dumps({"value":"1.0"}) # method 3
    }
)

but it does not work, and I get

HTTPError: 500 Server Error: Internal Server Error for url:…

Any idea I should format it please?

Asked By: Nicolas Rey

||

Answers:

They did not update the API docs. You should send a POST request and provide params as a request body.

import json
import requests

url = "https://api.compound.finance/api/v2/account"
data = {
    "max_health": {"value": "1.0"}
}

response = requests.post(url, data=json.dumps(data))  # <Response [200]>
response = response.json()  # {'accounts': ...}

Edit notes

The problem was that the API expects raw JSON so I used json.dumps.

Answered By: Artyom Vancyan

As Artyom already explained his beautiful answer, indeed their API documentation unfortunately outdated. In addition to his answer I’d like to add that requests library supports json argument that accepts raw JSON arguments starting with requests version 2.4.2. Therefore data=json.dumps(params) is not necessary anymore.

See my code below.

api_base = "https://api.compound.finance/api/v2/account"
params = {'max_health': {'value':'0.95'},
          'min_borrow_value_in_eth': { 'value': '0.002' },
          'page_number':19,
         }
response = requests.post(api_base, json=params).json()
Answered By: Mehmet Kaan ERKOÇ
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.