Create Encounter
Stay organized with collections
Save and categorize content based on your preferences.
Create a FHIR Encounter resource.
Explore further
For detailed documentation that includes this code sample, see the following:
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Create Encounter\n\nCreate a FHIR Encounter resource.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Creating and managing FHIR resources](/healthcare-api/docs/how-tos/fhir-resources)\n\nCode sample\n-----------\n\n### Python\n\n\nBefore trying this sample, follow the Python setup instructions in the\n[Cloud Healthcare API quickstart using\nclient libraries](https://cloud.google.com/healthcare-api/docs/store-healthcare-data-client-library).\n\n\nFor more information, see the\n[Cloud Healthcare API Python API\nreference documentation](https://googleapis.github.io/google-api-python-client/docs/dyn/healthcare_v1).\n\n\nTo authenticate to Cloud Healthcare API, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n # Imports the types Dict and Any for runtime type hints.\n from typing import Any, Dict # noqa: E402\n\n def create_encounter(\n project_id: str,\n location: str,\n dataset_id: str,\n fhir_store_id: str,\n patient_id: str,\n ) -\u003e Dict[str, Any]:\n \"\"\"Creates a new Encounter resource in a FHIR store that references a Patient resource.\n\n See\n https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/healthcare/api-client/v1/fhir\n before running the sample.\n See\n https://googleapis.github.io/google-api-python-client/docs/dyn/healthcare_v1.projects.locations.datasets.fhirStores.fhir.html#create\n for the Python API reference.\n\n Args:\n project_id: The project ID or project number of the Cloud project you want\n to use.\n location: The name of the parent dataset's location.\n dataset_id: The name of the parent dataset.\n fhir_store_id: The name of the FHIR store.\n patient_id: The \"logical id\" of the referenced Patient resource. The ID is\n assigned by the server.\n\n Returns:\n A dict representing the created Encounter resource.\n \"\"\"\n # Imports the Google API Discovery Service.\n from googleapiclient import discovery\n\n api_version = \"v1\"\n service_name = \"healthcare\"\n\n # Returns an authorized API client by discovering the Healthcare API\n # and using GOOGLE_APPLICATION_CREDENTIALS environment variable.\n client = discovery.build(service_name, api_version)\n\n # TODO(developer): Uncomment these lines and replace with your values.\n # project_id = 'my-project'\n # location = 'us-central1'\n # dataset_id = 'my-dataset'\n # fhir_store_id = 'my-fhir-store'\n # patient_id = 'b682d-0e-4843-a4a9-78c9ac64' # replace with the associated Patient resource's ID\n fhir_store_parent = (\n f\"projects/{project_id}/locations/{location}/datasets/{dataset_id}\"\n )\n fhir_store_name = f\"{fhir_store_parent}/fhirStores/{fhir_store_id}\"\n\n encounter_body = {\n \"status\": \"finished\",\n \"class\": {\n \"system\": \"http://hl7.org/fhir/v3/ActCode\",\n \"code\": \"IMP\",\n \"display\": \"inpatient encounter\",\n },\n \"reason\": [\n {\n \"text\": (\n \"The patient had an abnormal heart rate. She was\"\n \" concerned about this.\"\n )\n }\n ],\n \"subject\": {\"reference\": f\"Patient/{patient_id}\"},\n \"resourceType\": \"Encounter\",\n }\n\n request = (\n client.projects()\n .locations()\n .datasets()\n .fhirStores()\n .fhir()\n .create(parent=fhir_store_name, type=\"Encounter\", body=encounter_body)\n )\n # Sets required application/fhir+json header on the googleapiclient.http.HttpRequest.\n request.headers[\"content-type\"] = \"application/fhir+json;charset=utf-8\"\n response = request.execute()\n print(f\"Created Encounter resource with ID {response['id']}\")\n\n return response\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=healthcare)."]]