In this quickstart, we will walk through how to fetch Dune data in Python.

Prerequisites

Step 1: Prepare your Dune query

Have a Dune query you’d like to pull data from. Here, we’ll use this query to get the DAI token balance for vitalik.eth.

Step 2: Install Dune Python SDK

Ensure the Dune Python SDK is installed in your environment. You can install it using pip:

pip install dune-client

Step 3: Get data from Dune query

Import packages

from dune_client.types import QueryParameter
from dune_client.client import DuneClient
from dune_client.query import QueryBase
# import other needed packages
import pandas as pd

Set up a Dune Python client

dune = DuneClient(
    api_key='<paste your api key here>',
    base_url="https://api.dune.com",
    request_timeout=300 # request will time out after 300 seconds
)

Get query result

You can choose to either get the latest query result without triggering an execution or to trigger an execution and get the result to ensure freshest data.

  query_result = dune.get_latest_result(3373921) # get latest result in json format
  # query_result = dune.get_latest_result_dataframe(3373921) # get latest result in Pandas dataframe format
  • To paginate query results, please visit the pagination page to get more info.
  • If you are using the Python SDK:
    • For higher level functions like run_query(), pagination is handled for you automatically behind the scene, so you will get the entire dataset as the returned result. You can pass in parameter batch_size to define the maximum number of rows per batch or pagination call.
    • For lower level functions like get_execution_results(), you can pass in pagination parameters limit and offset directly, as instructed here.