Create encounter

Create an encounter.

Explore further

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

Code sample


def create_encounter(
    """Creates a new Encounter resource in a FHIR store based on a Patient.

    before running the sample."""
    # Imports Python's built-in "os" module
    import os

    # Imports the google.auth.transport.requests transport
    from google.auth.transport import requests

    # Imports a module to allow authentication using a service account
    from google.oauth2 import service_account

    # Gets credentials from the environment.
    credentials = service_account.Credentials.from_service_account_file(
    scoped_credentials = credentials.with_scopes(
    # Creates a requests Session object with the credentials.
    session = requests.AuthorizedSession(scoped_credentials)

    # URL to the Cloud Healthcare API endpoint and version
    base_url = ""

    # TODO(developer): Uncomment these lines and replace with your values.
    # project_id = 'my-project'  # replace with your GCP project ID
    # location = 'us-central1'  # replace with the parent dataset's location
    # dataset_id = 'my-dataset'  # replace with the parent dataset's ID
    # fhir_store_id = 'my-fhir-store'  # replace with the FHIR store ID
    # patient_id = 'b682d-0e-4843-a4a9-78c9ac64'  # replace with the associated Patient resource's ID
    url = "{}/projects/{}/locations/{}".format(base_url, project_id, location)

    fhir_store_path = "{}/datasets/{}/fhirStores/{}/fhir/Encounter".format(
        url, dataset_id, fhir_store_id

    # Sets required application/fhir+json header on the request
    headers = {"Content-Type": "application/fhir+json;charset=utf-8"}

    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": "Patient/{}".format(patient_id)},
        "resourceType": "Encounter",

    response =, headers=headers, json=body)

    resource = response.json()

    print("Created Encounter resource with ID {}".format(resource["id"]))

    return response

What's next

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