Python
Install
Bash
pip install wex-health-sdkConfigure
Create one ApiClient at application startup. In FastAPI, use the lifespan hook to share it across all requests:
Python
import os, wex_health
from contextlib import asynccontextmanager
from fastapi import FastAPI
config = wex_health.Configuration(
host=os.environ["WEX_API_HOST"],
oauth2_credentials={
"client_id": os.environ["WEX_CLIENT_ID"],
"client_secret": os.environ["WEX_CLIENT_SECRET"],
"access_token_url": os.environ["WEX_TOKEN_URL"],
},
retries=3, # default — override as needed
request_timeout=30.0,
)
@asynccontextmanager
async def lifespan(app: FastAPI):
app.state.wex_client = wex_health.ApiClient(config)
await app.state.wex_client.__aenter__()
yield
await app.state.wex_client.__aexit__(None, None, None)For non-FastAPI applications, use async with ApiClient(config) as client: and keep that client alive for the lifetime of the process.
Call APIs
Python
async with ApiClient(config) as client:
claims_api = ClaimsApi(client)
# Single claim
claim = await claims_api.get_claim(
person_id="person-uuid-here",
claim_number="11BPEB1210513P0000101",
x_request_id="my-correlation-id" # optional — for tracing
)
# All claims for a participant
claims = await claims_api.get_claims_for_participant(
person_id="person-uuid-here",
x_request_id="correlation-123"
)
Handle Errors
Python
from wex_health.rest import ApiException
try:
return await api.get_consumer(consumer_id=consumer_id)
except ApiException as e:
if e.status == 401:
raise AuthenticationError("Invalid credentials")
elif e.status == 404:
return None # resource does not exist — not retried
elif e.status == 429:
raise RateLimitError("Too many requests")
raise # 5xx will have been retried before reaching hereLogging
The SDK integrates with Python's standard logging module:
Python
import logging
logging.getLogger("wex_health").setLevel(logging.INFO)Was this section helpful?
On this page
- Python