Bun · Elysia · Eden · OpenAPI · MCP

U.S. energy data,
ready for agents.

A typed, self-describing wrapper around the EIA APIv2. One query endpoint, 15 data domains and a built-in MCP server — running locally in minutes.

$ git clone https://github.com/maxoliverbr/eia-api-ts
# annual coal production & price, 2020–2024
curl -X POST localhost:3000/query \
  -H 'content-type: application/json' \
  -d '{"domain":"coal","route":"aggregate-production",
       "frequency":"annual","data":["production","price"],
       "start":"2020","end":"2024"}'

# → { "response": { "total": N, "data": [ … ] } }

Built for how agents work

Discover, inspect, query — without hard-coding a single energy domain.

🔎

Self-describing

/domains, /search and facet endpoints expose use cases, tags and example queries at runtime.

⚡

One query shape

A single POST /query fetches coal, electricity, natural gas, STEO, SEDS and more.

📘

Agent playbook

/guide ships workflows, decision trees and recipes — fetch once, use as a system prompt.

🧠

Energy ontology

An RDF-style graph bridges “Coal emits CO₂” to concrete EIA domains. Export as JSON-LD or Turtle.

🛡️

Key stays server-side

Caching, request spacing, 429 back-off and normalized errors protect your EIA key.

🧩

End-to-end types

TypeBox schemas flow into routes and Eden Treaty clients. Zero any.

📗

OpenAPI docs

Interactive docs at /openapi and a JSON spec at /openapi/json, generated from the code.

🔌

MCP server

Set MCP_ENABLED=true for 9 tools and 2 resources at /mcp.

How it fits together

One small server between your code and the EIA. REST and MCP share the same validated core.

CLIENTSTHIS APP (BUN + ELYSIA)UPSTREAM Your codeHTTP / curlAI agents Eden treaty<App>POST /query · GET /domainsMCP: Claude, Cursor RoutesValidate inputBuild EIA requestCache · back-off /query · /domains · /guide · /mcpTypeBox · 422 on bad paramsdomain → route → facets EIA APIv2 api.eia.gov/v2API key

Type-safe, from query to response

The server's types flow straight into your client. No codegen, no hand-written interfaces.

import { treaty } from '@elysiajs/eden'
import type { App } from './src'

const api = treaty<App>('localhost:3000')

const { data, error } = await api.query.post({
  domain: 'coal',
  route: 'aggregate-production',
  frequency: 'annual',
  data: ['production'],
})

// typed, autocompleted
data?.response.data[0]

// domain: 'nope' is a compile error
// and a 422 at runtime

Requests

Bodies are validated at runtime and typed from the same TypeBox schema.

Responses

Normalized EIA responses with typed errors — no more guessing at upstream shapes.

Schemas that match reality

Domains, routes, facets and frequencies are discovered from the live EIA metadata.

Errors

429s are backed off and retried; everything else surfaces as a typed error.

Documented automatically

The OpenAPI spec is generated from the same schemas, so it can't drift from the code.

# start the API, then open the docs
bun dev
http://localhost:3000/openapi       # Swagger UI
http://localhost:3000/openapi/json  # OpenAPI 3.1
# generate a client in any language
bunx openapi-typescript spec.json -o api.d.ts
bunx @openapitools/openapi-generator-cli \
  generate -i spec.json -g python -o ./client

Examples with a purpose

Discover first, then query. Every endpoint is plain HTTP.

# search by topic
curl -X POST localhost:3000/search \
  -H 'content-type: application/json' \
  -d '{"q": "solar"}'
# → domains, routes, tags, example queries
# filter by facet
curl -X POST localhost:3000/query \
  -H 'content-type: application/json' \
  -d '{"domain":"electricity","route":"retail-sales",
       "frequency":"monthly","data":["price"],
       "facets":{"stateid":["TX"],"sectorid":["RES"]}}'
# list domains / fetch the agent playbook
curl localhost:3000/domains
curl localhost:3000/guide
# run the bundled examples
bun run examples/02-discovery.ts
bun run examples/06-query-post-basic.ts
bun run examples/agent-usage.ts

Plug into any MCP client

Set MCP_ENABLED=true and the same surface is exposed at /mcp as 9 tools and 2 resources.

Claude DesktopCursorMastraLangChainEden Treatyplain fetch / curl

Quick start

Install, configure, query.

# run it
bun install
cp .env.example .env
# set EIA_API_KEY in .env
bun dev   # http://localhost:3000
# discover, then query
curl localhost:3000/domains
curl -X POST localhost:3000/query \
  -H 'content-type: application/json' \
  -d '{"domain":"coal","route":"aggregate-production",
       "frequency":"annual","data":["production"]}'

This site is static documentation. The API is self-hosted and needs your own free EIA key (get one here), which must never be published in a browser.