Dune Analytics Python Client

raw JSON →
1.10.0 verified Mon Apr 27 auth: no python

Official Python SDK for the Dune Analytics API. Version 1.10.0 supports async/sync clients, direct SQL execution, Pipeline API, Uploads, and Datasets. Backward-incompatible changes include removal of from_env() and custom_endpoints.

pip install dune-client
error from dune_client import DuneClient ImportError: cannot import name 'DuneClient' from 'dune_client'
cause DuneClient is in the client submodule.
fix
Use: from dune_client.client import DuneClient
error AttributeError: 'DuneClient' object has no attribute 'from_env'
cause from_env() removed in v1.8.0.
fix
Use: DuneClient(os.getenv('DUNE_API_KEY'))
error dune_client.client.DuneAuthenticationError: API key not found
cause API key not provided or empty.
fix
Set DUNE_API_KEY environment variable or pass key argument.
breaking from_env() classmethod removed in v1.8.0; use explicit DuneClient(api_key) instead.
fix Replace DuneClient.from_env() with DuneClient(os.getenv('DUNE_API_KEY'))
breaking custom_endpoints parameter removed in v1.9.1; use environment variable DUNE_API_BASE_URL.
fix Set DUNE_API_BASE_URL env var, or use client = DuneClient(api_key, base_url='https://custom.api.dune.com')
deprecated legacy Table API (create_table, insert_table, etc.) deprecated in v1.10.0; use Uploads and Datasets APIs.
fix Migrate to client.upload_csv() and client.create_dataset() per official docs.
gotcha DuneClient is thread-safe but not fork-safe; do not use with multiprocessing without reinitializing.
fix Create a new DuneClient instance per process.
gotcha API key must be from Dune Analytics (not Etherscan); use personal API key from https://dune.com/settings/api
fix Generate API key at https://dune.com/settings/api

Initialize client with API key, fetch latest query result, and refresh a query with parameters.

import os
from dune_client.client import DuneClient
from dune_client.types import QueryParameter

api_key = os.environ.get("DUNE_API_KEY", "")
if not api_key:
    raise ValueError("Set DUNE_API_KEY environment variable")

client = DuneClient(api_key)
# Execute a query by ID
result = client.get_latest_result(12345)
print(result)

# Execute a query with parameters
params = [QueryParameter.text_type(name="network", value="ethereum")]
result = client.refresh(12345, params=params)
print(result)