import os
from qiskit_ibm_runtime import QiskitRuntimeService

def get_service(api_key: str = None, instance: str = None) -> QiskitRuntimeService:
    token = api_key or os.environ.get("IBMQ_API_KEY")
    crn = instance or os.environ.get("IBMQ_INSTANCE")

    if not token:
        raise ValueError(
            "IBM Cloud API key required. "
            "Set IBMQ_API_KEY env var or pass api_key= parameter."
        )
    if not crn:
        raise ValueError(
            "IBM instance CRN required. "
            "Set IBMQ_INSTANCE env var or pass instance= parameter. "
            "Get yours free at quantum.cloud.ibm.com"
        )

    return QiskitRuntimeService(
        channel="ibm_cloud",
        token=token,
        instance=crn
    )