If you are using the Python SDK, you can directly executes and fetches result in one function call, like below:
Use 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 dataframe
Python SDK
import dotenv, os, jsonfrom dune_client.types import QueryParameterfrom dune_client.client import DuneClientfrom dune_client.query import QueryBase# change the current working directory where .env file livesos.chdir("/Users/abc/local-Workspace/python-notebook-examples")# load .env filedotenv.load_dotenv(".env")# setup Dune Python clientdune = 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 returnedfor row in result.result.rows:print(row)# as an example we print the rows
If the query has parameters and you don’t add them in your API call, it will just run with the default params. You may add query parameters as part of the POST params data.
You can choose to include a 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"}'
Query parameters in key-value pairs. Each parameter is an object consisting of keys such as 'key', 'type', 'value', and optionally 'enumOptions'. The API allows for partial submission of parameters. For example, if the query expects three parameters and you only pass in two, the third one will automatically use its default value as defined in the API. This feature enables you to customize the query execution according to your specific needs while providing sensible defaults for unspecified parameters.
The type of the parameter, determines the format of 'value(s)'. 'number': Numeric parameters, the value must be a number (e.g., '20'). 'text': String parameters, value can be any text including hex 0x-prefixed values (e.g., '0xae2fc...'), an empty value defaults to an empty string. 'datetime': Date and time parameters, value must be in 'YYYY-MM-DD hh:mm:ss' format (e.g., '2021-12-31 23:59:59'). 'enum': Parameters with a specific list of values, 'EnumValues' field is mandatory, providing a JSON list of strings representing valid options, the 'value' must be one of these options (e.g., 'Option1').
Defines the engine the execution will be run on. Can be either medium or large tier. Medium consumes 10 credits per run and large consumes 20 credits per run. By default performance is medium.