Create function schedule

async AsyncCogniteClient.functions.schedules.create(
name: str | FunctionScheduleWrite,
cron_expression: str | None = None,
function_id: int | None = None,
function_external_id: str | None = None,
client_credentials: dict[str, str] | ClientCredentials | None = None,
description: str | None = None,
data: dict[str, object] | None = None,
) FunctionSchedule

Create a schedule associated with a specific project.

Parameters:
  • name (str | FunctionScheduleWrite) – Name of the schedule or FunctionSchedule object. If a function schedule object is passed, the other arguments are ignored except for the client_credentials argument.

  • cron_expression (str | None) – Cron expression.

  • function_id (int | None) – Id of the function to attach the schedule to.

  • function_external_id (str | None) – (DEPRECATED) External id of the function to attach the schedule to. Note: Will be automatically converted to (internal) ID, as schedules must be bound to an ID.

  • client_credentials (dict[str, str] | ClientCredentials | None) – Instance of ClientCredentials or a dictionary containing client credentials: ‘client_id’ and ‘client_secret’.

  • description (str | None) – Description of the schedule.

  • data (dict[str, object] | None) – Data to be passed to the scheduled run.

Returns:

Created function schedule.

Return type:

FunctionSchedule

Note

There are several ways to authenticate the function schedule — the order of priority is as follows:
  1. nonce (if provided in the FunctionScheduleWrite object)

  2. client_credentials (if provided)

  3. The credentials of this AsyncCogniteClient.

Warning

Do not pass secrets or other confidential information via the data argument. There is a dedicated secrets argument in FunctionsAPI.create() for this purpose.

Passing the reference to the Function by function_external_id is just here as a convenience to the user. The API require that all schedules must be attached to a Function by (internal) ID for authentication- and security purposes. This means that the lookup to get the ID is first done on behalf of the user.

Examples

Create a function schedule that runs using specified client credentials (recommended):

>>> import os
>>> from cognite.client import CogniteClient
>>> from cognite.client.data_classes import ClientCredentials
>>> client = CogniteClient()
>>> # async_client = AsyncCogniteClient()  # another option
>>> schedule = client.functions.schedules.create(
...     name="My schedule",
...     function_id=123,
...     cron_expression="*/5 * * * *",
...     client_credentials=ClientCredentials(
...         "my-client-id", os.environ["MY_CLIENT_SECRET"]
...     ),
...     description="This schedule does magic stuff.",
...     data={"magic": "stuff"},
... )

You may also create a schedule that runs with your -current- credentials, i.e. the same credentials you used to instantiate the AsyncCogniteClient (that you’re using right now). Note: Unless you happen to already use client credentials, this is not a recommended way to create schedules, as it will create an explicit dependency on your user account, which it will run the function “on behalf of” (until the schedule is eventually removed):

>>> schedule = client.functions.schedules.create(
...     name="My schedule",
...     function_id=456,
...     cron_expression="*/5 * * * *",
...     description="A schedule just used for some temporary testing.",
... )

Create a function schedule with an oneshot session (typically used for testing purposes):

>>> from cognite.client.data_classes.functions import FunctionScheduleWrite
>>> session = client.iam.sessions.create(session_type="ONESHOT_TOKEN_EXCHANGE")
>>> schedule = client.functions.schedules.create(
...     FunctionScheduleWrite(
...         name="My schedule",
...         function_id=456,
...         cron_expression="*/5 * * * *",
...         description="A schedule just used for some temporary testing.",
...         nonce=session.nonce,
...     ),
... )