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?
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
.
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()
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?
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
.
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()