Create Encounter

Create a FHIR Encounter resource.

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample


Before trying this sample, follow the Python setup instructions in the Cloud Healthcare API quickstart using client libraries. For more information, see the Cloud Healthcare API Python API reference documentation.

To authenticate to Cloud Healthcare API, set up Application Default Credentials. For more information, see Set up authentication for a local development environment.

# Imports the types Dict and Any for runtime type hints.
from typing import Any, Dict  # noqa: E402

def create_encounter(
    project_id: str,
    location: str,
    dataset_id: str,
    fhir_store_id: str,
    patient_id: str,
) -> Dict[str, Any]:
    """Creates a new Encounter resource in a FHIR store that references a Patient resource.

    before running the sample.
    for the Python API reference.

      project_id: The project ID or project number of the Cloud project you want
        to use.
      location: The name of the parent dataset's location.
      dataset_id: The name of the parent dataset.
      fhir_store_id: The name of the FHIR store.
      patient_id: The "logical id" of the referenced Patient resource. The ID is
        assigned by the server.

      A dict representing the created Encounter resource.
    # Imports the Google API Discovery Service.
    from googleapiclient import discovery

    api_version = "v1"
    service_name = "healthcare"

    # Returns an authorized API client by discovering the Healthcare API
    # and using GOOGLE_APPLICATION_CREDENTIALS environment variable.
    client =, api_version)

    # TODO(developer): Uncomment these lines and replace with your values.
    # project_id = 'my-project'
    # location = 'us-central1'
    # dataset_id = 'my-dataset'
    # fhir_store_id = 'my-fhir-store'
    # patient_id = 'b682d-0e-4843-a4a9-78c9ac64'  # replace with the associated Patient resource's ID
    fhir_store_parent = (
    fhir_store_name = f"{fhir_store_parent}/fhirStores/{fhir_store_id}"

    encounter_body = {
        "status": "finished",
        "class": {
            "system": "",
            "code": "IMP",
            "display": "inpatient encounter",
        "reason": [
                "text": (
                    "The patient had an abnormal heart rate. She was"
                    " concerned about this."
        "subject": {"reference": f"Patient/{patient_id}"},
        "resourceType": "Encounter",

    request = (
        .create(parent=fhir_store_name, type="Encounter", body=encounter_body)
    # Sets required application/fhir+json header on the googleapiclient.http.HttpRequest.
    request.headers["content-type"] = "application/fhir+json;charset=utf-8"
    response = request.execute()
    print(f"Created Encounter resource with ID {response['id']}")

    return response

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser.