Execute query and get result in one call
Execute query and get result in one call
If you are using the Python SDK, you can directly executes and fetches result in one function call, like below:
run_query to get result in JSON,
run_query_csv to get result in CSV format,
and run_query_dataframe to get result in Pandas dataframePython SDK
import dotenv, os, json
from dune_client.types import QueryParameter
from dune_client.client import DuneClient
from dune_client.query import QueryBase
# change the current working directory where .env file lives
os.chdir("/Users/abc/local-Workspace/python-notebook-examples")
# load .env file
dotenv.load_dotenv(".env")
# setup Dune Python client
dune = DuneClient.from_env()
query = QueryBase(
name="Sample Query",
query_id=1215383,
)
result = dune.run_query(
query = query,
performance = 'large' # optionally define which tier to run the execution on (default is "medium")
)
# go over the results returned
for row in result.result.rows:
print (row) # as an example we print the rows
performance parameter, by default it will use the “medium” performance tier which consumes 10 credits. “large” will use 20 credits and are faster.
Returns an execution_id associated with the triggered query execution and the state of the execution.
curl -X POST "https://api.dune.com/api/v1/query/{{query_id}}/execute" \
-H "X-Dune-API-Key: {{api_key}} \
-H "Content-Type: application/json" \
-d '{"query_parameters": {"param1":24}, "performance": "large"}'
'''
Download Dune Python SDK by running `pip install dune-client`
For more info, visit the GitHub repository: https://github.com/duneanalytics/dune-client/tree/d2195b2a9577e2dcae5d2600cb3eddce20987f38
'''
import dotenv, os, json
from dune_client.types import QueryParameter
from dune_client.client import DuneClient
from dune_client.query import QueryBase
# change the current working directory where .env file lives
os.chdir("/Users/abc/local-Workspace/python-notebook-examples")
# load .env file
dotenv.load_dotenv(".env")
# setup Dune Python client
dune = DuneClient.from_env()
# define the query to run
query = QueryBase(
name="Sample Query",
query_id=1215383,
params=[
QueryParameter.text_type(name="TextField", value="Word"),
QueryParameter.number_type(name="NumberField", value=3.1415926535),
QueryParameter.date_type(name="DateField", value="2022-05-04 00:00:00"),
QueryParameter.enum_type(name="ListField", value="Option 1"),
],
)
result = dune.execute_query(query = query # pass in the query we just defined
, performance = "large" # define engine query is run on, default is "medium"
)
import requests
url = "https://api.dune.com/api/v1/query/{query_id}/execute"
headers = {"X-DUNE-API-KEY": "<x-dune-api-key>"}
response = requests.request("POST", url, headers=headers)
print(response.text)
const options = {method: 'POST', headers: {'X-DUNE-API-KEY': '<x-dune-api-key>'}};
fetch('https://api.dune.com/api/v1/query/{query_id}/execute', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
package main
import (
"fmt"
"net/http"
"io/ioutil"
)
func main() {
url := "https://api.dune.com/api/v1/query/{query_id}/execute"
req, _ := http.NewRequest("POST", url, nil)
req.Header.Add("X-DUNE-API-KEY", "<x-dune-api-key>")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
}
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.dune.com/api/v1/query/{query_id}/execute",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => [
"X-DUNE-API-KEY: <x-dune-api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
HttpResponse<String> response = Unirest.post("https://api.dune.com/api/v1/query/{query_id}/execute")
.header("X-DUNE-API-KEY", "<x-dune-api-key>")
.asString();

