One Health Atlas documentation
View source

Integration recipes

Load county observations into a table, page until next_page_token is null, and retry 429 and 503.

Send these scripts to https://api.onehealthatlas.org. Public reads need no API key. Leave the ATLAS_API_BASE_URL environment variable unset for production. Set it only when the script should use another origin.

Each script keeps county FIPS as a 5-character string, leaves a null value null, and prints release_id with the rows. release_id names the data release. It is not the response time. A 400, 404, or 422 is not retried. 429 and 503 wait for Retry-After when that header is present. Without it, the script waits 1 second, doubles after each failure up to 60 seconds, adds jitter, and stops after 4 attempts. Read the media type before parsing a problem body. Do not invent a code when the media type is not application/problem+json.

Interpret with care

A null value is not zero. MISSING, SUPPRESSED, and UNAVAILABLE stay null. A case floor is not a diagnosis or an individual risk. Read the limit in Evidence, provenance, and uncertainty.

Codes, retries, and value states are on Errors and retries. Parameters and the observation schema are on Observations.

The samples below are from release governed-2026-10-15-january-climate-prod. The Python and TypeScript snippets print the captured sample. The R scripts print the same columns.

Load several counties

Request case_count_floor_2023 for FIPS 01001, 08001, and 36061 in 2023. Load the rows into a table. 01001 keeps its leading zero. In this release, 01001 and 08001 are MISSING and value is null. 36061 is OBSERVED.

Python uses httpx and pandas. R uses httr2 and dplyr. TypeScript uses fetch.

Client
county-table.py
import json
import os
import random
import time

import httpx
import pandas as pd

NULL_STATES = {"MISSING", "SUPPRESSED", "UNAVAILABLE"}
MAX_ATTEMPTS = 4
BASE_URL = os.environ.get(
    "ATLAS_API_BASE_URL", "https://api.onehealthatlas.org"
).rstrip("/")


def retry_delay_seconds(header: str | None, attempt: int) -> float:
    if header is not None and header.strip() != "":
        seconds = float(header)
        if seconds >= 0:
            return seconds
    base = min(60.0, float(2 ** (attempt - 1)))
    return min(60.0, base + random.uniform(0, min(1.0, base * 0.1)))


def main() -> str:
    notes, rows = fetch_observations(
        [
            ("measure_id", "case_count_floor_2023"),
            ("geography_type", "county"),
            ("geography_id", "01001"),
            ("geography_id", "08001"),
            ("geography_id", "36061"),
            ("year", "2023"),
        ]
    )
    frame = table_frame(rows)
    assert_fips(frame)
    assert_null_states(frame)
    return "".join(notes) + render_table(frame)


def fetch_observations(
    params: list[tuple[str, str]],
) -> tuple[list[str], list[dict[str, object]]]:
    notes: list[str] = []
    with httpx.Client(timeout=30.0) as client:
        for attempt in range(1, MAX_ATTEMPTS + 1):
            response = client.get(f"{BASE_URL}/v1/observations", params=params)
            if response.status_code in {429, 503}:
                code = problem_code(response)
                delay = retry_delay_seconds(
                    response.headers.get("Retry-After"), attempt
                )
                code_text = f" {code}" if code else ""
                notes.append(
                    f"retry: {response.status_code}{code_text} {format_delay(delay)}\n"
                )
                if attempt == MAX_ATTEMPTS:
                    raise RuntimeError(
                        f"Atlas HTTP {response.status_code} after {MAX_ATTEMPTS} attempts"
                    )
                time.sleep(delay)
                continue
            if response.status_code >= 400:
                raise RuntimeError(
                    f"Atlas HTTP {response.status_code} {problem_detail(response)}"
                )
            return notes, read_rows(response)
    raise RuntimeError("Atlas retry loop ended")


def read_rows(response: httpx.Response) -> list[dict[str, object]]:
    assert_media(response, "application/json")
    body = response.json()
    return [read_observation(item) for item in body["data"]]


def read_observation(item: dict[str, object]) -> dict[str, object]:
    geography = item["geography"]
    if not isinstance(geography, dict):
        raise RuntimeError("Atlas observation is missing geography")
    return {
        "geography_id": geography["geography_id"],
        "release_id": item["release_id"],
        "value": item["value"],
        "value_state": item["value_state"],
    }


def table_frame(rows: list[dict[str, object]]) -> pd.DataFrame:
    frame = pd.DataFrame.from_records(
        rows,
        columns=["geography_id", "value", "value_state", "release_id"],
    )
    frame["geography_id"] = pd.Series(frame["geography_id"], dtype="string")
    frame["value"] = pd.Series(
        [None if value is None else value for value in rows_value(rows)],
        dtype="object",
    )
    frame["value_state"] = pd.Series(frame["value_state"], dtype="string")
    frame["release_id"] = pd.Series(frame["release_id"], dtype="string")
    return frame


def rows_value(rows: list[dict[str, object]]) -> list[object]:
    return [row["value"] for row in rows]


def assert_fips(frame: pd.DataFrame) -> None:
    for geography_id in frame["geography_id"].tolist():
        if not isinstance(geography_id, str) or len(geography_id) != 5:
            raise RuntimeError(
                f"FIPS must stay a 5-character string, got {geography_id!r}"
            )
        if not geography_id.isdigit():
            raise RuntimeError(
                f"FIPS must stay a 5-character string, got {geography_id!r}"
            )


def assert_null_states(frame: pd.DataFrame) -> None:
    for row in frame.to_dict("records"):
        if row["value_state"] not in NULL_STATES:
            continue
        value = row["value"]
        if value is None or (not isinstance(value, str) and pd.isna(value)):
            continue
        raise RuntimeError(
            f"{row['value_state']} for {row['geography_id']} was {value!r}, not null"
        )


def render_table(frame: pd.DataFrame) -> str:
    release_id = frame["release_id"].iloc[0]
    lines = [
        f"release_id: {release_id}",
        "geography_id,value,value_state,release_id",
    ]
    for row in frame.to_dict("records"):
        lines.append(
            f"{row['geography_id']},{format_value(row['value'])},{row['value_state']},{row['release_id']}"
        )
    return "\n".join(lines) + "\n"


def format_value(value: object) -> str:
    if value is None or (not isinstance(value, str) and pd.isna(value)):
        return "null"
    if isinstance(value, bool):
        raise RuntimeError("Atlas value was a boolean")
    if isinstance(value, int):
        return str(value)
    if isinstance(value, float):
        if value.is_integer():
            return str(int(value))
        return json.dumps(value)
    if isinstance(value, str):
        return value
    raise RuntimeError(f"Unexpected Atlas value {value!r}")


def format_delay(delay: float) -> str:
    if float(delay).is_integer():
        return str(int(delay))
    return str(delay)


def problem_code(response: httpx.Response) -> str | None:
    problem = problem_body(response)
    code = problem.get("code") if problem else None
    return code if isinstance(code, str) else None


def problem_detail(response: httpx.Response) -> str:
    problem = problem_body(response)
    detail = problem.get("detail") if problem else None
    return detail if isinstance(detail, str) else ""


def problem_body(response: httpx.Response) -> dict[str, object] | None:
    if media_type(response) != "application/problem+json":
        return None
    body = response.json()
    if isinstance(body, dict):
        return body
    return None


def assert_media(response: httpx.Response, expected: str) -> None:
    actual = media_type(response)
    if actual != expected:
        raise RuntimeError(f"Atlas Content-Type was {actual or 'missing'}")


def media_type(response: httpx.Response) -> str | None:
    header = response.headers.get("Content-Type")
    if header is None:
        return None
    return header.split(";", 1)[0].strip().lower()


if __name__ == "__main__":
    print(main(), end="")

Expected output for the captured sample:

county-table.txt
release_id: governed-2026-10-15-january-climate-prod
geography_id,value,value_state,release_id
01001,null,MISSING,governed-2026-10-15-january-climate-prod
08001,null,MISSING,governed-2026-10-15-january-climate-prod
36061,1114,OBSERVED,governed-2026-10-15-january-climate-prod

Page until the token is null

Request January 2025 average temperature for FIPS 36061 with page_size 2. Send meta.next_page_token back as page_token with the same filters. Stop when next_page_token is null. Do not parse the token. The loop also stops after 100 pages if a token never clears.

The captured sample has two pages. The second page in the sample has next_page_token null, so the script stops after 2025-01-04. A live January query keeps paging until the API returns null.

Client
page-collection.py
import json
import os
import random
import time

import httpx
import pandas as pd

NULL_STATES = {"MISSING", "SUPPRESSED", "UNAVAILABLE"}
MAX_ATTEMPTS = 4
MAX_PAGES = 100
BASE_URL = os.environ.get(
    "ATLAS_API_BASE_URL", "https://api.onehealthatlas.org"
).rstrip("/")


def main() -> str:
    filters = [
        ("measure_id", "nclimgrid_tavg_county_day"),
        ("geography_type", "county"),
        ("geography_id", "36061"),
        ("year", "2025"),
        ("page_size", "2"),
    ]
    notes: list[str] = []
    rows: list[dict[str, object]] = []
    token: str | None = None
    pages = 0
    release_id = ""
    while True:
        if pages >= MAX_PAGES:
            raise RuntimeError(
                "Stopped after 100 pages because next_page_token was still set"
            )
        params = list(filters)
        if token is not None:
            params.append(("page_token", token))
        page_notes, page_rows, release_id, token = fetch_page(params)
        notes.extend(page_notes)
        rows.extend(page_rows)
        pages += 1
        if token is None:
            break
    frame = table_frame(rows)
    assert_fips(frame)
    assert_null_states(frame)
    lines = [
        *notes,
        f"release_id: {release_id}",
        f"pages: {pages}",
        "next_page_token: null",
        "period_start,geography_id,value,value_state,release_id",
    ]
    for row in frame.to_dict("records"):
        lines.append(
            f"{row['period_start']},{row['geography_id']},{format_value(row['value'])},{row['value_state']},{row['release_id']}"
        )
    return "\n".join(lines) + "\n"


def fetch_page(
    params: list[tuple[str, str]],
) -> tuple[list[str], list[dict[str, object]], str, str | None]:
    notes: list[str] = []
    with httpx.Client(timeout=30.0) as client:
        for attempt in range(1, MAX_ATTEMPTS + 1):
            response = client.get(f"{BASE_URL}/v1/observations", params=params)
            if response.status_code in {429, 503}:
                code = problem_code(response)
                delay = retry_delay_seconds(
                    response.headers.get("Retry-After"), attempt
                )
                code_text = f" {code}" if code else ""
                notes.append(
                    f"retry: {response.status_code}{code_text} {format_delay(delay)}"
                )
                if attempt == MAX_ATTEMPTS:
                    raise RuntimeError(
                        f"Atlas HTTP {response.status_code} after {MAX_ATTEMPTS} attempts"
                    )
                time.sleep(delay)
                continue
            if response.status_code >= 400:
                raise RuntimeError(
                    f"Atlas HTTP {response.status_code} {problem_detail(response)}"
                )
            rows, release_id, token = read_page(response)
            return notes, rows, release_id, token
    raise RuntimeError("Atlas retry loop ended")


def read_page(
    response: httpx.Response,
) -> tuple[list[dict[str, object]], str, str | None]:
    assert_media(response, "application/json")
    body = response.json()
    meta = body["meta"]
    token = meta["next_page_token"]
    if token is not None and not isinstance(token, str):
        raise RuntimeError("Atlas next_page_token is not a string or null")
    release_id = meta["release_id"]
    if not isinstance(release_id, str):
        raise RuntimeError("Atlas response is missing release_id")
    rows = [read_observation(item) for item in body["data"]]
    return rows, release_id, token


def read_observation(item: dict[str, object]) -> dict[str, object]:
    geography = item["geography"]
    if not isinstance(geography, dict):
        raise RuntimeError("Atlas observation is missing geography")
    return {
        "geography_id": geography["geography_id"],
        "period_start": item["period_start"],
        "release_id": item["release_id"],
        "value": item["value"],
        "value_state": item["value_state"],
    }


def table_frame(rows: list[dict[str, object]]) -> pd.DataFrame:
    frame = pd.DataFrame.from_records(
        rows,
        columns=[
            "period_start",
            "geography_id",
            "value",
            "value_state",
            "release_id",
        ],
    )
    frame["geography_id"] = pd.Series(frame["geography_id"], dtype="string")
    frame["value"] = pd.Series([row["value"] for row in rows], dtype="object")
    frame["value_state"] = pd.Series(frame["value_state"], dtype="string")
    frame["release_id"] = pd.Series(frame["release_id"], dtype="string")
    frame["period_start"] = pd.Series(frame["period_start"], dtype="string")
    return frame


def assert_fips(frame: pd.DataFrame) -> None:
    for geography_id in frame["geography_id"].tolist():
        if (
            not isinstance(geography_id, str)
            or len(geography_id) != 5
            or not geography_id.isdigit()
        ):
            raise RuntimeError(
                f"FIPS must stay a 5-character string, got {geography_id!r}"
            )


def assert_null_states(frame: pd.DataFrame) -> None:
    for row in frame.to_dict("records"):
        if row["value_state"] not in NULL_STATES:
            continue
        value = row["value"]
        if value is None or (not isinstance(value, str) and pd.isna(value)):
            continue
        raise RuntimeError(
            f"{row['value_state']} for {row['geography_id']} was {value!r}, not null"
        )


def format_value(value: object) -> str:
    if value is None or (not isinstance(value, str) and pd.isna(value)):
        return "null"
    if isinstance(value, bool):
        raise RuntimeError("Atlas value was a boolean")
    if isinstance(value, int):
        return str(value)
    if isinstance(value, float):
        if value.is_integer():
            return str(int(value))
        return json.dumps(value)
    if isinstance(value, str):
        return value
    raise RuntimeError(f"Unexpected Atlas value {value!r}")


def retry_delay_seconds(header: str | None, attempt: int) -> float:
    if header is not None and header.strip() != "":
        seconds = float(header)
        if seconds >= 0:
            return seconds
    base = min(60.0, float(2 ** (attempt - 1)))
    return min(60.0, base + random.uniform(0, min(1.0, base * 0.1)))


def format_delay(delay: float) -> str:
    if float(delay).is_integer():
        return str(int(delay))
    return str(delay)


def problem_code(response: httpx.Response) -> str | None:
    problem = problem_body(response)
    code = problem.get("code") if problem else None
    return code if isinstance(code, str) else None


def problem_detail(response: httpx.Response) -> str:
    problem = problem_body(response)
    detail = problem.get("detail") if problem else None
    return detail if isinstance(detail, str) else ""


def problem_body(response: httpx.Response) -> dict[str, object] | None:
    if media_type(response) != "application/problem+json":
        return None
    body = response.json()
    if isinstance(body, dict):
        return body
    return None


def assert_media(response: httpx.Response, expected: str) -> None:
    actual = media_type(response)
    if actual != expected:
        raise RuntimeError(f"Atlas Content-Type was {actual or 'missing'}")


def media_type(response: httpx.Response) -> str | None:
    header = response.headers.get("Content-Type")
    if header is None:
        return None
    return header.split(";", 1)[0].strip().lower()


if __name__ == "__main__":
    print(main(), end="")

Expected output for the captured sample:

page-collection.txt
release_id: governed-2026-10-15-january-climate-prod
pages: 2
next_page_token: null
period_start,geography_id,value,value_state,release_id
2025-01-01,36061,7.744015461888341,OBSERVED,governed-2026-10-15-january-climate-prod
2025-01-02,36061,6.788281929151528,OBSERVED,governed-2026-10-15-january-climate-prod
2025-01-03,36061,3.995322054093146,OBSERVED,governed-2026-10-15-january-climate-prod
2025-01-04,36061,1.6377173882177052,OBSERVED,governed-2026-10-15-january-climate-prod

Read value_state and retry

Request case_count_floor_2023 for FIPS 08001 and 36061. Read value only with value_state. On 429 or 503, wait for Retry-After, then send the same request again. The captured sample returns 429 with Retry-After: 0 and code RATE_LIMITED, then the rows. Rules for other statuses: Errors and retries. Value states: Errors and retries.

Client
value-state.py
import json
import os
import random
import time

import httpx
import pandas as pd

NULL_STATES = {"MISSING", "SUPPRESSED", "UNAVAILABLE"}
RETRYABLE_STATUSES = {429, 503}
MAX_ATTEMPTS = 4
BASE_URL = os.environ.get(
    "ATLAS_API_BASE_URL", "https://api.onehealthatlas.org"
).rstrip("/")


def retry_delay_seconds(header: str | None, attempt: int) -> float:
    if header is not None and header.strip() != "":
        seconds = float(header)
        if seconds >= 0:
            return seconds
    base = min(60.0, float(2 ** (attempt - 1)))
    return min(60.0, base + random.uniform(0, min(1.0, base * 0.1)))


def main() -> str:
    notes, rows = fetch_observations(
        [
            ("measure_id", "case_count_floor_2023"),
            ("geography_type", "county"),
            ("geography_id", "08001"),
            ("geography_id", "36061"),
            ("year", "2023"),
        ]
    )
    frame = table_frame(rows)
    assert_fips(frame)
    assert_null_states(frame)
    return "".join(notes) + render_table(frame)


def fetch_observations(
    params: list[tuple[str, str]],
) -> tuple[list[str], list[dict[str, object]]]:
    notes: list[str] = []
    with httpx.Client(timeout=30.0) as client:
        for attempt in range(1, MAX_ATTEMPTS + 1):
            response = client.get(f"{BASE_URL}/v1/observations", params=params)
            if response.status_code in RETRYABLE_STATUSES:
                code = problem_code(response)
                delay = retry_delay_seconds(
                    response.headers.get("Retry-After"), attempt
                )
                code_text = f" {code}" if code else ""
                notes.append(
                    f"retry: {response.status_code}{code_text} {format_delay(delay)}\n"
                )
                if attempt == MAX_ATTEMPTS:
                    raise RuntimeError(
                        f"Atlas HTTP {response.status_code} after {MAX_ATTEMPTS} attempts"
                    )
                time.sleep(delay)
                continue
            if response.status_code >= 400:
                raise RuntimeError(
                    f"Atlas HTTP {response.status_code} {problem_detail(response)}"
                )
            return notes, read_rows(response)
    raise RuntimeError("Atlas retry loop ended")


def read_rows(response: httpx.Response) -> list[dict[str, object]]:
    assert_media(response, "application/json")
    body = response.json()
    return [read_observation(item) for item in body["data"]]


def read_observation(item: dict[str, object]) -> dict[str, object]:
    geography = item["geography"]
    if not isinstance(geography, dict):
        raise RuntimeError("Atlas observation is missing geography")
    return {
        "geography_id": geography["geography_id"],
        "release_id": item["release_id"],
        "value": item["value"],
        "value_state": item["value_state"],
    }


def table_frame(rows: list[dict[str, object]]) -> pd.DataFrame:
    frame = pd.DataFrame.from_records(
        rows,
        columns=["geography_id", "value", "value_state", "release_id"],
    )
    frame["geography_id"] = pd.Series(frame["geography_id"], dtype="string")
    frame["value"] = pd.Series([row["value"] for row in rows], dtype="object")
    frame["value_state"] = pd.Series(frame["value_state"], dtype="string")
    frame["release_id"] = pd.Series(frame["release_id"], dtype="string")
    return frame


def assert_fips(frame: pd.DataFrame) -> None:
    for geography_id in frame["geography_id"].tolist():
        if (
            not isinstance(geography_id, str)
            or len(geography_id) != 5
            or not geography_id.isdigit()
        ):
            raise RuntimeError(
                f"FIPS must stay a 5-character string, got {geography_id!r}"
            )


def assert_null_states(frame: pd.DataFrame) -> None:
    for row in frame.to_dict("records"):
        if row["value_state"] not in NULL_STATES:
            continue
        value = row["value"]
        if value is None or (not isinstance(value, str) and pd.isna(value)):
            continue
        raise RuntimeError(
            f"{row['value_state']} for {row['geography_id']} was {value!r}, not null"
        )


def render_table(frame: pd.DataFrame) -> str:
    release_id = frame["release_id"].iloc[0]
    lines = [
        f"release_id: {release_id}",
        "geography_id,value,value_state,release_id",
    ]
    for row in frame.to_dict("records"):
        lines.append(
            f"{row['geography_id']},{format_value(row['value'])},{row['value_state']},{row['release_id']}"
        )
    return "\n".join(lines) + "\n"


def format_value(value: object) -> str:
    if value is None or (not isinstance(value, str) and pd.isna(value)):
        return "null"
    if isinstance(value, bool):
        raise RuntimeError("Atlas value was a boolean")
    if isinstance(value, int):
        return str(value)
    if isinstance(value, float):
        if value.is_integer():
            return str(int(value))
        return json.dumps(value)
    if isinstance(value, str):
        return value
    raise RuntimeError(f"Unexpected Atlas value {value!r}")


def format_delay(delay: float) -> str:
    if float(delay).is_integer():
        return str(int(delay))
    return str(delay)


def problem_code(response: httpx.Response) -> str | None:
    problem = problem_body(response)
    code = problem.get("code") if problem else None
    return code if isinstance(code, str) else None


def problem_detail(response: httpx.Response) -> str:
    problem = problem_body(response)
    detail = problem.get("detail") if problem else None
    return detail if isinstance(detail, str) else ""


def problem_body(response: httpx.Response) -> dict[str, object] | None:
    if media_type(response) != "application/problem+json":
        return None
    body = response.json()
    if isinstance(body, dict):
        return body
    return None


def assert_media(response: httpx.Response, expected: str) -> None:
    actual = media_type(response)
    if actual != expected:
        raise RuntimeError(f"Atlas Content-Type was {actual or 'missing'}")


def media_type(response: httpx.Response) -> str | None:
    header = response.headers.get("Content-Type")
    if header is None:
        return None
    return header.split(";", 1)[0].strip().lower()


if __name__ == "__main__":
    print(main(), end="")

Expected output for the captured sample:

value-state.txt
retry: 429 RATE_LIMITED 0
release_id: governed-2026-10-15-january-climate-prod
geography_id,value,value_state,release_id
08001,null,MISSING,governed-2026-10-15-january-climate-prod
36061,1114,OBSERVED,governed-2026-10-15-january-climate-prod

Last updated on