Skip to content

EIA

Explore EIA API v2 routes, facets and frequencies, then retrieve energy observations and legacy series with original dimensions and units

Connector software
Free and open source
Service credentials
Required
Use with an agent

Install the Orchard skill in your project to help your agent find connectors, configure them, and verify SQL results.

npx skills add Query-farm/vgi-skills --skill orchard

Copy this request into your coding agent to get started.

Use https://query.farm/products/orchard/connectors/eia/index.md to help me query EIA with DuckDB or Haybarn 2.0.1 and VGI protocol 3.4. Ask only for missing details about the result I need or whether I want local execution, self-hosting or Query Farm hosting. Check the connector's actual hosting availability, credentials and input requirements. Read one format of each needed reference and the exact object declarations before writing SQL. Use a released connector, and distinguish a saved example result from anything you actually execute. If you cannot run the query, tell me what remains to verify.

Track residential electricity prices in Colorado

Uses your connected account. Set up the connector and credentials to run it with your data.

SELECT period, value, units
FROM eia.main.observations('electricity/retail-sales', 'price', 'monthly', facets_json := '{"stateid":["CO"],"sectorid":["RES"]}', start_period := '2024-01', end_period := '2024-03')
ORDER BY period

Quick start

Choose where to run EIA, then follow the steps below.

The connector runs alongside DuckDB or Haybarn. No Query Farm account is needed.

1. Install the vgi extension

Run this in your SQL client. On DuckDB, keep FROM query_farm on LOAD in later sessions.

setup.sql
SET allow_extension_repositories = 'allowed';

CREATE EXTENSION REPOSITORY IF NOT EXISTS query_farm
    WITH PREFIX 'https://duckdb-extensions.query.farm';

INSTALL vgi FROM query_farm;
LOAD vgi FROM query_farm;

2. Attach the connector

Attach with uvx

Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.

ATTACH 'eia' AS eia (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-eia-worker'
);

3. Add service credentials

After attaching, create the secret for the service you want to query in the same SQL session. Replace the placeholders with your service settings.

eia_api credentials

EIA Open Data API key; register at https://www.eia.gov/opendata/.

Get these credentials →
secret.sql
CREATE SECRET eia_api (
    TYPE eia_api,
    api_key '<EIA_API_API_KEY>'  -- secret
);
Credential fields and requirements →

4. Run a query

query.sql
SELECT period, value, units
FROM eia.main.observations('electricity/retail-sales', 'price', 'monthly', facets_json := '{"stateid":["CO"],"sectorid":["RES"]}', start_period := '2024-01', end_period := '2024-03')
ORDER BY period
More example queries →
Technical details
Verified connector release
v0.2.0
Engine
2.0.1
VGI protocol
3.4.0
Verified on
December 1, 2026
Release notes on GitHub
Pin a version for production

When running the connector yourself, keep a specific release until you’re ready to upgrade. Replace the ATTACH statement above with this one and follow the same extension and credential setup.

Attach with uvx

Needs uv. uvx builds the worker from its release tag on first attach and reuses the cached environment after that.

ATTACH 'eia' AS eia (
  TYPE vgi,
  LOCATION 'uvx --from git+https://github.com/Query-farm/[email protected] vgi-eia-worker'
);

Overview

US Energy Information Administration energy data from EIA API v2, covering electricity, petroleum, natural gas, coal and renewables, as SQL tables with route and facet discovery and the original units. Needs a free EIA API key.

Catalog

Everything eia exposes once attached — read straight from the worker's released build.

eia 1 schema
main 8 functions

EIA API v2 as table functions: route, facet and frequency discovery plus energy observations.

Schema examples (11)
Examples (11)

Discover EIA routes

SELECT route, name
FROM eia.main.routes(route := 'electricity')
ORDER BY route;

Inspect EIA dataset metadata

SELECT name, start_period, end_period
FROM eia.main.metadata('electricity/retail-sales');

Pair the retail-sales dataset's default frequency with that frequency's native period format and the advertised period range, to know how to write start_period and end_period

SELECT m.name, m.default_frequency, f.period_format, m.start_period,
  m.end_period
FROM eia.main.metadata('electricity/retail-sales') AS m
JOIN eia.main.frequencies('electricity/retail-sales') AS f ON f.frequency = m.default_frequency
ORDER BY f.frequency;

Discover EIA frequencies

SELECT frequency, period_format
FROM eia.main.frequencies('electricity/retail-sales')
ORDER BY frequency;
7 more examples

Discover EIA measures

SELECT measure, units
FROM eia.main.measures('electricity/retail-sales')
ORDER BY measure;

Discover EIA dimensions

SELECT facet, description
FROM eia.main.facets('electricity/retail-sales')
ORDER BY facet;

Discover EIA dimension values

SELECT id, name
FROM eia.main.facet_values('electricity/retail-sales', 'sectorid')
ORDER BY id;

List every retail-sales sector ID and name with Colorado's 2024 annual average price, NULL for sectors with no published 2024 Colorado price

SELECT s.id AS sectorid, s.name AS sector, o.value AS cents_per_kwh
FROM eia.main.facet_values('electricity/retail-sales', 'sectorid') AS s
LEFT JOIN eia.main.observations('electricity/retail-sales', 'price', 'annual', facets_json := '{"stateid":["CO"]}', start_period := '2024', end_period := '2024') AS o ON o.dimensions_json ->> '$.sectorid' = s.id
ORDER BY s.id;

Read EIA observations

SELECT period, value, units
FROM eia.main.observations('electricity/retail-sales', 'price', 'monthly', facets_json := '{"stateid":["CO"],"sectorid":["RES"]}', start_period := '2024-01', end_period := '2024-03')
ORDER BY period;

Colorado residential 2024 electricity price weighted by monthly retail sales, beside the simple average of the 12 monthly prices (cents per kilowatt-hour)

WITH price AS (SELECT period, value AS cents_per_kwh FROM eia.main.observations('electricity/retail-sales', 'price', 'monthly', facets_json := '{"stateid":["CO"],"sectorid":["RES"]}', start_period := '2024-01', end_period := '2024-12')), sales AS (SELECT period, value AS million_kwh FROM eia.main.observations('electricity/retail-sales', 'sales', 'monthly', facets_json := '{"stateid":["CO"],"sectorid":["RES"]}', start_period := '2024-01', end_period := '2024-12')) SELECT count(*) AS months, round(sum(price.cents_per_kwh * sales.million_kwh) / sum(sales.million_kwh), 2) AS weighted_price, round(avg(price.cents_per_kwh), 2) AS simple_average_price
FROM price
JOIN sales USING (period)
ORDER BY months;

Read a legacy EIA series

SELECT period, value, units
FROM eia.main.series('ELEC.SALES.CO-RES.A', start_period := '2022', end_period := '2024')
ORDER BY period;
Table functions
facet_values (route VARCHAR, facet VARCHAR) 2 examples

List a dimension's value IDs, names and aliases. This endpoint has no documented pagination; the worker validates totalFacets and fails if the bounded response is incomplete.

Arguments
route VARCHAR
Relative metadata route, for example electricity/retail-sales; omit v2 and data.
facet VARCHAR
Dimension identifier returned by facets().
Returns columns
id VARCHAR
Facet value identifier, preserving leading zeroes.
name VARCHAR
Published facet value name.
alias VARCHAR
Published facet value alias.
Examples (2)

Discover EIA dimension values

SELECT id, name
FROM eia.main.facet_values('electricity/retail-sales', 'sectorid')
ORDER BY id;

List every retail-sales sector ID and name with Colorado's 2024 annual average price, NULL for sectors with no published 2024 Colorado price

SELECT s.id AS sectorid, s.name AS sector, o.value AS cents_per_kwh
FROM eia.main.facet_values('electricity/retail-sales', 'sectorid') AS s
LEFT JOIN eia.main.observations('electricity/retail-sales', 'price', 'annual', facets_json := '{"stateid":["CO"]}', start_period := '2024', end_period := '2024') AS o ON o.dimensions_json ->> '$.sectorid' = s.id
ORDER BY s.id;
Link
facets (route VARCHAR) 1 example

List dimension identifiers accepted as facets_json keys by observations(). Use facet_values() to discover their available string identifiers.

Arguments
route VARCHAR
Relative metadata route, for example electricity/retail-sales; omit v2 and data.
Returns columns
facet VARCHAR
Dimension identifier used as a facets_json key.
description VARCHAR
Published dimension description.
Example

Discover EIA dimensions

SELECT facet, description
FROM eia.main.facets('electricity/retail-sales')
ORDER BY facet;
Link
frequencies (route VARCHAR) 1 example

List the selected leaf dataset's supported frequency identifiers and native period formats. Use the frequency identifier as observations()'s third argument.

Arguments
route VARCHAR
Relative metadata route, for example electricity/retail-sales; omit v2 and data.
Returns columns
frequency VARCHAR
Frequency identifier accepted by observations().
description VARCHAR
Published frequency description.
query_code VARCHAR
Legacy frequency code, when supplied.
period_format VARCHAR
Native period format advertised by EIA.
Example

Discover EIA frequencies

SELECT frequency, period_format
FROM eia.main.frequencies('electricity/retail-sales')
ORDER BY frequency;
Link
measures (route VARCHAR) 1 example

List numeric data columns and published units for a leaf dataset. Select one measure per observations() call to keep source row counts and output row counts identical.

Arguments
route VARCHAR
Relative metadata route, for example electricity/retail-sales; omit v2 and data.
Returns columns
measure VARCHAR
Data column identifier accepted by observations().
name VARCHAR
Published measure alias or label.
units VARCHAR
Source units; values are not rescaled.
metadata_json VARCHAR
Complete source descriptor for this measure.
Example

Discover EIA measures

SELECT measure, units
FROM eia.main.measures('electricity/retail-sales')
ORDER BY measure;
Link
metadata (route VARCHAR) 2 examples

Return one metadata row containing source notes, period bounds and the complete response descriptor. The API request echo and its credential are excluded.

Arguments
route VARCHAR
Relative metadata route, for example electricity/retail-sales; omit v2 and data.
Returns columns
route VARCHAR
Requested relative metadata route.
name VARCHAR
Published dataset name.
description VARCHAR
Dataset scope and source notes.
start_period VARCHAR
Earliest native period advertised by EIA.
end_period VARCHAR
Latest native period advertised by EIA.
default_frequency VARCHAR
Default frequency advertised by the source.
metadata_json VARCHAR
Complete metadata response object; request echoes are excluded.
Examples (2)

Inspect EIA dataset metadata

SELECT name, start_period, end_period
FROM eia.main.metadata('electricity/retail-sales');

Pair the retail-sales dataset's default frequency with that frequency's native period format and the advertised period range, to know how to write start_period and end_period

SELECT m.name, m.default_frequency, f.period_format, m.start_period,
  m.end_period
FROM eia.main.metadata('electricity/retail-sales') AS m
JOIN eia.main.frequencies('electricity/retail-sales') AS f ON f.frequency = m.default_frequency
ORDER BY f.frequency;
Link
observations (route VARCHAR, measure VARCHAR, frequency VARCHAR, end_period := VARCHAR, facets_json := VARCHAR, max_rows := BIGINT, page_size := BIGINT, sort_direction := VARCHAR, start_offset := BIGINT, start_period := VARCHAR) 2 examples

Stream one measure at an explicit frequency. Required positional arguments are route, measure and frequency. All other arguments are named; their display order has no binding significance. Metadata validates filters and supplies dimension tie-breakers for stable sorting. Periods, units, missing markers and dimensions are preserved; source total changes or incomplete pages fail the query.

Positional arguments
route VARCHAR
Relative data route returned by routes(); omit v2 and data.
measure VARCHAR
One numeric data column returned by measures().
frequency VARCHAR
Explicit frequency identifier returned by frequencies().
Named arguments
end_period := VARCHAR
Inclusive native period upper bound; empty is unbounded. · default ""
facets_json := VARCHAR
Object of dimension names to string arrays, e.g. {"stateid":["CO"],"sectorid":["RES"]}. · default "{}"
max_rows := BIGINT
Explicit output cap; zero streams all matching observations. · default 0
page_size := BIGINT
Source rows requested per page, up to EIA's 5000-row ceiling. · default 1000
sort_direction := VARCHAR
Period and dimension sort direction; all facet identifiers break period ties. · default "asc"
start_offset := BIGINT
Zero-based source row offset at which to begin. · default 0
start_period := VARCHAR
Inclusive native period lower bound; empty is unbounded. · default ""
Returns columns
route VARCHAR
Requested dataset route or seriesid/legacy-identifier.
period VARCHAR
Native EIA observation period; no date is inferred.
frequency VARCHAR
Frequency reported by the response.
measure VARCHAR
Selected data column or the legacy series' inferred measure.
value DOUBLE
Numeric value in published units; NULL for documented missing/suppressed markers.
value_text VARCHAR
Original string or canonical text of a legacy numeric value.
value_status VARCHAR
numeric, missing, withheld or insignificant.
units VARCHAR
Per-row source units, without rescaling.
dimensions_json VARCHAR
All dimension IDs and labels from the original row.
source_json VARCHAR
Complete source observation object.
Examples (2)

Read EIA observations

SELECT period, value, units
FROM eia.main.observations('electricity/retail-sales', 'price', 'monthly', facets_json := '{"stateid":["CO"],"sectorid":["RES"]}', start_period := '2024-01', end_period := '2024-03')
ORDER BY period;

Colorado residential 2024 electricity price weighted by monthly retail sales, beside the simple average of the 12 monthly prices (cents per kilowatt-hour)

WITH price AS (SELECT period, value AS cents_per_kwh FROM eia.main.observations('electricity/retail-sales', 'price', 'monthly', facets_json := '{"stateid":["CO"],"sectorid":["RES"]}', start_period := '2024-01', end_period := '2024-12')), sales AS (SELECT period, value AS million_kwh FROM eia.main.observations('electricity/retail-sales', 'sales', 'monthly', facets_json := '{"stateid":["CO"],"sectorid":["RES"]}', start_period := '2024-01', end_period := '2024-12')) SELECT count(*) AS months, round(sum(price.cents_per_kwh * sales.million_kwh) / sum(sales.million_kwh), 2) AS weighted_price, round(avg(price.cents_per_kwh), 2) AS simple_average_price
FROM price
JOIN sales USING (period)
ORDER BY months;
Link
routes (route := VARCHAR) 1 example

List direct children of a parent route, or the API root when route is empty. Leaf datasets return no children; inspect metadata() and measures() to query them.

Named arguments
route := VARCHAR
Relative parent route; empty discovers the API root. · default ""
Returns columns
route VARCHAR
Relative child route, suitable for further discovery.
id VARCHAR
Child route identifier.
name VARCHAR
Published route name.
description VARCHAR
Published route description.
Example

Discover EIA routes

SELECT route, name
FROM eia.main.routes(route := 'electricity')
ORDER BY route;
Link
series (series_id VARCHAR, end_period := VARCHAR, max_rows := BIGINT, page_size := BIGINT, sort_direction := VARCHAR, start_offset := BIGINT, start_period := VARCHAR) 1 example

Translate one legacy series ID through the v2 seriesid endpoint and stream its observations. Frequency and the single measure are inferred from the response. Ambiguous measure metadata fails; use observations() for explicit selection.

Positional arguments
series_id VARCHAR
Legacy EIA series identifier, for example ELEC.SALES.CO-RES.A.
Named arguments
end_period := VARCHAR
Inclusive native period upper bound; empty is unbounded. · default ""
max_rows := BIGINT
Explicit output cap; zero streams all matching observations. · default 0
page_size := BIGINT
Source rows requested per page, up to EIA's 5000-row ceiling. · default 1000
sort_direction := VARCHAR
Period and dimension sort direction; all facet identifiers break period ties. · default "asc"
start_offset := BIGINT
Zero-based source row offset at which to begin. · default 0
start_period := VARCHAR
Inclusive native period lower bound; empty is unbounded. · default ""
Returns columns
route VARCHAR
Requested dataset route or seriesid/legacy-identifier.
period VARCHAR
Native EIA observation period; no date is inferred.
frequency VARCHAR
Frequency reported by the response.
measure VARCHAR
Selected data column or the legacy series' inferred measure.
value DOUBLE
Numeric value in published units; NULL for documented missing/suppressed markers.
value_text VARCHAR
Original string or canonical text of a legacy numeric value.
value_status VARCHAR
numeric, missing, withheld or insignificant.
units VARCHAR
Per-row source units, without rescaling.
dimensions_json VARCHAR
All dimension IDs and labels from the original row.
source_json VARCHAR
Complete source observation object.
Example

Read a legacy EIA series

SELECT period, value, units
FROM eia.main.series('ELEC.SALES.CO-RES.A', start_period := '2022', end_period := '2024')
ORDER BY period;
Link

Credentials

The secret type eia declares, as reported by the worker itself.

eia_api

How to get these credentials

EIA Open Data API key; register at https://www.eia.gov/opendata/.

secret.sql
CREATE SECRET eia_api (
    TYPE eia_api,
    api_key '<EIA_API_API_KEY>'  -- secret
);
Key Type Required Description
api_key secret VARCHAR — Your EIA Open Data API key.

The worker doesn't mark which keys are required.

Create the secret in the same session, after attaching eia — the worker registers its secret types at ATTACH. Keys marked secret are masked in duckdb_secrets(); angle-bracket values are placeholders. Read credentials from your environment or secret store in your application and pass them through your SQL client’s parameter bindings. Keep real values out of saved SQL and history. Native DuckDB and Haybarn do not provide a SQL getenv() function.

Try these queries

routes_columns — Show the columns routes() returns without an API key: binding the function with LIMIT 0 resolves its schema and makes no EIA API request

SELECT * FROM eia.main.routes(route := 'electricity') LIMIT 0;

Uses routes

Inspect EIA dataset metadata

SELECT name, start_period, end_period
FROM eia.main.metadata('electricity/retail-sales')

Uses metadata

Discover EIA frequencies

SELECT frequency, period_format
FROM eia.main.frequencies('electricity/retail-sales')
ORDER BY frequency

Uses frequencies

Discover EIA measures

SELECT measure, units
FROM eia.main.measures('electricity/retail-sales')
ORDER BY measure

Uses measures

Discover EIA dimensions

SELECT facet, description
FROM eia.main.facets('electricity/retail-sales')
ORDER BY facet

Uses facets

Discover EIA dimension values

SELECT id, name
FROM eia.main.facet_values('electricity/retail-sales', 'sectorid')
ORDER BY id

Uses facet_values

5 more examples in the Catalog, each listed under the table or function it uses.

EIA is an independent open-source connector from Query.Farm. It is not affiliated with, endorsed by or sponsored by the services, projects or organisations it works with. Product names, logos and trademarks belong to their respective owners.