Upsert assets

async AsyncCogniteClient.assets.upsert(
item: Asset | AssetWrite | Sequence[Asset | AssetWrite],
mode: Literal['patch', 'replace'] = 'patch',
) Asset | AssetList

Upsert assets, i.e., update if it exists, and create if it does not exist.

Note this is a convenience method that handles the upserting for you by first calling update on all items, and if any of them fail because they do not exist, it will create them instead.

For more details, see Upsert.

Parameters:
  • item (Asset | AssetWrite | Sequence[Asset | AssetWrite]) – Asset or list of assets to upsert.

  • mode (Literal['patch', 'replace']) – Whether to patch or replace in the case the assets are existing. If you set ‘patch’, the call will only update fields with non-null values (default). Setting ‘replace’ will unset any fields that are not specified.

Returns:

The upserted asset(s).

Return type:

Asset | AssetList

Examples

Upsert for assets:

>>> from cognite.client import CogniteClient
>>> from cognite.client.data_classes import AssetWrite
>>> client = CogniteClient()
>>> # async_client = AsyncCogniteClient()  # another option
>>> existing_asset = client.assets.retrieve(id=1)
>>> existing_asset.description = "New description"
>>> new_asset = AssetWrite(
...     external_id="new_asset", name="my asset", description="New asset"
... )
>>> res = client.assets.upsert([existing_asset, new_asset], mode="replace")