The Data Commons Python API is a Python library that enables developers to programmatically access nodes in the Data Commons knowledge graph. This package allows users to explore the structure of the graph, integrate statistics from the graph into data analysis workflows and much more. Please see the overview for more details on the design and structure of the API.
Before proceeding, make sure you have followed the setup instructions below.
To get started using the Python Client API requires the following steps:
- Install the API using
- Create an API key and enable the Data Commons API.
- Provide the API key to the Python Client API and begin developing.
First, install the
datacommons package through
$ pip install datacommons
For more information about installing
pip and setting up other parts of your Python development environment, please refer to the Python Development Environment Setup Guide for Google Cloud Platform.
Using the Data Commons Python API requires you to setup access to the Data Commons API on Google Cloud Platform. Follow the setup guide here.
With the API key created and Data Commons API activated, we can now get started using the Data Commons Python Client API. There are two ways to provide your key to the Python Client API package.
You can set the API key by calling
datacommons.set_api_key. Start by importing
datacommons, then set the API key like so.
import datacommons as dc dc.set_api_key('YOUR-API-KEY')
This will create an environment variable in your Python runtime called
DC_API_KEYholding your key. Your key will then be used whenever the package sends a request to the Data Commons graph.
You can export an environment variable in your shell like so.
After you’ve exported the variable, you can start using the Data Commons package!
import datacommons as dc
This route is particularly useful if you are building applications that depend on this API, and are deploying them to hosting services.
You are now ready to go! From here you can view our tutorials on how to use the API to perform certain tasks, or see a full list of functions, classes and methods available for use below.