Transport for the eCFR API: the settings resolver (per-call opts first, then context variables, then defaults), a typed HttpError, gzip-aware GETs, and query-string building that drops unset values.

opts keyContext variableDefault
base-urlecfr.base-urlhttps://www.ecfr.gov/api

Every request sends accept-encoding: gzip, because the full-text endpoint answers 406 without it. ::hot::http doesn't decompress, so body-text decodes the body when content-encoding says gzip and reads it as it is otherwise.

eCFR takes no credential, so a per-call base-url needs none.

Context Vars

Optional
  • ecfr.base-url

Values

BASE_URL

BASE_URL: Str "https://www.ecfr.gov/api"

PACKAGE_AGENT

PACKAGE_AGENT: Str "hot-dev-ecfr-pkg/0.1 (+https://hot.dev)"

SOURCE

SOURCE: Str "ecfr"

Functions

body-text

fn (response: Any): Any

A response body as text (pure). Decompresses it when content-encoding says gzip, since a server can also answer uncompressed. Bad gzip is an Err.

Example

::ecfr::api/body-text({headers: {}, body: "<DIV8/>"})  // "<DIV8/>"

get-json

fn (url: Str): Any

GET a JSON resource and return the decoded body, or an Err carrying HttpError.

Example

body ::ecfr::api/get-json("https://www.ecfr.gov/api/versioner/v1/titles.json")
length(body.titles)

get-json-or-null

fn (url: Str): Any

get-json, but a 404 gives null instead of an Err.

Example

tree ::ecfr::api/get-json-or-null("https://www.ecfr.gov/api/versioner/v1/structure/2026-11-02/title-31.json")
if(is-null(tree), "no such title", tree.label)

get-xml-or-null

fn (url: Str): Any

GET an XML resource and return its text, null on a 404, or an Err carrying HttpError.

Example

xml ::ecfr::api/get-xml-or-null("https://www.ecfr.gov/api/versioner/v1/full/2026-11-02/title-31.xml?section=1010.230")
if(is-null(xml), null, ::ecfr/parse-full(xml).citation)

given

fn (v: Any): Bool

True for a non-null value that is not a blank string.

Example

::ecfr::api/given("1010")  // true
::ecfr::api/given("  ")  // false

html-error

fn (response: ::hot::http/HttpResponse, url: Str): HttpError

Build an HttpError for an HTML page served in place of API data. Exposed for testing.

Example

response {status: 200, headers: {content-type: "text/html"}, body: "<html></html>"}
e ::ecfr::api/html-error(response, "https://www.ecfr.gov/api/versioner/v1/titles.json")
e.status  // 200
contains(e.message, "got an HTML page")  // true

http-error

fn (response: ::hot::http/HttpResponse, url: Str): HttpError

Build an HttpError from a non-2xx response, decoding a gzip error body. Exposed for testing; every request uses it.

Example

response {status: 404, headers: {}, body: {error: "No matching content found."}}
e ::ecfr::api/http-error(response, "https://www.ecfr.gov/api/versioner/v1/titles.json")
e.status  // 404
e.message  // "ecfr: HTTP 404: No matching content found."

is-html

fn (response: Any): Bool

True when a response is an HTML page (pure). No endpoint here serves HTML, so a 200 page means the request was sent somewhere else. eCFR redirects clients it flags as automated to a CAPTCHA page, and ::hot::http follows the redirect.

Example

::ecfr::api/is-html({headers: {content-type: "text/html; charset=utf-8"}})  // true
::ecfr::api/is-html({headers: {content-type: "application/json"}})  // false

is-not-found

fn (e: Any): Bool

True when e is an HttpError with status 404.

Example

::ecfr::api/is-not-found({status: 404})  // true
::ecfr::api/is-not-found({status: 500})  // false
::ecfr::api/is-not-found("timed out")  // false

is-past-date

fn (e: Any): Bool

True when e is the 404 eCFR gives for a date past a title's current date. It isn't an empty lookup: the section may well exist, and today is usually past the current date.

Example

e {status: 404, body: {error: "2030-01-01 is past the title's most recent issue date"}}
::ecfr::api/is-past-date(e)  // true
::ecfr::api/is-past-date({status: 404, body: {error: "No matching content found."}})  // false

null-on-404

fn (out: Result): Any

Null for a 404 Err, so a lookup that finds nothing isn't a failure. is-past-date errors and every other Err pass through.

Example

::ecfr::api/null-on-404(err({status: 404}))  // null
::ecfr::api/null-on-404("<DIV8/>")  // "<DIV8/>"

path-segment

fn (value: Any): Str

Percent-encode one path segment.

Example

::ecfr::api/path-segment("part 10")  // "part%2010"
::ecfr::api/path-segment(31)  // "31"

query-string

fn (params: Map): Str

Build a ?a=1&b=2 suffix from a Map, dropping null and blank values (pure); "" when nothing remains. Keys may contain brackets (hierarchy[title]) and are encoded.

Example

::ecfr::api/query-string({date: "2026-11-02", part: null})  // "?date=2026-11-02"
::ecfr::api/query-string({part: ""})  // ""

request

fn (url: Str, headers: Map): ::hot::http/HttpResponse

GET an eCFR URL with headers and accept-encoding: gzip, returning the HttpResponse on 2xx or an Err carrying HttpError. An HTML page is an Err too. A transport failure passes through as its own Err, including a response over http.max-response-bytes.

Example

response ::ecfr::api/request("https://www.ecfr.gov/api/versioner/v1/titles.json", {Accept: "application/json"})
::ecfr::api/body-text(response)

resolve

fn (opts: Map): Map

Resolve settings for one call: {base-url}. A value in opts wins over context.

Example

cfg ::ecfr::api/resolve({base-url: "https://ecfr-proxy.example/api"})

Types

HttpError

HttpError type {
    source: Str,
    status: Int,
    url: Str,
    headers: Map,
    body: Any,
    retry-after: Int?,
    message: Str
}

A failed eCFR response. source is "ecfr"; url the requested URL (eCFR takes no credentials in URLs); retry-after the parsed Retry-After seconds on 429s; message a one-line summary that never includes credentials. status can be 200 when the site served an HTML page in place of API data (see is-html).