Quickstart

Instantiate a new client

Use this code to instantiate a client in order to execute API calls to Cognite Data Fusion (CDF). The client_name is a user-defined string intended to give the client a unique identifier. You can provide the client_name by passing it directly to the ClientConfig constructor.

The Cognite API uses OpenID Connect (OIDC) to authenticate. Use one of the credential providers such as OAuthClientCredentials to authenticate:

Note

The following example sets a global client configuration which will be used if no config is explicitly passed to CogniteClient. All examples in this documentation assume that such a global configuration has been set.

from cognite.client import CogniteClient, ClientConfig, global_config
from cognite.client.credentials import OAuthClientCredentials

# This value will depend on the cluster your CDF project runs on
cluster = "api"
base_url = f"https://{cluster}.cognitedata.com"
tenant_id = "my-tenant-id"
client_id = "my-client-id"
# client secret should not be stored in-code, so we load it from an environment variable
client_secret = os.environ["MY_CLIENT_SECRET"]
creds = OAuthClientCredentials(
  token_url=f"https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token",
  client_id=client_id,
  client_secret=client_secret,
  scopes=[f"{base_url}/.default"]
)

cnf = ClientConfig(
  client_name="my-special-client",
  base_url=base_url,
  project="my-project",
  credentials=creds
)

global_config.default_client_config = cnf
client = CogniteClient()

Examples for all OAuth credential providers can be found in the Credential Providers section.

You can also make your own credential provider:

from cognite.client import CogniteClient, ClientConfig
from cognite.client.credentials import Token

def token_provider():
    ...

cnf = ClientConfig(
  client_name="my-special-client",
  base_url="https://<cluster>.cognitedata.com",
  project="my-project",
  credentials=Token(token_provider)
)
client = CogniteClient(cnf)

Discover time series

For this, you will need to supply ids for the time series that you want to retrieve. You can find some ids by listing the available time series. Limits for listing resources default to 25, so the following code will return the first 25 time series resources.

from cognite.client import CogniteClient

client = CogniteClient()
ts_list = client.time_series.list()

List available spaces in your Data Modeling project

In the following example, we list all spaces in the project.

from cognite.client import CogniteClient

client = CogniteClient()
spaces = client.data_modeling.spaces.list()