RxCheck engineering guide

Check Drug Interactions in Python

A production-minded requests example with timeouts, errors, and result-status handling.

Published 2026-07-13 · Updated 2026-11-29 · 4 minute read

Install requests and keep the key on your server

Use Python in a backend process or a local development environment. Install the HTTP client with python -m pip install requests, obtain a key from Pricing, and set RXCHECK_API_KEY in that process environment. Keep the key out of committed source, browser bundles, query strings, screenshots, and logs. The sample reads the variable at request time so a missing key fails before contacting the API.

Run a bounded single-pair lookup

Save this example as check_interaction.py and run python check_interaction.py. Requests encodes drug1 and drug2 as query parameters. The connect and read timeouts are separate: the tuple limits connection setup and a stalled response. HTTP errors are handled before parsing a successful result. The example intentionally makes one request and does not retry automatically.

Python
import os
import requests


def check_pair(drug1, drug2):
    response = requests.get(
        "https://api.rxcheck.dev/v1/interactions",
        headers={"X-API-Key": os.environ["RXCHECK_API_KEY"]},
        params={"drug1": drug1, "drug2": drug2},
        timeout=(3.05, 10),
    )
    response.raise_for_status()
    result = response.json()
    if result.get("result_status") not in {"interaction_found", "no_record_found"}:
        raise ValueError("Unexpected result_status; do not infer safety")
    return result


if __name__ == "__main__":
    try:
        result = check_pair("warfarin", "aspirin")
        print(result["result_status"])
        print("Dataset:", result.get("dataset_release", "not returned"))
        if result["result_status"] == "no_record_found":
            print("No stored record; this does not establish safety.")
    except requests.Timeout:
        raise SystemExit("Lookup timed out; no conclusion is available")
    except requests.HTTPError as error:
        raise SystemExit(f"Lookup failed: HTTP {error.response.status_code}")
    except requests.RequestException:
        raise SystemExit("Lookup unavailable; no conclusion is available")
    except (ValueError, KeyError):
        raise SystemExit("Invalid response or missing RXCHECK_API_KEY")

Handle found, missing, and unavailable as separate states

interaction_found means the active dataset contains a matching stored record. no_record_found means no matching stored record was found; it is not a safety clearance. A timeout, failed HTTP response, or unexpected result_status supplies no usable lookup conclusion. Keep those failures separate from an empty result in your application. Store dataset_release with any result you retain so a later dataset update does not silently change the meaning of an earlier lookup.

Test the integration without making medical claims

Mock the HTTP boundary for contract tests. Cover a found record, a no-record response, HTTP 401 for an invalid key, HTTP 429 for a limit, a dependency failure, a timeout, and malformed JSON. Verify that every failure leaves the result unavailable and that no-record copy states its limitation. Keep medication inputs and raw response bodies out of routine logs; log a returned request identifier when available. Evaluate data fitness separately before any clinically consequential use.

Frequently asked questions

How do I authenticate the Python example?

Create a free API key from Pricing and set RXCHECK_API_KEY in your server process environment. The example sends it in the X-API-Key header, never in the URL.

Does no_record_found mean the medications are safe together?

No. It means the active dataset has no matching stored record. It is not a clinical safety conclusion.

Should I automatically retry a failed lookup?

This example does not retry. Handle invalid input and authentication errors explicitly, respect rate-limit responses, and only introduce bounded retries after defining how your application represents unavailable results.