Insurance Coverage

Use the b.well Kotlin SDK to read a user's insurance plans — insurer, plan name, group and member numbers.

New to the SDK? See Initialize and Authenticate before making your first call.

What can you do?

📘 Related Documents

  • Working with claims for a specific plan? See Insurance ClaimsgetClaimsForPlan there takes the id you get back here.

Get all coverage plans for the current user

Method Signature :

suspend fun getCoverages(request: CoverageRequest?): BWellResult<Coverage>

Example Code :

val coverageRequest = CoverageRequest.Builder()
    .page(0)
    .pageSize(20)
    .build()

viewLifecycleOwner.lifecycleScope.launch {
    when (val result = BWellSdk.financials.getCoverages(coverageRequest)) {
        is BWellResult.ResourceCollection -> {
            val coverages = result.data ?: emptyList()
            println("Found ${coverages.size} coverage(s)")
        }
        else -> {
            println("getCoverages failed: ${result.error}")
        }
    }
}

📘 Notes

  • getCoverages returns a sealed BWellResult<Coverage> — pattern-match with when. There is no .data() method call.
  • CoverageRequest is optional — pass null to fetch every available Coverage with default paging.
  • When the user has no coverage on file, result.data is null or empty. This is a valid state, not an error — see Troubleshooting.

Read insurer, plan name, group #, and member # from a plan

None of these live on a single top-level field — insurer is a reference display, and Plan Name / Group # both live in the same class list, distinguished only by a type code.

val coverage: Coverage = /* one entry from the ResourceCollection above */

val insurer = coverage.payor?.firstOrNull()?.display

val planName = coverage.`class`
    ?.firstOrNull { it.type?.coding?.any { c -> c.code == "plan" } == true }?.name

val groupNumber = coverage.`class`
    ?.firstOrNull { it.type?.coding?.any { c -> c.code == "group" } == true }?.value

// Member # prefers the MB-typed identifier; subscriberId is the documented fallback
val memberNumber = coverage.identifier
    ?.firstOrNull { it.type?.coding?.any { c -> c.code == "MB" } == true }?.value
    ?: coverage.subscriberId

📘 Notes

  • class is a Kotlin reserved word — access it with backticks: coverage.`class`.
  • Filter class entries by type.coding[].code ("plan" vs "group"), never by position in the list — a payor can omit either entry.
  • Always use firstOrNull { ... }, never .first(). Calling .first() throws when no entry matches, which happens whenever a payor hasn't populated that class.

Handle users with more than one active plan

Coverage results are always a list — a user can hold multiple plans simultaneously. Iterate; don't assume a single result.

val activePlans = coverages.filter { coverage ->
    coverage.status == FinancialResourceStatusCodes.ACTIVE &&
        (coverage.period?.end == null || coverage.period.end > today)
}

📘 Notes

  • status == ACTIVE alone is not sufficient — combine it with period.end (null or in the future) to decide what counts as "active" for display purposes.

Paginate results

val coverageRequest = CoverageRequest.Builder()
    .page(1)       // zero-based — page 1 is the second page
    .pageSize(20)
    .build()

📘 Notes

  • page is zero-based.
  • pageSize defaults to the SDK's standard page size if omitted.

Reference


Result type cheat sheet

MethodReturnsHow to read it
BWellSdk.financials.getCoverages(...)BWellResult<Coverage>Pattern-match with when or as?. is BWellResult.ResourceCollection.data: List<Coverage>?. Any other branch → .error: OperationOutcome.

CoverageRequest fields

Builder methodRequired?Notes
.patient(patientId)OptionalScope to a single patient. Omit to fetch all coverages the authenticated user can see.
.page(n)OptionalZero-indexed page number. Defaults to 0.
.pageSize(n)OptionalResults per page. Defaults to the SDK's standard page size.

Coverage response shape

FieldTypeRequiredNotes
idStringRequiredUnique identifier
resourceTypeStringRequiredAlways "Coverage"
statusFinancialResourceStatusCodesRequiredACTIVE, CANCELLED, DRAFT, ENTERED_IN_ERROR
identifierList<Identifier>OptionalMember # is the entry where type.coding[].code == "MB"
typeCodeableConceptOptionalPlan type; read type.text, fall back to type.coding[0].display
subscriberIdStringOptionalFallback Member # when the MB identifier is absent
periodPeriodOptionalstart / end — Effective Start/End Date
payorList<CoveragePayorReference>OptionalInsurer name is payor[0].display
classList<CoverageClass>OptionalCarries Plan Name ("plan") and Group # ("group")

CoveragePayorReference fields

FieldTypeRequiredNotes
referenceStringOptionalReference to the payor resource
typeStringOptionalExpected type of the reference target
displayStringOptionalPlain-text Insurer display name — this is the field read as payor[0].display

CoverageClass fields

FieldTypeRequiredNotes
idStringOptionalUnique identifier for this class entry
typeCodeableConceptOptionalFilter by type.coding[].code: "plan"name is the Plan Name; "group"value is the Group #
valueStringOptionalAlphanumeric insurer-issued label (populated for the "group" entry)
nameStringOptionalShort description for the class (populated for the "plan" entry)

Annotated shape:

{
  "id": "coverage-8841",
  "resourceType": "Coverage",
  "status": "active",
  "type": { "text": "Medicare Advantage PPO" },
  "subscriberId": "SUB-99213456",
  "identifier": [
    { "type": { "coding": [{ "code": "MB" }] }, "value": "MB-2201938" }
  ],
  "period": { "start": "2026-01-01", "end": "2026-12-31" },
  "payor": [ { "display": "Acme Health Plans" } ],
  "class": [
    { "type": { "coding": [{ "code": "plan" }] }, "name": "PPO Gold" },
    { "type": { "coding": [{ "code": "group" }] }, "value": "GRP-4471" }
  ]
}


Troubleshooting


Each heading below is the symptom or error you may encounter. Search this section for the closest match to what you're hitting.


Issue : Does not compile: data is not a member of BWellResult<Coverage> You accessed .data directly on the BWellResult value instead of narrowing to BWellResult.ResourceCollection first.
Fix : (result as? BWellResult.ResourceCollection)?.data ?: emptyList(), or pattern-match with when.

Issue : getCoverages returns an error result where it used to succeed The session is missing or expired — getCoverages doesn't throw on an auth failure, it returns the non-ResourceCollection branch.
Fix : Check result.error for an OperationOutcome before treating this as a data problem. Re-run SDK initialization/authentication and retry.

Issue : data is null or an empty list Either the authenticated user has no Coverage records, or the current page/pageSize window has no results.
Fix Treat this as a valid "no coverages" state, not an error. Don't assume page(0) always has results.

Issue : Member # comes back null Code read only the MB-typed identifier and didn't fall back.
Fix : identifier.firstOrNull { ... "MB" ... }?.value ?: coverage.subscriberId.

Issue : Plan Name or Group # returns null class entries were filtered by the wrong type.coding[].code, or the payor hasn't populated that class entry.
Fix : Confirm the filter code matches exactly ("plan" / "group") and treat a missing entry as "not provided by this payor," not a bug.

If your issue isn't listed here, reach out to support and include: the SDK version, the exact CoverageRequest builder calls used, and the full OperationOutcome from result.error if the failure branch was hit.


Did this page help you?