API Overview¶
This page summarizes the core concepts of the Mednet EDC REST API and how the SDK interacts with it.
The SDK uses httpx for both synchronous and asynchronous HTTP transport.
Lifecycle Management¶
ImednetSDK and AsyncImednetSDK
have distinct, mutually exclusive lifecycle management methods:
ImednetSDK– synchronous only. Usewith ImednetSDK(...) as sdk:or callsdk.close(). Callingclose()on an instance that has an active async client raisesRuntimeError; useawait sdk.aclose()in that case.AsyncImednetSDK– asynchronous only. Always useasync with AsyncImednetSDK(...) as sdk:or callawait sdk.aclose()explicitly. Using the synchronous context manager (with AsyncImednetSDK(...)) raisesTypeError.
Base URL¶
The SDK communicates with the production API at
https://edc.prod.imednetapi.com by default. Override this with
IMEDNET_BASE_URL or the base_url argument to load_config for a
private deployment. See Configuration for details.
Authentication¶
Authentication and request header generation are owned by
AuthStrategy implementations rather than
hardcoded client headers (see ADR 0005):
ApiKeyAuth– populatesx-api-keyandx-imn-security-keyheaders for static Mednet API/security key pairs.OIDCAuth– verifies JWT signatures and claims (exp,nbfwith optionalleeway,aud, andiss), emitsAuthorization: Bearer <token>, and extractssub/preferred_usernameuser identifiers plusroles/groupsRBAC claims (admin,manager,reviewer,viewer).OIDCAuthdefaults to asymmetric JWS algorithms (RS*,PS*,ES*,EdDSA), enforces strict key-type separation (rejectingalg: "none"and symmetricoctJWKs), and reads verification settings fromIMEDNET_OIDC_PUBLIC_KEY,IMEDNET_OIDC_JWKS_URL,IMEDNET_OIDC_AUDIENCE,IMEDNET_OIDC_ALGORITHMS, andIMEDNET_OIDC_ISSUER.Custom
AuthStrategy– passing an explicitauth=...strategy initializesImednetSDK,AsyncImednetSDK,Client, andAsyncClientdirectly without requiring placeholder API or security keys. Supplyingauthalongside an already constructedclientorasync_clientraisesValueErrorbecause the injected client owns its transport headers.
When auth is not passed explicitly, imednet.config.load_config()
resolves each credential field in strict precedence order:
Explicit constructor or
load_config(...)keyword arguments (explicit empty strings suppress fallback and fail validation withConfigurationError).A caller-owned
CredentialSourcesnapshot such asMemoryCredentialStorereturningCredentialsin memory without touching filesystem state. Lookup errors fail closed asConfigurationErrorwithout echoing raw secret text, andConfig,Credentials,MemoryCredentialStore,ApiKeyAuth, andOIDCAuthredact secrets inrepr().Environment variables (
IMEDNET_OIDC_TOKEN,IMEDNET_API_KEY, andIMEDNET_SECURITY_KEY).
Once resolved, oidc_token takes precedence over static api_key and
security_key pairs. Updating or clearing a MemoryCredentialStore affects
subsequent client initializations without mutating live client instances. See
Configuration for all authentication environment variables.
import jwt
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric.ed25519 import Ed25519PrivateKey
from imednet import ImednetSDK
from imednet.auth import Credentials, MemoryCredentialStore, OIDCAuth
from imednet.simulation import SimulationTransport
signing_key = Ed25519PrivateKey.generate()
public_pem = signing_key.public_key().public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo,
)
signed_jwt = jwt.encode(
{
"sub": "auditor@study.example",
"iss": "https://idp.study.example",
"aud": "imednet-edc",
"roles": ["admin"],
},
signing_key,
algorithm="EdDSA",
)
oidc_auth = OIDCAuth(
signed_jwt,
public_key=public_pem,
issuer="https://idp.study.example",
audience="imednet-edc",
)
assert oidc_auth.get_user_roles() == ["admin"]
assert "Bearer " in oidc_auth.get_headers()["Authorization"]
with ImednetSDK(auth=oidc_auth, transport=SimulationTransport()) as sdk:
studies = list(sdk.studies.list())
assert len(studies) >= 1
cred_store = MemoryCredentialStore(
Credentials(
api_key="mem-api-key", # pragma: allowlist secret
security_key="mem-security-key", # pragma: allowlist secret
)
)
assert "<redacted>" in repr(cred_store)
with ImednetSDK(credential_source=cred_store, transport=SimulationTransport()) as sdk:
assert sdk.auth is not None
HTTP methods and errors¶
The API primarily supports GET and POST requests. Non‐successful status
codes raise typed ApiError subclasses:
400–BadRequestError401–UnauthorizedError403–ForbiddenError404–NotFoundError409–ConflictError429–RateLimitError5xx–ServerError
Every SDK exception inherits from ImednetError and carries an
optional DiagnosticContext (with method, query-redacted
url, retry_after, attempt_count, and cause_type):
Network-level transport failures (such as connection drops or read timeouts) raise
RequestErrorafter exhausting configured retries, populatingattempt_countwith the executed wire attempts andcause_typewith the underlyinghttpx.RequestErrorclass name.When a client’s
CircuitBreakeris open after consecutive upstream failures, pre-flight calls raiseCircuitBreakerError—a direct subclass ofImednetErrorrather thanRequestError—withattempt_count == 0so callers can separate fast-fail circuit blocks from wire-level network errors while still catchingImednetErroras a single root.
See Retry Policy and Error Handling for examples of handling these errors and configuring custom retry logic.
Dynamic path segment validation¶
Before constructing REST endpoint paths for a Study, Subject, Site, Form,
Record, Visit, Interval, Variable, Query, Data Dictionary, or
background Job, the SDK validates every caller-supplied URL segment through
validate_path_segment() and formats paths with
build_safe_path():
validate_path_segment()rejectsNone,boolvalues, empty or whitespace-only strings, ASCII control characters, path separators (/and\), query/fragment delimiters (?and#), dot segments (.and..), and single- or multi-stage percent-encoded traversal sequences (such as%2e%2eor%252f), raisingPathTraversalValidationError(aValidationErrorsubclass withstatus_code=400) before any HTTP request leaves the client.build_safe_path()validatesbase_pathand each positional path segment, returning a slash-normalized relative path with reserved characters percent-encoded while keeping@unescaped for Mednet identifier compatibility.
from imednet.errors import PathTraversalValidationError
from imednet.utils import build_safe_path, validate_path_segment
assert validate_path_segment("STUDY_01", name="study_key") == "STUDY_01"
safe_job_path = build_safe_path("studies", "STUDY 01", "jobs", "batch@2026")
assert safe_job_path == "studies/STUDY%2001/jobs/batch@2026"
for unsafe_study_key in ("../admin", "%2e%2e%2fsecret", "STUDY?admin=1"):
rejected_seg = False
try:
validate_path_segment(unsafe_study_key, name="study_key")
except PathTraversalValidationError as path_err:
rejected_seg = True
assert path_err.status_code == 400
assert rejected_seg
Filtering¶
Endpoints accept a filter parameter that matches the syntax documented in the
Mednet API guide. For clinical data queries, recordDataFilter can be supplied
alongside filter. See imednet.endpoints.records for usage examples.
Endpoints are stateless and fetch fresh data on every call. For local schema and record caching helpers, see Caching Behavior.
Dates must use UTC timestamps except where noted. When filtering visits by
startDate, dueDate, endDate or visitDate, use YYYY-MM-DD.
String values containing spaces or special characters must be wrapped in
double quotes. For example: siteName=="My Site".
Filter helper¶
Use imednet.utils.filters.build_filter_string() to construct filter
expressions from a mapping. Values containing spaces must be wrapped in double
quotes. Example:
build_filter_string({"site_name": "Bright Test Site"})
# siteName=="Bright Test Site"
Error responses¶
When a request fails, error details are returned in the metadata section of
the response body. Validation errors include the offending field and value.
Example:
{
"metadata": {
"status": "BAD_REQUEST",
"path": "/api/v1/edc/studies",
"timestamp": "2018-10-18 05:46:29",
"error": {
"code": "1000",
"description": "Field raised validation errors",
"field": {
"attribute": "page",
"value": "XX"
}
}
}
}
Error response fields¶
codeError code
descriptionError description message
field.attributeOrigination request attribute which caused the error
field.valueThe value of request attribute passed in the request
ApiError normalizes both single-object metadata.error payloads
and multi-error lists into errors while exposing the
primary offending attribute/variable name via field_attribute
(reading field.attribute or field.variable) and the redacted offending value via
field_value:
from imednet.errors import BadRequestError, DiagnosticContext
diag = DiagnosticContext(
method="GET",
url="https://edc.prod.imednetapi.com/api/v1/edc/studies/STUDY/records?api_key=secret&page=XX", # pragma: allowlist secret
attempt_count=1,
)
bad_req = BadRequestError(
{
"metadata": {
"status": "BAD_REQUEST",
"error": [
{
"code": "1000",
"description": "Field raised validation errors",
"field": {"attribute": "page", "value": "XX"},
},
{
"code": "1000",
"description": "Unknown Variable filter",
"field": {"variable": "V_WEIGHT", "value": "-1"},
},
],
}
},
status_code=400,
diagnostic_context=diag,
)
assert len(bad_req.errors) == 2
assert bad_req.field_attribute == "page"
assert bad_req.field_value == "XX"
assert bad_req.method == "GET"
assert "secret" not in (bad_req.url or "")
assert "page=XX" in (bad_req.url or "")
Error codes¶
1000Validation error. Request contain invalid value.
9000Unknown error. Please contact Mednet support for assistance.
9001Unauthorized error. Insufficient permission to retrieve data.
Integrations and analytical exports¶
For DuckDB table exports, Hive-partitioned Parquet layouts, and incremental bronze/silver ingestion workflows, see DuckDB Integration.