Python module guide
CMS Marketplace client
Build households and places, search and cache plan records, inspect benefits, and understand the calculations implemented in this module.
Object API reference
API for cms_marketplace 0.4.1. Import examples use import cms_marketplace as cms. Entries describe the checked-in implementation; remote response values are not type-validated.
Start with configuration for API keys and import behavior, or worked examples for complete workflows. await in examples must run inside an async function.
Person
- class
cms_marketplace.Person(date_of_birth, is_pregnant=False, is_parent=False, uses_tobacco=False, aptc_eligible=True, gender=None, relationship=None, has_mec=None)¶ One person seeking coverage. Construct locally; no network request.
- Parameters
date_of_birth(date | datetime | str | int) — Birth date, supported date string, or integer age. Prefer a date when using the age property. Required.is_pregnant(bool) — Whether the person is pregnant. Default: False.is_parent(bool) — Whether the person is a parent. Default: False.uses_tobacco(bool) — Whether the person uses tobacco. Default: False.aptc_eligible(bool) — Whether to mark the person eligible for APTC in the request. Default: True.gender(str | None) — Optional gender. Use set_gender() for normalization. Default: None.relationship(str | None) — Relationship to the primary applicant. Default: None.has_mec(bool | None) — Optional minimum essential coverage flag. Default: None.
- Raises
ValueError— Unsupported birth-date input or unparseable date string.
person = cms.Person("1990-01-15") person.set_gender("f") payload = person.dict()
Attributes and properties
- attribute
date_of_birth¶ Date of birth or age. Constructor also accepts datetime and YYYY-MM-DD, MM/DD/YYYY, MM-DD-YYYY strings.
- Type
- date | int
- attribute
is_pregnant¶ Default False.
- Type
- bool
- attribute
is_parent¶ Default False.
- Type
- bool
- attribute
uses_tobacco¶ Default False.
- Type
- bool
- attribute
aptc_eligible¶ Default True.
- Type
- bool
- attribute
relationship¶ Default None; omitted from dict() when unset.
- Type
- optional values
- attribute
has_mec¶ Default None; omitted from dict() when unset.
- Type
- optional values
- property
age¶ Calculated from date of birth. Integer-age inputs fail here; use dict()["age"]. Leap-day dates can raise ValueError.
- Type
- int property
- Raises
AttributeError / ValueError— Integer-age input has no date attributes; February 29 may be invalid in the current year.
- property
gender¶ Raises ValueError when unset. Constructor does not normalize gender.
- Type
- str property
- Raises
ValueError— Gender has not been set.
Methods and supported operations
- method
set_gender(gender)¶ Normalize Male/Female or m/f, ignoring case. Invalid strings raise ValueError.
- Parameters
gender(str | None) — Optional gender. Use set_gender() for normalization. Required.
- Returns
- None
- Raises
ValueError— Gender string is not a supported Male/Female form.
- method
dict()¶ Serialize flags plus dob or age; include gender, relationship and has_mec only when supplied.
- Returns
- dict
Household
- class
cms_marketplace.Household(income=0, has_married_couple=False)¶ People and annual income used in a search.
- Parameters
income(number | str) — Annual household income; digit strings may contain commas. Default: 0.has_married_couple(bool) — Assign Spouse to the second person when their relationship is unset. Default: False.
household = cms.Household(income=42000) household.add_person(cms.Person("1990-01-15"))
Attributes and properties
- attribute
income¶ Annual income; numeric strings can contain commas.
- Type
- number
- attribute
has_married_couple¶ Default False; influences automatic relationship assignment.
- Type
- bool
Methods and supported operations
- method
add_person(person)¶ Require Person; append it and assign Self/Spouse when appropriate.
- Parameters
person(Person) — Person to append, retained by reference. Required.
- Returns
- None
- Raises
TypeError— person is not a Person.
- method
create_and_add_person(date_of_birth, is_pregnant=False, is_parent=False, uses_tobacco=False, aptc_eligible=True, gender=None, relationship=None, has_mec=None)¶ Construct and append a Person. Retrieve it via people[-1].
- Parameters
date_of_birth(date | datetime | str | int) — Birth date, supported date string, or integer age. Prefer a date when using the age property. Required.is_pregnant(bool) — Whether the person is pregnant. Default: False.is_parent(bool) — Whether the person is a parent. Default: False.uses_tobacco(bool) — Whether the person uses tobacco. Default: False.aptc_eligible(bool) — Whether to mark the person eligible for APTC in the request. Default: True.gender(str | None) — Optional gender. Use set_gender() for normalization. Default: None.relationship(str | None) — Relationship to the primary applicant. Default: None.has_mec(bool | None) — Optional minimum essential coverage flag. Default: None.
- Returns
- None
- method
dict()¶ Current income, married flag and serialized people.
- Returns
- dict
- operation
len(household)¶ Number of people.
- Returns
- int
Place
- class
cms_marketplace.Place(zipcode, state=None, countyfips=None, countyname=None)¶ ZIP, state and county for a request. Keep ZIP and FIPS as strings.
- Parameters
zipcode(str) — ZIP code; preserve leading zeroes. Required.state(str | None) — State abbreviation. Default: None.countyfips(str | None) — County FIPS code; preserve leading zeroes. Default: None.countyname(str | None) — County display name. Default: None.
place = cms.Place("33101", state="FL", countyfips="12086") payload = await place.full_dict()
Attributes and properties
- attribute
zipcode¶ Stored constructor values; only zipcode is required.
- Type
- str | None
- attribute
state¶ Stored constructor values; only zipcode is required.
- Type
- str | None
- attribute
countyfips¶ Stored constructor values; only zipcode is required.
- Type
- str | None
- attribute
countyname¶ Stored constructor values; only zipcode is required.
- Type
- str | None
- class attribute
FIPS_RETRIEVAL_METHOD¶ 0 HUD dataset (default), 1 CMS dataset, 2 CMS API, 3 combined datasets.
- Type
- class setting: int
- class attribute
MULTIPLE_RESULT_BEHAVIOR¶ 0 select one (default), 1 raise on ambiguity, 2 return all matches.
- Type
- class setting: int
- class attribute
DATA_FILE_NAME¶ Dataset file prefix; default countyfips.
- Type
- class setting: str
Methods and supported operations
- coroutine
full_dict()¶ zipcode/state/countyfips. Resolves missing state or FIPS; selects first ambiguous match. Does not persist resolved state/FIPS on the object.
- Returns
- dict
Usage:
await place.full_dict()
- coroutine · class-level
retrieve_county_fips(zipcode)¶ Lookup with selected backend. Normalize and check empty results before use.
- Parameters
zipcode(str) — ZIP code; preserve leading zeroes. Required.
- Returns
- Place | list[Place] | None
Usage:
await Place.retrieve_county_fips(zipcode)
- coroutine · class-level
retrieve_county_name(countyfips)¶ Lookup using selected backend; method 0 is not implemented.
- Parameters
countyfips(str | None) — County FIPS code; preserve leading zeroes. Required.
- Returns
- str | None
Usage:
await Place.retrieve_county_name(countyfips)
- coroutine · class-level
retrieve_county_fips_from_cms_api(zipcode)¶ Use CMS ZIP lookup endpoint. Result shape depends on matches and MULTIPLE_RESULT_BEHAVIOR.
- Parameters
zipcode(str) — ZIP code; preserve leading zeroes. Required.
- Returns
- Place | list[Place] | None
Usage:
await Place.retrieve_county_fips_from_cms_api(zipcode)
- coroutine · class-level
retrieve_county_fips_from_cms_dataset(zipcode)¶ Use CMS county-ZIP dataset; may download a local JSON file. Result shape depends on matches and MULTIPLE_RESULT_BEHAVIOR.
- Parameters
zipcode(str) — ZIP code; preserve leading zeroes. Required.
- Returns
- Place | list[Place] | None
Usage:
await Place.retrieve_county_fips_from_cms_dataset(zipcode)
- coroutine · class-level
retrieve_county_fips_from_hud_dataset(zipcode)¶ Use HUD-derived dataset; may download a local CSV file. Result shape depends on matches and MULTIPLE_RESULT_BEHAVIOR.
- Parameters
zipcode(str) — ZIP code; preserve leading zeroes. Required.
- Returns
- Place | list[Place] | None
Usage:
await Place.retrieve_county_fips_from_hud_dataset(zipcode)
- coroutine · class-level
retrieve_county_fips_from_combined_dataset(zipcode)¶ Use Combined HUD and CMS datasets; may download local files. Result shape depends on matches and MULTIPLE_RESULT_BEHAVIOR.
- Parameters
zipcode(str) — ZIP code; preserve leading zeroes. Required.
- Returns
- Place | list[Place] | None
Usage:
await Place.retrieve_county_fips_from_combined_dataset(zipcode)
- coroutine · class-level
retrieve_county_name_from_cms_dataset(countyfips)¶ Resolve a county name directly using cms dataset.
- Parameters
countyfips(str | None) — County FIPS code; preserve leading zeroes. Required.
- Returns
- str | None
Usage:
await Place.retrieve_county_name_from_cms_dataset(countyfips)
- coroutine · class-level
retrieve_county_name_from_cms_api(countyfips)¶ Resolve a county name directly using cms api.
- Parameters
countyfips(str | None) — County FIPS code; preserve leading zeroes. Required.
- Returns
- str | None
Usage:
await Place.retrieve_county_name_from_cms_api(countyfips)
Filter
- class
cms_marketplace.Filter()¶ Optional issuer filter for PlanSearchRequest.
issuer_filter = cms.Filter() issuer_filter.add_issuer("Example Issuer")
Attributes and properties
- attribute
issuers¶ Initially empty; issuer names are not deduplicated.
- Type
- list
Methods and supported operations
- method
add_issuer(issuer_name)¶ Append an issuer name.
- Parameters
issuer_name(str) — Issuer name to append to the filter. Required.
- Returns
- None
- method
dict()¶ {"issuers": issuers} when nonempty; otherwise {}.
- Returns
- dict
PlanSearchRequest
- class
cms_marketplace.PlanSearchRequest(place, household, market='Individual', year=None, filter=None, sort=None, csr_override=None)¶ Build a search from a Place and Household. Create a new request to refresh a cached search.
- Parameters
place(Place) — Location used for the request. Required.household(Household) — People and income used for the request. Required.market(str) — Marketplace market sent to the API. Default: 'Individual'.year(int | None) — Coverage year; None selects DEFAULT_YEAR. Default: None.filter(Filter | None) — Optional issuer filter. Default: None.sort(API value | None) — Sort value passed through without validation. Default: None.csr_override(API value | None) — CSR override passed through without validation. Default: None.
request = cms.PlanSearchRequest(place, household, year=2026) response = await request
Attributes and properties
- attribute
market¶ Default Individual.
- Type
- str
- attribute
year¶ None selects DEFAULT_YEAR at construction.
- Type
- int
- attribute
sort¶ Passed to API without local validation.
- Type
- optional values
- attribute
csr_override¶ Passed to API without local validation.
- Type
- optional values
Methods and supported operations
- coroutine
dict()¶ Serialize inputs; may resolve location with I/O.
- Returns
- dict
Usage:
await request.dict()
- coroutine
search()¶ POST search once; subsequent calls return stored response.
- Returns
- PlanSearchResponse
Usage:
await request.search()
- operation
await request¶ Equivalent to await request.search().
- Returns
- PlanSearchResponse
- coroutine
get_slcsp()¶ Fetch SLCSP for this place, household and year. Does not populate the search response.
- Returns
- decoded JSON
Usage:
await request.get_slcsp()
PlanSearchResponse
- class
cms_marketplace.PlanSearchResponse(data, request)¶ Normally returned by await request. Wraps search results and pagination state.
- Parameters
data(dict) — Raw search response containing plans and optional total. Required.request(PlanSearchRequest) — Originating request, retained for pagination. Required.
response = await cms.PlanSearchRequest(place, household, year=2026) async for plan in response: print(plan.name, plan.premium)
Attributes and properties
- attribute
request¶ Originating request.
- attribute
total¶ API total, falling back to initial loaded count.
- Type
- int
- attribute
loaded_all¶ Starts False; advisory and not always set at termination.
- Type
- bool
Methods and supported operations
- operation
len(response)¶ Reported total, not the loaded count.
- Returns
- int
Plan
- class
cms_marketplace.Plan(data)¶ One plan response. Usually returned by get_plan_by_id() or a search; constructing from data makes no request.
- Parameters
data(dict) — Raw Plan mapping; see attributes below for parsed fields. Required.
plan = await cms.get_plan_by_id("EXAMPLE_PLAN_ID", year=2026) print(plan.name, plan.issuer.name) combined, medical, drug = plan.individual_deductibles
Attributes and properties
- attribute
data¶ Original API mapping. Raw values are not type-validated.
- Type
- dict
- attribute
id¶ Aliases for the plan ID at construction.
- Type
- str | None
- attribute
plan_id¶ Aliases for the plan ID at construction.
- Type
- str | None
- attribute
name¶ Response values; missing fields default to None.
- Type
- str | None
- attribute
type¶ Response values; missing fields default to None.
- Type
- str | None
- attribute
metal_level¶ Response values; missing fields default to None.
- Type
- str | None
- attribute
state¶ Response values; missing fields default to None.
- Type
- str | None
- attribute
benefits_url¶ Response values; missing fields default to None.
- Type
- str | None
Raw premium from the response.
- Type
- number | None
- attribute
issuer_name¶ Copy of issuer.name at construction.
- Type
- str | None
- property
primary_copay¶ Parsed copay for matching benefit entries; not limited to one network tier.
- Type
- number | None
- property
specialist_copay¶ Parsed copay for matching benefit entries; not limited to one network tier.
- Type
- number | None
- property
generic_drugs_copay¶ Parsed copay for matching benefit entries; not limited to one network tier.
- Type
- number | None
- property
primary_display¶ Display text derived by the cost parser.
- Type
- str
- property
specialist_display¶ Display text derived by the cost parser.
- Type
- str
- property
generic_drugs_display¶ Display text derived by the cost parser.
- Type
- str
- property
individual_deductibles¶ (combined, medical, drug); missing components are None.
- Type
- tuple
- property
family_deductibles¶ (combined, medical, drug); missing components are None.
- Type
- tuple
- property
individual_max_out_of_pocket¶ Matching medical-and-drug maximum; None when absent.
- Type
- number | None
- property
family_max_out_of_pocket¶ Matching medical-and-drug maximum; None when absent.
- Type
- number | None
- property
has_individual_deductible¶ Whether any parsed deductible component is nonzero and non-None.
- Type
- bool
- property
has_family_deductible¶ Whether any parsed deductible component is nonzero and non-None.
- Type
- bool
Methods and supported operations
- method
spending_estimate(primary_visits=0, specialist_visits=0, generic_drugs=0, emergency_room_visits=0)¶ Synchronous estimate using counts, fixed base costs and individual deductible/MOOP logic. Excludes premiums; see calculation limitations below.
- Parameters
primary_visits(int) — Number of primary-care visits. Default: 0.specialist_visits(int) — Number of specialist visits. Default: 0.generic_drugs(int) — Number of generic-drug uses. Default: 0.emergency_room_visits(int) — Number of emergency-room visits. Default: 0.
- Returns
- number
Issuer
- class
cms_marketplace.Issuer(data)¶ Insurer information nested at plan.issuer. No public methods beyond construction.
- Parameters
data(dict) — Raw Issuer mapping; see attributes below for parsed fields. Required.
issuer = plan.issuer print(issuer.name, issuer.phone)
Attributes and properties
- attribute
data¶ Original issuer mapping; None is not accepted.
- Type
- dict
- attribute
id¶ Raw values; each defaults to None when missing.
- Type
- str | None
- attribute
name¶ Raw values; each defaults to None when missing.
- Type
- str | None
- attribute
phone¶ Raw values; each defaults to None when missing.
- Type
- str | None
- attribute
individual_url¶ Raw values; each defaults to None when missing.
- Type
- str | None
Benefit
- class
cms_marketplace.Benefit(data)¶ One entry in plan.benefits. No public methods beyond construction.
- Parameters
data(dict) — Raw Benefit mapping; see attributes below for parsed fields. Required.
for benefit in plan.benefits: print(benefit.name, benefit.covered)
Attributes and properties
- attribute
data¶ Original benefit mapping.
- Type
- dict
- attribute
name¶ Benefit label and type.
- Type
- str | None
- attribute
type¶ Benefit label and type.
- Type
- str | None
- attribute
covered¶ Response flags; default False.
- Type
- bool
- attribute
has_limits¶ Response flags; default False.
- Type
- bool
- attribute
limit_units¶ Limit unit and quantity; default None.
- Type
- response value | None
- attribute
limit_quantity¶ Limit unit and quantity; default None.
- Type
- response value | None
- attribute
cost_sharings¶ All network-tier entries; defaults to an empty list.
- Type
- list[CostSharing]
CostSharing
- class
cms_marketplace.CostSharing(data)¶ One entry in benefit.cost_sharings. Flags are computed once during construction.
- Parameters
data(dict) — Raw CostSharing mapping; see attributes below for parsed fields. Required.
for benefit in plan.benefits: for share in benefit.cost_sharings: if share.network_tier == "In-Network" and share.is_valid: print(benefit.name, share.display_string)
Attributes and properties
- attribute
data¶ Original cost-sharing mapping.
- Type
- dict
- attribute
network_tier¶ For example In-Network or In-Network Tier 2.
- Type
- str | None
- attribute
copay_amount¶ Raw response amounts; default None.
- Type
- number | None
- attribute
coinsurance_rate¶ Raw response amounts; default None.
- Type
- number | None
- attribute
copay_options¶ Default Not Applicable.
- Type
- str
- attribute
coinsurance_options¶ Default Not Applicable.
- Type
- str
- attribute
display_string¶ Display text; default empty string.
- Type
- str
- attribute
csr¶ Raw CSR field.
- Type
- response value | None
- attribute
benefit_before_deductible¶ True only for the literal BBD.
- Type
- bool
- attribute
is_copay¶ Copay amount exists and display is neither Not Applicable nor Not Covered.
- Type
- bool
- attribute
is_coinsurance¶ Rate exists, is neither 0 nor 1, and display is applicable/covered.
- Type
- bool
- attribute
is_valid¶ Display is applicable/covered and copay exists or rate is neither None nor 1.
- Type
- bool
Methods and supported operations
- method
get_relevant_cost_sharing()¶ Currently references an absent cost_sharings attribute. Select entries from benefit.cost_sharings yourself.
- Returns
- raises AttributeError on ordinary instances
- Raises
AttributeError— Ordinary instances have no cost_sharings attribute.
PlanCache
- class
cms_marketplace.PlanCache¶ Shared class-level SQLite cache. Use PlanCache directly; instances do not isolate configuration.
cms.PlanCache.ENABLED = True plan = await cms.PlanCache.get_plan_from_id("EXAMPLE_PLAN_ID")
Attributes and properties
- class attribute
ENABLED¶ Default False.
- Type
- bool
- class attribute
CACHE_FILE¶ Default _CMS_MARKETPLACE_CACHE.sqlite3.
- Type
- str
- class attribute
COMPRESSION_METHOD¶ Method 0 = zlib, 1 = lzma; level defaults to 6.
- Type
- int
- class attribute
COMPRESSION_LEVEL¶ Method 0 = zlib, 1 = lzma; level defaults to 6.
- Type
- int
- class attribute
UPDATE_EVERY¶ Default 86400. See cache section for freshness limitations.
- Type
- seconds
- awaitable property
db¶ Awaitable class property, without parentheses. Opens shared database and initializes schema.
- Type
- Database
Methods and supported operations
- coroutine · class-level
get_plan_from_id(plan_id)¶ Cached result, or None when disabled, absent or recognized as expired.
- Parameters
plan_id(str) — Plan identifier. Required.
- Returns
- Plan | None
Usage:
await PlanCache.get_plan_from_id(plan_id)
- coroutine · class-level
get_plan_id_from_name(plan_name, fuzzy=False)¶ Cached ID only; optional fuzzy lookup.
- Parameters
plan_name(str) — Name to look up in the cache. Required.fuzzy(bool) — Allow a fuzzy name match. Default: False.
- Returns
- str | None
Usage:
await PlanCache.get_plan_id_from_name(plan_name, fuzzy=False)
- coroutine · class-level
add_plan_to_cache(plan)¶ Store Plan.data and commit; may skip disabled/recent entries.
- Parameters
plan(Plan) — Plan whose raw data is cached. Required.
- Returns
- None
Usage:
await PlanCache.add_plan_to_cache(plan)
- method · class-level
compress_json(data: dict)¶ Compress a dictionary using shared settings.
- Parameters
data(dict) — Raw PlanCache mapping; see attributes below for parsed fields. Required.
- Returns
- bytes
- method · class-level
decompress_json(blob: bytes)¶ Decode compressed JSON with matching settings.
- Parameters
blob(bytes) — JSON compressed with the configured cache method. Required.
- Returns
- dict
PovertyGuidelines
- class
cms_marketplace.PovertyGuidelines(data)¶ Wrapper around supplied guideline data. Method name calculate_guidline preserves the source spelling.
- Parameters
data(dict) — Raw PovertyGuidelines mapping; see attributes below for parsed fields. Required.
guidelines = cms.PovertyGuidelines({ "guidelines": [{"household_size": 1, "guideline": 100}], "add_per_person_after_eight": 20, }) # Illustrative data only. print(guidelines[1])
Attributes and properties
- attribute
data¶ Original mapping.
- Type
- dict
- attribute
guidelines¶ Household size → guideline amount.
- Type
- dict
- attribute
per_person_after_eight¶ Additional amount per person above eight; default 0.
- Type
- number
Methods and supported operations
- method
calculate_guidline(household_size)¶ Stored amount or extrapolation above eight. Unsupported smaller sizes raise ValueError.
- Parameters
household_size(int) — Number of people used to select a guideline amount. Required.
- Returns
- number
- Raises
ValueError / KeyError— Unsupported size at or below eight, or missing size-eight data for extrapolation.
- operation
guidelines[household_size]¶ Equivalent to calculate_guidline(size) on a guideline object.
- Returns
- number
- coroutine · class-level
get_guidelines(year=None)¶ Load year-specific data; see calculation section for existing year/state limitations.
- Parameters
year(int | None) — Coverage year; None selects DEFAULT_YEAR. Default: None.
- Returns
- PovertyGuidelines
Usage:
await PovertyGuidelines.get_guidelines(year=None)
- coroutine · class-level
calculate_guideline_for_year(household_size, year=None)¶ Load guidelines then calculate for the household size.
- Parameters
household_size(int) — Number of people used to select a guideline amount. Required.year(int | None) — Coverage year; None selects DEFAULT_YEAR. Default: None.
- Returns
- number
Usage:
await PovertyGuidelines.calculate_guideline_for_year(household_size, year=None)
Configuration and imports
This guide describes the checked-in Python implementation, including its incomplete paths. Its calculations and datasets are code behavior; their constants do not establish current eligibility rules, premiums, or legal requirements.
Set the module-level API_KEY before making API calls. The module uses toolbox.AiohttpResponse and aiosqliteObj; install their aiohttp, requests, and aiosqlite dependencies and provision pmblue_update. At import time it calls pmblue_update.maintenance("toolbox", "aiosqliteObj"), imports those dependencies, and calls self_update(primary_mod=True). Importing can therefore perform network requests, create/update local module and cache files, or prompt to publish a missing/newer local module. See updater configuration.
DEFAULT_YEAR is set once during import to the current calendar year, advancing to the next year in November and December. Pass year explicitly when results must be tied to a particular year.
import asyncio
import os
import cms_marketplace as cms
cms.API_KEY = os.environ["CMS_MARKETPLACE_API_KEY"]
async def main():
# Explicit place data avoids an additional ZIP-to-county lookup.
place = cms.Place("33101", state="FL", countyfips="12086")
household = cms.Household(income=42000)
household.add_person(cms.Person("1990-01-15"))
request = cms.PlanSearchRequest(place, household, year=2026)
response = await request
# Synchronous iteration covers the already-loaded first page.
for plan in response:
print(plan.id, plan.name, plan.premium)
asyncio.run(main())People and households
Person(date_of_birth, is_pregnant=False, is_parent=False, uses_tobacco=False, aptc_eligible=True, gender=None, relationship=None, has_mec=None) accepts a date/datetime, a date string (YYYY-MM-DD, MM/DD/YYYY, or MM-DD-YYYY), or an integer age. dict() serializes a date as dob and an integer as age, including optional fields only when supplied.
set_gender() normalizes Male/Female and their lowercase or one-letter forms; reading gender while unset raises ValueError. The constructor itself stores a supplied gender without normalization. age computes age from a date, but fails for integer-age inputs because it expects .year, .month, and .day. Use person.dict()["age"] for integer input. Pass actual dates or small age integers; the apparent birth-year conversion branch restores the original integer and is not reliable.
Household(income=0, has_married_couple=False) stores annual income and a list of people. Numeric income strings may contain commas. add_person(person) requires Person and assigns Self to the first person; when the married flag is set it assigns Spouse to the second person if their relationship is unset. create_and_add_person(...) accepts the same person fields and returns None. dict() returns the request object and len(household) counts people.
Places and county lookup
Place(zipcode, state=None, countyfips=None, countyname=None) describes a location. await place.full_dict() returns zipcode, state, and countyfips; it looks up missing state/FIPS values on demand. Keep ZIP and FIPS values as strings to preserve leading zeroes.
| Place setting | Current values and effect |
|---|---|
FIPS_RETRIEVAL_METHOD | 0 (default): HUD-derived CSV from /resources/zip_county, labeled June 2025 in source. 1: CMS county-ZIP dataset. 2: CMS ZIP lookup endpoint. 3: combined datasets. |
MULTIPLE_RESULT_BEHAVIOR | 0: select a result (HUD-based paths use weighting; other paths may use the first). 1: raise on ambiguity. 2: return all matches. |
DATA_FILE_NAME | Defaults to countyfips; dataset methods write countyfips.csv and/or countyfips.json in the working directory and cache data in memory. |
cms.Place.FIPS_RETRIEVAL_METHOD = 2
cms.Place.MULTIPLE_RESULT_BEHAVIOR = 2
matches = await cms.Place.retrieve_county_fips("33101")
if matches is None or matches == []:
raise ValueError("No county found")
if isinstance(matches, list):
# Present matches to your user; use their selected Place.
print([(p.countyfips, p.countyname) for p in matches])
else:
print(matches.countyfips, matches.countyname)
Choose an ambiguous result before building the request: full_dict() silently selects the first element of a returned list. Empty/no matches can produce IndexError or AttributeError. County-name lookup is unavailable for method 0 (NotImplementedError). Dataset files have no automatic freshness refresh.
Plan search and pagination
PlanSearchRequest(place, household, market="Individual", year=None, filter=None, sort=None, csr_override=None) builds the request. await request.dict() exposes the serialized request; await request.search() and await request return a PlanSearchResponse. The first response is cached on the request object, so create a new request when inputs change.
issuer_filter = cms.Filter()
issuer_filter.add_issuer("Example Issuer")
request = cms.PlanSearchRequest(
place, household, year=2026, filter=issuer_filter
)
response = await request
async for plan in response:
print(plan.name, plan.metal_level)
# Or collect all pages into a list (can make multiple requests):
all_plans = await response
| Expression | Behavior |
|---|---|
for plan in response | Iterates only the currently loaded response.data list. |
async for plan in response | Reads stored objects and fetches additional pages with offsets increasing by 10. Attempts cache writes when caching is enabled. |
await response | Collects asynchronous iteration into a list of Plan. |
len(response), response.total | Reported total, which can exceed the loaded list length. |
Filter | add_issuer(name) appends an issuer name; dict() emits the issuer list when nonempty. |
Await a new request before iterating it asynchronously: direct async for plan in request on an unfetched request incorrectly awaits a synchronous __aiter__() result and can raise TypeError. The paginator assumes pages of 10, can make an extra request at the end, and does not reset its offset when iteration restarts; use a fresh request for a fresh traversal. loaded_all is advisory and is not always set at termination.
Plans, benefits, and costs
await get_plan_by_id(plan_id, year=None) (alias get_plan_from_id) checks the optional local cache, then requests a plan and returns Plan. A plan exposes id/plan_id, name, type, premium, metal_level, state, benefits_url, raw data, an Issuer, and a list of Benefit objects. Issuer provides id, name, phone, and individual_url.
Benefit exposes its name, type, covered, has_limits, limit_units, limit_quantity, raw data, and cost_sharings. Each CostSharing contains network tier, copay/coinsurance amounts and options, display_string, CSR data, benefit_before_deductible, and the derived booleans is_copay, is_coinsurance, and is_valid.
plan = await cms.get_plan_by_id("EXAMPLE_PLAN_ID", year=2026)
for benefit in plan.benefits:
for share in benefit.cost_sharings:
if share.network_tier == "In-Network" and share.is_valid:
print(benefit.name, share.display_string)
primary_copay, specialist_copay, and generic_drugs_copay select the highest parsed copay across matching benefit entries. They do not limit themselves to one network tier. Display properties are primary_display, specialist_display, and generic_drugs_display.
individual_deductibles and family_deductibles return three-tuples (combined, medical, drug), with missing values represented by None. Boolean has_individual_deductible/has_family_deductible and individual/family maximum-out-of-pocket properties are also available. Parsed values follow this implementation's matching rules; retain plan.data to inspect the full response.
CostSharing.get_relevant_cost_sharing() is currently placed on a class that does not have cost_sharings and raises AttributeError for an ordinary instance. Iterate benefit.cost_sharings explicitly, as above. get_plan_from_text() also has inconsistent results: a 14-character string returns an unawaited coroutine, while a name returns only a cached plan ID or None. Prefer get_plan_by_id() when you have an ID.
Optional SQLite plan cache
PlanCache.ENABLED defaults to False. Enable and configure it before the first cache access. Cache operations use aiosqliteObj and an on-demand plans table containing plan ID, name, update timestamp, and compressed JSON bytes.
cms.PlanCache.CACHE_FILE = "marketplace-plans.sqlite3"
cms.PlanCache.COMPRESSION_METHOD = 0 # zlib; 1 selects lzma
cms.PlanCache.COMPRESSION_LEVEL = 6
cms.PlanCache.UPDATE_EVERY = 86400
cms.PlanCache.ENABLED = True
cache_db = await cms.PlanCache.db # Initializes schema and name crosswalk.
plan = await cms.get_plan_by_id("EXAMPLE_PLAN_ID", year=2026)
cached = await cms.PlanCache.get_plan_from_id(plan.id)
# At application shutdown, after all cache users are finished:
await cache_db.close()
UPDATE_EVERY accepts seconds or a datetime.timedelta. compress_json() and decompress_json() use the globally selected algorithm; choosing LZMA without the lzma module raises ImportError. Use a separate cache file when changing compression because existing rows do not record their compression algorithm.
get_plan_id_from_name(name, fuzzy=False) looks up uppercase names in the in-memory crosswalk, optionally using toolbox.Matching.fuzzy_within(..., threshold=.85). The crosswalk is populated when the database opens and is not updated by new inserts. Name lookup does not itself initialize the database.
The cache is keyed only by plan ID, without year or household context. Reusing a file across years/households may reuse a plan or premium from a different request. Expiration is checked against the in-memory timestamps before the first database initialization, so preexisting stale rows may be returned on the first lookup. Expired rows are treated as misses after loading; no active deletion occurs. A write within one tenth of UPDATE_EVERY of the previous write is skipped. These behaviors mean the cache should not be treated as an authoritative plan store.
Eligibility and calculation helpers
await get_household_eligibility(place, household, year=None) posts to the eligibility estimates endpoint and returns decoded JSON. await get_slcsp(...) posts the household and place with market="Individual" and returns decoded JSON. await request.get_slcsp() uses the request's place, household, and year.
PovertyGuidelines wraps a returned guideline table. Call await PovertyGuidelines.get_guidelines(year) or await PovertyGuidelines.calculate_guideline_for_year(household_size, year). Instances support guidelines[household_size] and the intentionally misspelled public method calculate_guidline(household_size).
_year value, so requesting an earlier available property can fetch DEFAULT_YEAR instead. Requests also hardcode states/FL. Missing year attributes can raise AttributeError, and an unset API key raises ValueError.interpolate_applicable_percentage(fpl_ratio) uses the static APPLICABLE_PERCENTAGE_BRACKETS table below. These are the literal values in version 0.4.1, not verified rates for any current coverage year. Interpolation is linear within each listed interval; values outside all intervals return None.
| FPL ratio interval | Lower to upper percentage in source |
|---|---|
| 0–1.33 | 2.10%–2.10% |
| 1.33–1.50 | 3.14%–4.19% |
| 1.50–2.00 | 4.19%–6.60% |
| 2.00–2.50 | 6.60%–8.44% |
| 2.50–3.00 | 8.44%–9.96% |
| 3.00–4.00 | 9.96%–9.96% |
await calculate_aptc(place, household, year=None) divides household income by the retrieved guideline, interpolates that table, and subtracts the calculated annual contribution from 12 times the returned SLCSP premium, floored at zero. The result includes annual_aptc, monthly_aptc, and calculation inputs. A ratio outside the table returns zero with the code's fixed reason string Income above 400% FPL. Passing a different year changes API requests, not the static percentage table.
plan.spending_estimate(primary_visits=0, specialist_visits=0, generic_drugs=0, emergency_room_visits=0) returns a rough individual cost total, excluding premiums. It uses fixed assumed uninsured costs of 110, 180, 25, and 1500 respectively, iterates service counts against deductible logic, and caps the result at the parsed individual maximum-out-of-pocket value when present. Counts should be nonnegative integers. Its separate medical/drug deductible branch assigns some spending to the wrong accumulator; its result is an implementation estimate, not a benefits determination. Family deductible fields are not used by this method.
Errors and operational limits
- HTTP results are generally decoded directly without checking status, so authentication, schema, network, and JSON failures can surface as
KeyError,AttributeError, transport exceptions, or incomplete result objects. The module has no common API-error type. - Invalid person dates/gender or lookup settings raise
ValueError; non-Personhousehold entries raiseTypeError. Date-based age calculation can also fail for a February 29 birthday during a non-leap year. - Several model constructors assume complete response dictionaries. For example,
PlanconstructsIssuereven if issuer data is absent, which can raiseAttributeError. - File paths are relative to the process working directory unless configured otherwise; downloads, compression, and SQLite caching need writable storage. Synchronous file reads/writes and compression occur inside otherwise asynchronous flows.
- The guide describes the current module and explicitly identifies incomplete helpers. Examples do not contact live services unless executed with valid credentials; verify returned data and the desired year in your application.
Object handbook
Request-side objects (Person, Household, Place, and Filter) are assembled by the application. A PlanSearchRequest turns them into API JSON and returns a PlanSearchResponse, which creates Plan objects. Each plan owns an Issuer and a list of Benefit objects; benefits own CostSharing objects. Except for the optional cache, these are in-memory wrappers around request or response data.
Person and Household
A Person stores date_of_birth, is_pregnant, is_parent, uses_tobacco, aptc_eligible, relationship, and has_mec. These fields are included by dict() according to the rules in the household guide above. It has no link to a particular household and makes no API requests. set_gender(value) changes normalized gender locally and returns None. age and gender are synchronous properties with the documented missing/integer/leap-date limitations.
Household(income=0, has_married_couple=False) owns mutable income, has_married_couple, and people. add_person(person) appends the supplied object rather than copying it and may fill in its relationship. create_and_add_person(...) constructs and appends a person but returns no object; use household.people[-1] afterward if needed. dict() builds a fresh request dictionary from the current people. Changing a person therefore affects subsequent household serialization, but it does not refresh a search request whose response was already fetched.
Place and Filter
Place(zipcode, state=None, countyfips=None, countyname=None) stores the supplied attributes. await full_dict() may do network or dataset I/O when state/FIPS is missing. Resolved state and FIPS are used in the returned dictionary but are not written back to the corresponding instance attributes; county name may be filled on the instance. Pass known location data at construction when you want to avoid repeated resolution logic.
Place.retrieve_county_fips(zipcode) and retrieve_county_name(countyfips) are class-level asynchronous functions, selected by the shared class settings. A FIPS lookup's result may be a Place, list, or no result depending on the selected backend and ambiguity setting. Do not assume a consistent list shape without normalizing it yourself. Filter() initializes an empty issuers list; add_issuer(name) appends without deduplication and dict() returns either an issuer filter or an empty dictionary.
PlanSearchRequest
The constructor retains place, household, market, year, filter, sort, and csr_override. When year=None, it captures DEFAULT_YEAR at construction. await dict() serializes current arguments and may resolve the place. The optional sort and csr_override values pass through without local validation.
await search() performs the initial POST only if the request has no stored response; it then returns its PlanSearchResponse. await request is the same operation. await get_slcsp() calls the module-level SLCSP helper with this request's place, household, and year, returning decoded JSON without storing it as the search response. A new request object is the straightforward way to run a fresh query after changing inputs.
PlanSearchResponse
PlanSearchResponse(data, request) parses data.get("plans", []) into Plan instances, sets request to the originating request, and sets total to the returned total or the initial loaded count. data on this object is a list of plans, unlike the raw mapping stored as plan.data. loaded_all begins false and is advisory.
Synchronous iteration returns the currently loaded list without I/O; asynchronous iteration may POST further pages and append to that same list. len(response) uses the reported total. await response collects asynchronous iteration into a list. Reusing the same response concurrently shares mutable iteration state, and restarting iteration does not reset its pagination offset. Create a new request for independent traversals.
Plan, Issuer, Benefit, and CostSharing
Plan(data) expects one plan mapping. It parses identifying/display fields immediately, creates Issuer(data.get("issuer")), and creates one Benefit for each benefits entry. It retains the original mapping as data. Construction does not fetch more data. Copay/deductible/out-of-pocket properties are computed from that mapping and many are cached; mutating raw data after first access may leave derived values stale. Reconstruct a plan from replacement data when you need a consistent new snapshot.
Issuer(data) stores id, name, phone, individual_url, and raw data. It has no API methods. plan.issuer_name copies plan.issuer.name at construction and will not automatically follow later manual changes to the issuer.
Benefit(data) stores name, type, covered (default false), has_limits (default false), limit_units, limit_quantity, and raw data. It builds cost_sharings from the response list and does not select a preferred tier for the caller. Coverage and limits remain separate from the derived validity of any cost-sharing entry.
CostSharing(data) stores network_tier, copay_amount, copay_options, coinsurance_rate, coinsurance_options, display_string, csr, and raw data. The constructor maps only the literal BBD to true for benefit_before_deductible; all other values become false. It computes is_coinsurance, is_copay, and is_valid once, excluding display values Not Applicable/Not Covered and certain absent/full-rate amounts. These flags are this parser's heuristics, not a replacement for the complete benefit response. The misplaced get_relevant_cost_sharing() method remains unusable on ordinary instances.
Plan.spending_estimate(...) is synchronous, returns a number, and uses the fixed cost/deductible algorithm documented above. It does not make a request, include premiums, or incorporate the household's family utilization. Individual/family deductible properties each return three-tuples; absent values are None, not guaranteed zeroes.
PlanCache
PlanCache is configured and called at class level; separate instances do not provide independent files or settings. await PlanCache.db creates/opens the shared aiosqliteObj.Database, initializes its schema/indexes, and loads timestamp/name crosswalks. This property is awaitable and has no parentheses. Its database object can be closed at application shutdown after all cache work has stopped.
await get_plan_from_id(plan_id) returns a cached Plan or None. await get_plan_id_from_name(plan_name, fuzzy=False) returns only an ID or None; it does not fetch a plan. await add_plan_to_cache(plan) inserts/updates raw plan data and commits, or returns immediately when caching is disabled/recently written. compress_json(data) and decompress_json(blob) are synchronous byte conversion helpers using the selected global compression method. Cache keys and freshness limitations remain as described above.
PovertyGuidelines and top-level functions
PovertyGuidelines(data) stores raw data, per_person_after_eight, and a guidelines dictionary keyed by household size. calculate_guidline(size) and object[size] return a directly stored amount or, for sizes above eight, the size-eight value plus the extra-person amount. Unsupported smaller sizes raise ValueError; a missing size-eight row can raise KeyError. Constructing an object from supplied data requires no request.
Class-level await get_guidelines(year=None) returns a guideline object and await calculate_guideline_for_year(size, year=None) returns its calculated number, subject to the existing year/state bug. Top-level eligibility/SLCSP helpers return remote JSON; get_plan_by_id()/get_plan_from_id() return Plan; get_plan_from_text() currently has inconsistent return types. interpolate_applicable_percentage(ratio) is a synchronous numeric helper; calculate_aptc() combines remote JSON with the static table and returns a calculation dictionary. None of these helpers persists household data.
Worked examples
Construct and inspect a household request before fetching
import datetime
import cms_marketplace as cms
household = cms.Household(income="42,000", has_married_couple=True)
first = cms.Person(
datetime.date(1990, 1, 15),
uses_tobacco=False,
aptc_eligible=True,
has_mec=False,
)
first.set_gender("f")
household.add_person(first)
household.create_and_add_person("1992-06-20", has_mec=False)
assert first.relationship == "Self"
assert household.people[1].relationship == "Spouse"
payload = household.dict()
print(payload["income"], len(payload["people"]))
# In an async function, inspect the complete request before sending:
# place = cms.Place("33101", state="FL", countyfips="12086")
# request = cms.PlanSearchRequest(place, household, year=2026)
# request_payload = await request.dict()
The constructor methods make local objects. The person defaults and relationship assignments are explicit here; choose flags from the application's actual inputs rather than inferring them from this example. Serialization with an explicit place avoids a county lookup.
Choose a display entry explicitly from each benefit
def preferred_cost_sharing(benefit):
for tier in ("In-Network", "In-Network Tier 2"):
for sharing in benefit.cost_sharings:
if sharing.network_tier == tier and sharing.is_valid:
return sharing
return None
def benefit_rows(plan):
rows = []
for benefit in plan.benefits:
sharing = preferred_cost_sharing(benefit)
rows.append({
"name": benefit.name,
"covered": benefit.covered,
"has_limits": benefit.has_limits,
"limit_quantity": benefit.limit_quantity,
"limit_units": benefit.limit_units,
"network_tier": sharing.network_tier if sharing else None,
"display": sharing.display_string if sharing else None,
})
return rows
This is application selection logic over the parsed object tree. It makes the tier preference visible and handles an absent usable entry. It deliberately preserves coverage and limit fields alongside display text; a missing selected entry does not establish that the benefit is uncovered.
Produce a plain-data plan report from a fresh search
async def plan_report(place, household, year):
request = cms.PlanSearchRequest(place, household, year=year)
response = await request
report = []
async for plan in response:
report.append({
"requested_year": year,
"id": plan.id,
"name": plan.name,
"issuer": plan.issuer.name,
"metal_level": plan.metal_level,
"premium": plan.premium,
"deductibles": plan.individual_deductibles,
"maximum_out_of_pocket": plan.individual_max_out_of_pocket,
"benefits": benefit_rows(plan),
})
return report
This consumes the response asynchronously, so it can make several requests. Each returned record retains the requested year as application context; the module's plan cache itself does not use year or household in its key. Keep caching disabled for requests that must not share cached household/year-specific values.
Resolve a cached name to a plan in two explicit steps
async def cached_plan_by_name(name, year):
if not cms.PlanCache.ENABLED:
raise RuntimeError("Enable and configure the plan cache first")
await cms.PlanCache.db # Opens the DB and loads its name crosswalk.
plan_id = await cms.PlanCache.get_plan_id_from_name(name, fuzzy=False)
if plan_id is None:
return None
return await cms.get_plan_by_id(plan_id, year=year)
This avoids get_plan_from_text()'s coroutine/ID ambiguity. It searches only names loaded into the cache's current crosswalk, not the live Marketplace API. New inserts do not refresh that crosswalk, and the cache's year/freshness caveats still apply. Close the shared cache database once at application shutdown, not after every lookup.