Samples
In the following section, you will find example code for interacting with the API using JavaScript and Python. These examples demonstrate how to send a GraphQL query to the API endpoint and print the response.
- Javascript
- Python
const endpoint = "https://api.stakingrewards.com/public/query";
const query = `
{
assets(where: {symbols: ["ETH"]}, limit: 1) {
name
slug
description
symbol
}
}
`;
fetch(endpoint, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-API-KEY": "YOUR_API_KEY",
},
body: JSON.stringify({ query }),
})
.then((response) => response.json())
.then((data) => console.log(data.data));
This code sends a POST
request to our endpoint
with a Content-Type
of application/json
and an X-API-KEY
header containing your API key. The request body contains the GraphQL query as a string, which is passed as the query
variable. The fetch
function returns a promise, which is resolved with the JSON response from the server when the request is successful. You can then access the data returned by the query by accessing the data
property of the response.
Note: You will need to replace YOUR_API_KEY
with a valid API key in order for the request to be successful. You may also need to modify the query and endpoint to match the structure and requirements of the API you are using.
The response
looks like this:
{
"data": {
"assets": [
{
"name": "Ethereum",
"slug": "ethereum-2-0",
"description": "the worlds largest and most decentralised Layer1 blockchain. The network is used for building dApps, holding assets, transacting and communicating without being controlled by a central authority. The Ethereum vision is to build a digital future on a global scale, that is powerful enough to help all of humanity. The native token of the network is Ether ($ETH) which is used to pay for certain activities on the Ethereum network",
"symbol": "ETH"
}
]
}
}
import requests
endpoint = "https://api.stakingrewards.com/public/query"
query = """
query {
assets(where: {symbols: ["ETH"]}, limit: 1) {
name
slug
description
symbol
}
}
"""
headers = {
"Content-Type": "application/json",
"X-API-KEY": "YOUR_API_KEY",
}
data = {"query": query}
response = requests.post(endpoint, json=data, headers=headers)
if response._code == 200:
print(response.json())
else:
print("Error occurred:", response.status_code)
This code sends a POST
request to the endpoint
with a Content-Type
of application/json
and an x-auth-key
header containing a valid authentication key. The request body contains the GraphQL query as a string, which is passed as the query
variable. The requests.post
function returns a response object, which contains the JSON response from the server when the request is successful. If the request is successful, the response is printed to the console. Otherwise, an error message is printed.
You also could use variables with queries like this one:
query = """
query ($id: [String!]) {
assets(limit:1,where: {ids:$id}) {
id
slug
symbol
metrics(limit:1) {
label
metricKey
defaultValue
}
}
}
"""
Note: You will need to replace YOUR_AUTH_KEY
with a valid authentication key in order for the request to be successful. You may also need to modify the query and endpoint to match the structure and requirements of the API you are using.
The response
looks like this:
{
"data": {
"assets": [
{
"name": "Ethereum",
"slug": "ethereum-2-0",
"description": "the worlds largest and most decentralised Layer1 blockchain. The network is used for building dApps, holding assets, transacting and communicating without being controlled by a central authority. The Ethereum vision is to build a digital future on a global scale, that is powerful enough to help all of humanity. The native token of the network is Ether ($ETH) which is used to pay for certain activities on the Ethereum network",
"symbol": "ETH"
}
]
}
}