Dune Analytics API
A skill for querying and analyzing blockchain data via the Dune Analytics API.
Setup
pip install dune-client
Set DUNE_API_KEY via environment variable, .env file, or agent config.
⚠️ Usage Rules
- Read before writing SQL — Select and read the relevant reference files (see Reference Selection) before writing any query. Do not skip this step.
- Prefer Private Queries — Try
is_private=True first. Fall back to public if it fails (free plan), and notify the user.
- Don't create duplicates — Reuse/update existing queries unless explicitly asked to create new ones.
- Confirm before updating — Ask the user before modifying an existing query.
- Track credits — Report credits consumed after each execution. See query-execution.md.
Reference Selection
Before writing any SQL, route to the correct reference file(s) based on your task:
| Task involves... |
Read this reference |
| Finding tables / inspecting schema / discovering protocols |
table-discovery.md |
| Finding decoded tables by contract address |
table-discovery.md |
| Searching Dune documentation / guides / examples |
table-discovery.md |
| Wallet / address tracking / router identification |
wallet-analysis.md |
| Table selection / common table names |
common-tables.md |
| SQL performance / complex joins / array ops |
sql-optimization.md |
| API calls / execution / caching / parameters |
query-execution.md |
| Uploading CSV/NDJSON data to Dune |
data-upload.md |
If your task spans multiple categories, read all relevant files. Do not guess table names or query patterns — the references contain critical details (e.g., specialized tables, anti-patterns) that are not covered in this overview.
Quick Start
from dune_client.client import DuneClient
from dune_client.query import QueryBase
import os
client = DuneClient(api_key=os.environ['DUNE_API_KEY'])
# Execute a query
result = client.run_query(query=QueryBase(query_id=123456), performance='medium', ping_frequency=5)
print(f"Rows: {len(result.result.rows)}")
# Get cached result (no re-execution)
result = client.get_latest_result(query_id=123456)
# Get/update SQL
sql = client.get_query(123456).sql
client.update_query(query_id=123456, query_sql="SELECT ...")
# Upload CSV data (quick, overwrites existing)
client.upload_csv(
data="col1,col2\nval1,val2",
description="My data",
table_name="my_table",
is_private=True
)
# Create table + insert (supports append)
client.create_table(
namespace="my_user",
table_name="my_table",
schema=[{"name": "col1", "type": "varchar"}, {"name": "col2", "type": "double"}],
is_private=True
)
import io
client.insert_data(
namespace="my_user",
table_name="my_table",
data=io.BytesIO(b"col1,col2\nabc,1.5"),
content_type="text/csv"
)
Subscription Tiers
| Method |
Description |
Plan |
run_query |
Execute saved query (supports {{param}}) |
Free |
run_sql |
Execute SQL directly (no params) |
Plus |
Key Concepts
dex.trades vs dex_aggregator.trades
| Table |
Use Case |
Volume |
dex.trades |
Per-pool analysis |
⚠️ Inflated ~30% (multi-hop counted multiple times) |
dex_aggregator.trades |
User/wallet analysis |
Accurate |
⚠️ For wallet/address analysis, use dex_aggregator.trades with tx_to matching router addresses from dune.lz_web3.dataset_crypto_wallet_router. Do not use labels.all for wallet router lookups. See wallet-analysis.md for full patterns.
Solana has no dex_aggregator_solana.trades. Dedupe by tx_id:
SELECT tx_id, MAX(amount_usd) as amount_usd
FROM dex_solana.trades
GROUP BY tx_id
Data Freshness
| Layer |
Delay |
Example |
| Raw |
< 1 min |
ethereum.transactions, solana.transactions |
| Decoded |
15-60 sec |
uniswap_v3_ethereum.evt_Swap |
| Curated |
~1 hour+ |
dex.trades, dex_solana.trades |
Query previous day's data after UTC 12:00 for completeness.
References
Detailed documentation is organized in the references/ directory:
| File |
Description |
| table-discovery.md |
Table discovery: search tables by name, inspect schema/columns, list schemas and uploads |
| query-execution.md |
API patterns: execute, update, cache, multi-day fetch, credits tracking, subqueries |
| common-tables.md |
Quick reference of commonly used tables: raw, decoded, curated, community data |
| sql-optimization.md |
SQL optimization: CTE, JOIN strategies, array ops, partition pruning |
| wallet-analysis.md |
Wallet tracking: Solana/EVM queries, multi-chain aggregation, fee analysis |
| data-upload.md |
Data upload: CSV/NDJSON upload, create table, insert data, manage tables, credits |
1---
2name: dune-analytics-api
3version: 1.1.2
4description: "Dune Analytics API for blockchain data queries. Use for: (1) Discovering tables and inspecting schemas, (2) Executing/refreshing Dune queries, (3) SQL query optimization for Solana/EVM chains, (4) Understanding dex.trades vs dex_aggregator.trades, (5) Working with Solana transactions and log parsing, (6) Managing query parameters and results, (7) Uploading CSV/NDJSON data to Dune tables, (8) Finding decoded tables by contract address, (9) Searching Dune documentation. Triggers on: Dune query, blockchain data, DEX trades, Solana transactions, on-chain analytics, query optimization, data upload, CSV upload, table discovery, find table, schema inspection, contract address lookup, decoded tables, search docs."
5homepage: https://github.com/LZ-Web3/dune-analytics-api-skills
6metadata:
7 source: https://skillmd.com/skills/modbender/dune-analytics-api
8 clawdbot:
9 emoji: "📊"
10 requires:
11 bins:
12 - python3
13 env:
14 - DUNE_API_KEY
15 primaryEnv: DUNE_API_KEY
16 files:
17 - "references/*"
18---
19
20# Dune Analytics API
21
22A skill for querying and analyzing blockchain data via the [Dune Analytics](https://dune.com) API.
23
24## Setup
25
26```bash
27pip install dune-client
28```
29
30Set `DUNE_API_KEY` via environment variable, `.env` file, or agent config.
31
32## ⚠️ Usage Rules
33
341. **Read before writing SQL** — Select and read the relevant reference files (see [Reference Selection](#reference-selection)) **before** writing any query. Do not skip this step.
352. **Prefer Private Queries** — Try `is_private=True` first. Fall back to public if it fails (free plan), and notify the user.
363. **Don't create duplicates** — Reuse/update existing queries unless explicitly asked to create new ones.
374. **Confirm before updating** — Ask the user before modifying an existing query.
385. **Track credits** — Report credits consumed after each execution. See [query-execution.md](references/query-execution.md#credits-tracking).
39
40## Reference Selection
41
42**Before writing any SQL, route to the correct reference file(s) based on your task:**
43
44| Task involves... | Read this reference |
45|-----------------|-------------------|
46| Finding tables / inspecting schema / discovering protocols | [table-discovery.md](references/table-discovery.md) |
47| Finding decoded tables by contract address | [table-discovery.md](references/table-discovery.md#search-tables-by-contract-address) |
48| Searching Dune documentation / guides / examples | [table-discovery.md](references/table-discovery.md#search-dune-documentation) |
49| Wallet / address tracking / router identification | [wallet-analysis.md](references/wallet-analysis.md) |
50| Table selection / common table names | [common-tables.md](references/common-tables.md) |
51| SQL performance / complex joins / array ops | [sql-optimization.md](references/sql-optimization.md) |
52| API calls / execution / caching / parameters | [query-execution.md](references/query-execution.md) |
53| Uploading CSV/NDJSON data to Dune | [data-upload.md](references/data-upload.md) |
54
55If your task spans multiple categories, read **all** relevant files. Do not guess table names or query patterns — the references contain critical details (e.g., specialized tables, anti-patterns) that are not covered in this overview.
56
57## Quick Start
58
59```python
60from dune_client.client import DuneClient
61from dune_client.query import QueryBase
62import os
63
64client = DuneClient(api_key=os.environ['DUNE_API_KEY'])
65
66# Execute a query
67result = client.run_query(query=QueryBase(query_id=123456), performance='medium', ping_frequency=5)
68print(f"Rows: {len(result.result.rows)}")
69
70# Get cached result (no re-execution)
71result = client.get_latest_result(query_id=123456)
72
73# Get/update SQL
74sql = client.get_query(123456).sql
75client.update_query(query_id=123456, query_sql="SELECT ...")
76
77# Upload CSV data (quick, overwrites existing)
78client.upload_csv(
79 data="col1,col2\nval1,val2",
80 description="My data",
81 table_name="my_table",
82 is_private=True
83)
84
85# Create table + insert (supports append)
86client.create_table(
87 namespace="my_user",
88 table_name="my_table",
89 schema=[{"name": "col1", "type": "varchar"}, {"name": "col2", "type": "double"}],
90 is_private=True
91)
92import io
93client.insert_data(
94 namespace="my_user",
95 table_name="my_table",
96 data=io.BytesIO(b"col1,col2\nabc,1.5"),
97 content_type="text/csv"
98)
99```
100
101## Subscription Tiers
102
103| Method | Description | Plan |
104|--------|-------------|------|
105| `run_query` | Execute saved query (supports `{{param}}`) | Free |
106| `run_sql` | Execute SQL directly (no params) | Plus |
107
108## Key Concepts
109
110### dex.trades vs dex_aggregator.trades
111
112| Table | Use Case | Volume |
113|-------|----------|--------|
114| `dex.trades` | Per-pool analysis | ⚠️ Inflated ~30% (multi-hop counted multiple times) |
115| `dex_aggregator.trades` | User/wallet analysis | Accurate |
116
117> ⚠️ **For wallet/address analysis**, use `dex_aggregator.trades` with `tx_to` matching router addresses from `dune.lz_web3.dataset_crypto_wallet_router`. Do **not** use `labels.all` for wallet router lookups. See [wallet-analysis.md](references/wallet-analysis.md) for full patterns.
118
119Solana has no `dex_aggregator_solana.trades`. Dedupe by `tx_id`:
120```sql
121SELECT tx_id, MAX(amount_usd) as amount_usd
122FROM dex_solana.trades
123GROUP BY tx_id
124```
125
126### Data Freshness
127
128| Layer | Delay | Example |
129|-------|-------|---------|
130| Raw | < 1 min | `ethereum.transactions`, `solana.transactions` |
131| Decoded | 15-60 sec | `uniswap_v3_ethereum.evt_Swap` |
132| Curated | ~1 hour+ | `dex.trades`, `dex_solana.trades` |
133
134Query previous day's data after **UTC 12:00** for completeness.
135
136## References
137
138Detailed documentation is organized in the `references/` directory:
139
140| File | Description |
141|------|-------------|
142| [table-discovery.md](references/table-discovery.md) | Table discovery: search tables by name, inspect schema/columns, list schemas and uploads |
143| [query-execution.md](references/query-execution.md) | API patterns: execute, update, cache, multi-day fetch, credits tracking, subqueries |
144| [common-tables.md](references/common-tables.md) | Quick reference of commonly used tables: raw, decoded, curated, community data |
145| [sql-optimization.md](references/sql-optimization.md) | SQL optimization: CTE, JOIN strategies, array ops, partition pruning |
146| [wallet-analysis.md](references/wallet-analysis.md) | Wallet tracking: Solana/EVM queries, multi-chain aggregation, fee analysis |
147| [data-upload.md](references/data-upload.md) | Data upload: CSV/NDJSON upload, create table, insert data, manage tables, credits |