Stay organized with collectionsSave and categorize content based on your preferences.
This page shows how to get started with the Cloud Client Libraries for the
Enterprise Knowledge Graph API. Client libraries make it easier to access
Google Cloud APIs from a supported language. Although you can use
Google Cloud APIs directly by making raw requests to the server, client
libraries provide simplifications that significantly reduce the amount of code
you need to write.
Read more about the Cloud Client Libraries
and the older Google API Client Libraries inClient libraries explained.
To authenticate calls to Google Cloud APIs, client libraries supportApplication Default Credentials (ADC);
the libraries look for credentials in a set of defined locations and use those credentials
to authenticate requests to the API. With ADC, you can make
credentials available to your application in a variety of environments, such as local
development or production, without needing to modify your application code.
For production environments, the way you set up ADC depends on the service
and context. For more information, seeSet up Application Default Credentials.
For a local development environment, you can set up ADC with the credentials
that are associated with your Google Account:
Installthe Google Cloud CLI.
After installation,initializethe Google Cloud CLI by running the following command:
The following example shows how to use the client library.
Python
from__future__importannotationsfromcollections.abcimportSequencefromgoogle.cloudimportenterpriseknowledgegraphasekg# TODO(developer): Uncomment these variables before running the sample.# project_id = 'YOUR_PROJECT_ID'# location = 'YOUR_GRAPH_LOCATION' # Values: 'global'# search_query = 'YOUR_SEARCH_QUERY'# languages = ['en'] # Optional: List of ISO 639-1 Codes# types = [''] # Optional: List of schema.org types to return# limit = 20 # Optional: Number of entities to returndefsearch_public_kg_sample(project_id:str,location:str,search_query:str,languages:Sequence[str]=None,types:Sequence[str]=None,limit:int=20,):# Create a clientclient=ekg.EnterpriseKnowledgeGraphServiceClient()# The full resource name of the location# e.g. projects/{project_id}/locations/{location}parent=client.common_location_path(project=project_id,location=location)# Initialize request argument(s)request=ekg.SearchPublicKgRequest(parent=parent,query=search_query,languages=languages,types=types,limit=limit,)# Make the requestresponse=client.search_public_kg(request=request)print(f"Search Query:{search_query}\n")# Extract and print date from responseforiteminresponse.item_list_element:result=item.get("result")print(f"Name:{result.get('name')}")print(f"- Description:{result.get('description')}")print(f"- Types:{result.get('@type')}\n")detailed_description=result.get("detailedDescription")ifdetailed_description:print("- Detailed Description:")print(f"\t- Article Body:{detailed_description.get('articleBody')}")print(f"\t- URL:{detailed_description.get('url')}")print(f"\t- License:{detailed_description.get('license')}\n")print(f"- Cloud MID:{result.get('@id')}")foridentifierinresult.get("identifier"):print(f"\t-{identifier.get('name')}:{identifier.get('value')}")print("\n")
Additional resources
Python
The following list contains links to more resources related to the
client library for Python:
[[["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"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThis page provides guidance on using the Cloud Client Libraries to interact with the Enterprise Knowledge Graph API, simplifying interactions compared to direct API requests.\u003c/p\u003e\n"],["\u003cp\u003eThe Enterprise Knowledge Graph Client Library is currently in a Pre-GA state, indicating it is available "as is" with potentially limited support, so please review the linked terms and launch descriptions for further information.\u003c/p\u003e\n"],["\u003cp\u003eYou can install the client library using \u003ccode\u003epip install --upgrade google-cloud-enterpriseknowledgegraph\u003c/code\u003e, which will enable you to set up authentication with Application Default Credentials (ADC) for both local development and production environments.\u003c/p\u003e\n"],["\u003cp\u003eThe client library authenticates requests using Application Default Credentials, which can be set up via the Google Cloud CLI using the \u003ccode\u003egcloud init\u003c/code\u003e and \u003ccode\u003egcloud auth application-default login\u003c/code\u003e commands.\u003c/p\u003e\n"],["\u003cp\u003eAn example is given in Python to demonstrate how to use the client library to search the public knowledge graph, including how to initiate a search, make the request, and extract the relevant data from the response.\u003c/p\u003e\n"]]],[],null,["This page shows how to get started with the Cloud Client Libraries for the\nEnterprise Knowledge Graph API. Client libraries make it easier to access\nGoogle Cloud APIs from a supported language. Although you can use\nGoogle Cloud APIs directly by making raw requests to the server, client\nlibraries provide simplifications that significantly reduce the amount of code\nyou need to write.\n\nRead more about the Cloud Client Libraries\nand the older Google API Client Libraries in\n[Client libraries explained](/apis/docs/client-libraries-explained).\n|\n| **Preview**\n|\n|\n| This library is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| Pre-GA libraries are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\n\nInstall the client library \n\nPython \n\n```\npip install --upgrade google-cloud-enterpriseknowledgegraph\n```\n\nFor more information, see [Setting Up a Python Development Environment](/python/docs/setup).\n\n\u003cbr /\u003e\n\nSet up authentication To authenticate calls to Google Cloud APIs, client libraries support [Application Default Credentials (ADC)](/docs/authentication/application-default-credentials); the libraries look for credentials in a set of defined locations and use those credentials to authenticate requests to the API. With ADC, you can make credentials available to your application in a variety of environments, such as local development or production, without needing to modify your application code.\n\nFor production environments, the way you set up ADC depends on the service\nand context. For more information, see [Set up Application Default Credentials](/docs/authentication/provide-credentials-adc).\n\nFor a local development environment, you can set up ADC with the credentials\nthat are associated with your Google Account:\n\n1.\n [Install](/sdk/docs/install) the Google Cloud CLI.\n\n After installation,\n [initialize](/sdk/docs/initializing) the Google Cloud CLI by running the following command:\n\n ```bash\n gcloud init\n ```\n\n\n If you're using an external identity provider (IdP), you must first\n [sign in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n2.\n\n If you're using a local shell, then create local authentication credentials for your user\n account:\n\n ```bash\n gcloud auth application-default login\n ```\n\n You don't need to do this if you're using Cloud Shell.\n\n\n If an authentication error is returned, and you are using an external identity provider\n (IdP), confirm that you have\n [signed in to the gcloud CLI with your federated identity](/iam/docs/workforce-log-in-gcloud).\n\n\n A sign-in screen appears. After you sign in, your credentials are stored in the\n [local credential file used by ADC](/docs/authentication/application-default-credentials#personal).\n\nUse the client library\n\n\nThe following example shows how to use the client library. \n\nPython \n\n\n from __future__ import annotations\n\n from collections.abc import Sequence\n\n from google.cloud import enterpriseknowledgegraph as ekg\n\n # TODO(developer): Uncomment these variables before running the sample.\n # project_id = 'YOUR_PROJECT_ID'\n # location = 'YOUR_GRAPH_LOCATION' # Values: 'global'\n # search_query = 'YOUR_SEARCH_QUERY'\n # languages = ['en'] # Optional: List of ISO 639-1 Codes\n # types = [''] # Optional: List of schema.org types to return\n # limit = 20 # Optional: Number of entities to return\n\n\n def search_public_kg_sample(\n project_id: str,\n location: str,\n search_query: str,\n languages: Sequence[str] = None,\n types: Sequence[str] = None,\n limit: int = 20,\n ):\n # Create a client\n client = ekg.EnterpriseKnowledgeGraphServiceClient()\n\n # The full resource name of the location\n # e.g. projects/{project_id}/locations/{location}\n parent = client.common_location_path(project=project_id, location=location)\n\n # Initialize request argument(s)\n request = ekg.SearchPublicKgRequest(\n parent=parent,\n query=search_query,\n languages=languages,\n types=types,\n limit=limit,\n )\n\n # Make the request\n response = client.search_public_kg(request=request)\n\n print(f\"Search Query: {search_query}\\n\")\n\n # Extract and print date from response\n for item in response.item_list_element:\n result = item.get(\"result\")\n\n print(f\"Name: {result.get('name')}\")\n print(f\"- Description: {result.get('description')}\")\n print(f\"- Types: {result.get('@type')}\\n\")\n\n detailed_description = result.get(\"detailedDescription\")\n\n if detailed_description:\n print(\"- Detailed Description:\")\n print(f\"\\t- Article Body: {detailed_description.get('articleBody')}\")\n print(f\"\\t- URL: {detailed_description.get('url')}\")\n print(f\"\\t- License: {detailed_description.get('license')}\\n\")\n\n print(f\"- Cloud MID: {result.get('@id')}\")\n for identifier in result.get(\"identifier\"):\n print(f\"\\t- {identifier.get('name')}: {identifier.get('value')}\")\n\n print(\"\\n\")\n\n\u003cbr /\u003e\n\nAdditional resources \n\nPython\n\nThe following list contains links to more resources related to the\nclient library for Python:\n\n- [API reference](/python/docs/reference/enterpriseknowledgegraph/latest)\n- [Client libraries best practices](/apis/docs/client-libraries-best-practices)\n- [Issue tracker](https://github.com/GoogleCloudPlatform/google-cloud-python/issues)\n- [`enterprise-knowledge-graph` on Stack Overflow](https://stackoverflow.com/search?q=%5Benterprise-knowledge-graph%5D+%5Bpython%5D)\n- [Source code](https://github.com/GoogleCloudPlatform/google-cloud-python)\n\n\u003cbr /\u003e"]]