Create Observation
Stay organized with collections
Save and categorize content based on your preferences.
Create a FHIR Observation 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"]],[],[[["\u003cp\u003eThis code sample demonstrates how to create a new FHIR Observation resource within a specified FHIR store using the Google Cloud Healthcare API.\u003c/p\u003e\n"],["\u003cp\u003eThe Python function \u003ccode\u003ecreate_observation\u003c/code\u003e requires parameters such as project ID, location, dataset ID, FHIR store ID, patient ID, and encounter ID to create the Observation.\u003c/p\u003e\n"],["\u003cp\u003eThe code utilizes the Google API Discovery Service to interact with the Healthcare API and constructs an Observation resource with details like resource type, code, status, subject, effective date/time, value quantity, and context.\u003c/p\u003e\n"],["\u003cp\u003eThe newly created observation resource is associated with a patient and an encounter resources, referencing them by the respective IDs.\u003c/p\u003e\n"],["\u003cp\u003eThe function returns a dictionary representing the created Observation resource.\u003c/p\u003e\n"]]],[],null,["# Create Observation\n\nCreate a FHIR Observation 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_observation(\n project_id: str,\n location: str,\n dataset_id: str,\n fhir_store_id: str,\n patient_id: str,\n encounter_id: str,\n ) -\u003e Dict[str, Any]:\n \"\"\"Creates a new Observation resource in a FHIR store that references an Encounter and 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 encounter_id: The \"logical id\" of the referenced Encounter resource. The ID\n is assigned by the server.\n\n Returns:\n A dict representing the created Observation 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 # encounter_id = 'a7602f-ffba-470a-a5c1-103f993c6 # replace with the associated Encounter resource's ID\n fhir_store_parent = (\n f\"projects/{project_id}/locations/{location}/datasets/{dataset_id}\"\n )\n fhir_observation_path = (\n f\"{fhir_store_parent}/fhirStores/{fhir_store_id}/fhir/Observation\"\n )\n\n observation_body = {\n \"resourceType\": \"Observation\",\n \"code\": {\n \"coding\": [\n {\n \"system\": \"http://loinc.org\",\n \"code\": \"8867-4\",\n \"display\": \"Heart rate\",\n }\n ]\n },\n \"status\": \"final\",\n \"subject\": {\"reference\": f\"Patient/{patient_id}\"},\n \"effectiveDateTime\": \"2019-01-01T00:00:00+00:00\",\n \"valueQuantity\": {\"value\": 80, \"unit\": \"bpm\"},\n \"context\": {\"reference\": f\"Encounter/{encounter_id}\"},\n }\n\n request = (\n client.projects()\n .locations()\n .datasets()\n .fhirStores()\n .fhir()\n .create(\n parent=fhir_observation_path,\n type=\"Observation\",\n body=observation_body,\n )\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 Observation 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)."]]