Create or update nodes/edges
Creates, patches, replaces, or deletes nodes and edges in one transaction. Requires dataModelsAcl:WRITE. See Create, patch, and replace.
from cognite.client.data_classes.data_modeling import EdgeApply, NodeOrEdgeData, NodeApply
node = NodeApply("mySpace", "myNodeId")
res = client.data_modeling.instances.apply(node)
from cognite.client.data_classes.data_modeling import EdgeApply, NodeOrEdgeData, NodeApply, ViewId
actor = NodeApply(
space="actors",
external_id="arnold_schwarzenegger",
sources=[
NodeOrEdgeData(
ViewId("mySpace", "PersonView", "v1"),
{"name": "Arnold Schwarzenegger", "birthYear": 1947}
),
NodeOrEdgeData(
ViewId("mySpace", "ActorView", "v1"),
{"wonOscar": False}
)
]
)
movie = NodeApply(
space="movies",
external_id="Terminator",
sources=[
NodeOrEdgeData(
ViewId("mySpace", "MovieView", "v1"),
{"title": "Terminator", "releaseYear": 1984}
)
]
)
# This is one-to-many edge, in this case from a person to a movie
actor_to_movie = EdgeApply(
space="actors",
external_id="relation:arnold_schwarzenegger:terminator",
type=("types", "acts-in"),
start_node=("actors", "arnold_schwarzenegger"),
end_node=("movies", "Terminator"),
)
res = client.data_modeling.instances.apply([actor, movie], [actor_to_movie])
from cognite.client.data_classes.data_modeling import EdgeApply
actor_to_movie = EdgeApply(
space="actors",
external_id="relation:arnold_schwarzenegger:terminator",
type=("types", "acts-in"),
start_node=("actors", "arnold_schwarzenegger"),
end_node=("movies", "Terminator"),
)
res = client.data_modeling.instances.apply(
edges=actor_to_movie,
auto_create_start_nodes=True,
auto_create_end_nodes=True
)
from cognite.client.utils import datetime_to_ms_iso_timestamp
from datetime import datetime, timezone
my_date = datetime(2020, 3, 14, 15, 9, 26, 535000, tzinfo=timezone.utc)
data_model_timestamp = datetime_to_ms_iso_timestamp(my_date) # "2020-03-14T15:09:26.535+00:00"
from cognite.client.data_classes.data_modeling import TypedNodeApply, PropertyOptions
class PersonApply(TypedNodeApply):
birth_year = PropertyOptions(identifier="birthYear")
def __init__(self, space: str, external_id, name: str, birth_year: int):
super().__init__(space, external_id, type=("sp_model_space", "Person"))
self.name = name
self.birth_year = birth_year
def get_source(self):
return ViewId("sp_model_space", "Person", "v1")
person = PersonApply("sp_date_space", "my_person", "John Doe", 1980)
res = client.data_modeling.instances.apply(nodes=person)curl --request POST \
--url https://{cluster}.cognitedata.com/api/v1/projects/{project}/models/instances \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"items": [
{
"instanceType": "node",
"space": "<string>",
"externalId": "<string>",
"existingVersion": 123,
"sources": [
{
"source": {
"type": "view",
"space": "<string>",
"externalId": "<string>",
"version": "<string>"
},
"properties": {
"someStringProperty": "someStringValue",
"someDirectRelation": {
"space": "mySpace",
"externalId": "someNode"
},
"someIntArrayProperty": [
1,
2,
3,
4
]
}
}
]
}
],
"delete": [
{
"space": "<string>",
"externalId": "<string>",
"existingVersion": 123
}
],
"autoCreateDirectRelations": true,
"autoCreateStartNodes": false,
"autoCreateEndNodes": false,
"skipOnVersionConflict": false,
"replace": false
}
'const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
items: [
{
instanceType: 'node',
space: '<string>',
externalId: '<string>',
existingVersion: 123,
sources: [
{
source: {type: 'view', space: '<string>', externalId: '<string>', version: '<string>'},
properties: {
someStringProperty: 'someStringValue',
someDirectRelation: {space: 'mySpace', externalId: 'someNode'},
someIntArrayProperty: [1, 2, 3, 4]
}
}
]
}
],
delete: [{space: '<string>', externalId: '<string>', existingVersion: 123}],
autoCreateDirectRelations: true,
autoCreateStartNodes: false,
autoCreateEndNodes: false,
skipOnVersionConflict: false,
replace: false
})
};
fetch('https://{cluster}.cognitedata.com/api/v1/projects/{project}/models/instances', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://{cluster}.cognitedata.com/api/v1/projects/{project}/models/instances",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'items' => [
[
'instanceType' => 'node',
'space' => '<string>',
'externalId' => '<string>',
'existingVersion' => 123,
'sources' => [
[
'source' => [
'type' => 'view',
'space' => '<string>',
'externalId' => '<string>',
'version' => '<string>'
],
'properties' => [
'someStringProperty' => 'someStringValue',
'someDirectRelation' => [
'space' => 'mySpace',
'externalId' => 'someNode'
],
'someIntArrayProperty' => [
1,
2,
3,
4
]
]
]
]
]
],
'delete' => [
[
'space' => '<string>',
'externalId' => '<string>',
'existingVersion' => 123
]
],
'autoCreateDirectRelations' => true,
'autoCreateStartNodes' => false,
'autoCreateEndNodes' => false,
'skipOnVersionConflict' => false,
'replace' => false
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://{cluster}.cognitedata.com/api/v1/projects/{project}/models/instances"
payload := strings.NewReader("{\n \"items\": [\n {\n \"instanceType\": \"node\",\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"existingVersion\": 123,\n \"sources\": [\n {\n \"source\": {\n \"type\": \"view\",\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"version\": \"<string>\"\n },\n \"properties\": {\n \"someStringProperty\": \"someStringValue\",\n \"someDirectRelation\": {\n \"space\": \"mySpace\",\n \"externalId\": \"someNode\"\n },\n \"someIntArrayProperty\": [\n 1,\n 2,\n 3,\n 4\n ]\n }\n }\n ]\n }\n ],\n \"delete\": [\n {\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"existingVersion\": 123\n }\n ],\n \"autoCreateDirectRelations\": true,\n \"autoCreateStartNodes\": false,\n \"autoCreateEndNodes\": false,\n \"skipOnVersionConflict\": false,\n \"replace\": false\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://{cluster}.cognitedata.com/api/v1/projects/{project}/models/instances")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"items\": [\n {\n \"instanceType\": \"node\",\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"existingVersion\": 123,\n \"sources\": [\n {\n \"source\": {\n \"type\": \"view\",\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"version\": \"<string>\"\n },\n \"properties\": {\n \"someStringProperty\": \"someStringValue\",\n \"someDirectRelation\": {\n \"space\": \"mySpace\",\n \"externalId\": \"someNode\"\n },\n \"someIntArrayProperty\": [\n 1,\n 2,\n 3,\n 4\n ]\n }\n }\n ]\n }\n ],\n \"delete\": [\n {\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"existingVersion\": 123\n }\n ],\n \"autoCreateDirectRelations\": true,\n \"autoCreateStartNodes\": false,\n \"autoCreateEndNodes\": false,\n \"skipOnVersionConflict\": false,\n \"replace\": false\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://{cluster}.cognitedata.com/api/v1/projects/{project}/models/instances")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"items\": [\n {\n \"instanceType\": \"node\",\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"existingVersion\": 123,\n \"sources\": [\n {\n \"source\": {\n \"type\": \"view\",\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"version\": \"<string>\"\n },\n \"properties\": {\n \"someStringProperty\": \"someStringValue\",\n \"someDirectRelation\": {\n \"space\": \"mySpace\",\n \"externalId\": \"someNode\"\n },\n \"someIntArrayProperty\": [\n 1,\n 2,\n 3,\n 4\n ]\n }\n }\n ]\n }\n ],\n \"delete\": [\n {\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"existingVersion\": 123\n }\n ],\n \"autoCreateDirectRelations\": true,\n \"autoCreateStartNodes\": false,\n \"autoCreateEndNodes\": false,\n \"skipOnVersionConflict\": false,\n \"replace\": false\n}"
response = http.request(request)
puts response.read_body{
"items": [
{
"instanceType": "node",
"version": 123,
"wasModified": true,
"space": "<string>",
"externalId": "<string>",
"createdTime": 1730204346000,
"lastUpdatedTime": 1730204346000
}
],
"deleted": [
{
"instanceType": "node",
"externalId": "<string>",
"space": "<string>"
}
]
}{
"error": {
"code": 401,
"message": "Could not authenticate.",
"missing": [
{}
],
"duplicated": [
{}
]
}
}{
"error": {
"code": 409,
"message": "<string>",
"isAutoRetryable": true
}
}Authorizations
Access token issued by the CDF project's configured identity provider. Access token must be an OpenID Connect token, and the project must be configured to accept OpenID Connect tokens. Use a header key of 'Authorization' with a value of 'Bearer $accesstoken'. The token can be obtained through any flow supported by the identity provider.
Scopes
Body
Nodes/edges to add or update.
Should we create missing target nodes of direct relations? If the target-container constraint has been specified for a direct relation, the target node cannot be auto-created. If you want to point direct relations to a space where you have only read access, this option must be set to false.
If existingVersion is specified on any of the nodes/edges in the input, the default behaviour is that the entire ingestion will fail when version conflicts occur. If skipOnVersionConflict is set to true, items with version conflicts will be skipped instead. If no version is specified for nodes/edges, it will do the write directly.
How do we behave when a property value exists? Do we replace all matching and existing values with the supplied values (true)? Or should we merge in new values for properties together with the existing values (false)? Note: This setting applies for all nodes or edges specified in the ingestion call.
from cognite.client.data_classes.data_modeling import EdgeApply, NodeOrEdgeData, NodeApply
node = NodeApply("mySpace", "myNodeId")
res = client.data_modeling.instances.apply(node)
from cognite.client.data_classes.data_modeling import EdgeApply, NodeOrEdgeData, NodeApply, ViewId
actor = NodeApply(
space="actors",
external_id="arnold_schwarzenegger",
sources=[
NodeOrEdgeData(
ViewId("mySpace", "PersonView", "v1"),
{"name": "Arnold Schwarzenegger", "birthYear": 1947}
),
NodeOrEdgeData(
ViewId("mySpace", "ActorView", "v1"),
{"wonOscar": False}
)
]
)
movie = NodeApply(
space="movies",
external_id="Terminator",
sources=[
NodeOrEdgeData(
ViewId("mySpace", "MovieView", "v1"),
{"title": "Terminator", "releaseYear": 1984}
)
]
)
# This is one-to-many edge, in this case from a person to a movie
actor_to_movie = EdgeApply(
space="actors",
external_id="relation:arnold_schwarzenegger:terminator",
type=("types", "acts-in"),
start_node=("actors", "arnold_schwarzenegger"),
end_node=("movies", "Terminator"),
)
res = client.data_modeling.instances.apply([actor, movie], [actor_to_movie])
from cognite.client.data_classes.data_modeling import EdgeApply
actor_to_movie = EdgeApply(
space="actors",
external_id="relation:arnold_schwarzenegger:terminator",
type=("types", "acts-in"),
start_node=("actors", "arnold_schwarzenegger"),
end_node=("movies", "Terminator"),
)
res = client.data_modeling.instances.apply(
edges=actor_to_movie,
auto_create_start_nodes=True,
auto_create_end_nodes=True
)
from cognite.client.utils import datetime_to_ms_iso_timestamp
from datetime import datetime, timezone
my_date = datetime(2020, 3, 14, 15, 9, 26, 535000, tzinfo=timezone.utc)
data_model_timestamp = datetime_to_ms_iso_timestamp(my_date) # "2020-03-14T15:09:26.535+00:00"
from cognite.client.data_classes.data_modeling import TypedNodeApply, PropertyOptions
class PersonApply(TypedNodeApply):
birth_year = PropertyOptions(identifier="birthYear")
def __init__(self, space: str, external_id, name: str, birth_year: int):
super().__init__(space, external_id, type=("sp_model_space", "Person"))
self.name = name
self.birth_year = birth_year
def get_source(self):
return ViewId("sp_model_space", "Person", "v1")
person = PersonApply("sp_date_space", "my_person", "John Doe", 1980)
res = client.data_modeling.instances.apply(nodes=person)curl --request POST \
--url https://{cluster}.cognitedata.com/api/v1/projects/{project}/models/instances \
--header 'Authorization: Bearer <token>' \
--header 'Content-Type: application/json' \
--data '
{
"items": [
{
"instanceType": "node",
"space": "<string>",
"externalId": "<string>",
"existingVersion": 123,
"sources": [
{
"source": {
"type": "view",
"space": "<string>",
"externalId": "<string>",
"version": "<string>"
},
"properties": {
"someStringProperty": "someStringValue",
"someDirectRelation": {
"space": "mySpace",
"externalId": "someNode"
},
"someIntArrayProperty": [
1,
2,
3,
4
]
}
}
]
}
],
"delete": [
{
"space": "<string>",
"externalId": "<string>",
"existingVersion": 123
}
],
"autoCreateDirectRelations": true,
"autoCreateStartNodes": false,
"autoCreateEndNodes": false,
"skipOnVersionConflict": false,
"replace": false
}
'const options = {
method: 'POST',
headers: {Authorization: 'Bearer <token>', 'Content-Type': 'application/json'},
body: JSON.stringify({
items: [
{
instanceType: 'node',
space: '<string>',
externalId: '<string>',
existingVersion: 123,
sources: [
{
source: {type: 'view', space: '<string>', externalId: '<string>', version: '<string>'},
properties: {
someStringProperty: 'someStringValue',
someDirectRelation: {space: 'mySpace', externalId: 'someNode'},
someIntArrayProperty: [1, 2, 3, 4]
}
}
]
}
],
delete: [{space: '<string>', externalId: '<string>', existingVersion: 123}],
autoCreateDirectRelations: true,
autoCreateStartNodes: false,
autoCreateEndNodes: false,
skipOnVersionConflict: false,
replace: false
})
};
fetch('https://{cluster}.cognitedata.com/api/v1/projects/{project}/models/instances', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://{cluster}.cognitedata.com/api/v1/projects/{project}/models/instances",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'items' => [
[
'instanceType' => 'node',
'space' => '<string>',
'externalId' => '<string>',
'existingVersion' => 123,
'sources' => [
[
'source' => [
'type' => 'view',
'space' => '<string>',
'externalId' => '<string>',
'version' => '<string>'
],
'properties' => [
'someStringProperty' => 'someStringValue',
'someDirectRelation' => [
'space' => 'mySpace',
'externalId' => 'someNode'
],
'someIntArrayProperty' => [
1,
2,
3,
4
]
]
]
]
]
],
'delete' => [
[
'space' => '<string>',
'externalId' => '<string>',
'existingVersion' => 123
]
],
'autoCreateDirectRelations' => true,
'autoCreateStartNodes' => false,
'autoCreateEndNodes' => false,
'skipOnVersionConflict' => false,
'replace' => false
]),
CURLOPT_HTTPHEADER => [
"Authorization: Bearer <token>",
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://{cluster}.cognitedata.com/api/v1/projects/{project}/models/instances"
payload := strings.NewReader("{\n \"items\": [\n {\n \"instanceType\": \"node\",\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"existingVersion\": 123,\n \"sources\": [\n {\n \"source\": {\n \"type\": \"view\",\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"version\": \"<string>\"\n },\n \"properties\": {\n \"someStringProperty\": \"someStringValue\",\n \"someDirectRelation\": {\n \"space\": \"mySpace\",\n \"externalId\": \"someNode\"\n },\n \"someIntArrayProperty\": [\n 1,\n 2,\n 3,\n 4\n ]\n }\n }\n ]\n }\n ],\n \"delete\": [\n {\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"existingVersion\": 123\n }\n ],\n \"autoCreateDirectRelations\": true,\n \"autoCreateStartNodes\": false,\n \"autoCreateEndNodes\": false,\n \"skipOnVersionConflict\": false,\n \"replace\": false\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("Authorization", "Bearer <token>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://{cluster}.cognitedata.com/api/v1/projects/{project}/models/instances")
.header("Authorization", "Bearer <token>")
.header("Content-Type", "application/json")
.body("{\n \"items\": [\n {\n \"instanceType\": \"node\",\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"existingVersion\": 123,\n \"sources\": [\n {\n \"source\": {\n \"type\": \"view\",\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"version\": \"<string>\"\n },\n \"properties\": {\n \"someStringProperty\": \"someStringValue\",\n \"someDirectRelation\": {\n \"space\": \"mySpace\",\n \"externalId\": \"someNode\"\n },\n \"someIntArrayProperty\": [\n 1,\n 2,\n 3,\n 4\n ]\n }\n }\n ]\n }\n ],\n \"delete\": [\n {\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"existingVersion\": 123\n }\n ],\n \"autoCreateDirectRelations\": true,\n \"autoCreateStartNodes\": false,\n \"autoCreateEndNodes\": false,\n \"skipOnVersionConflict\": false,\n \"replace\": false\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://{cluster}.cognitedata.com/api/v1/projects/{project}/models/instances")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Authorization"] = 'Bearer <token>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"items\": [\n {\n \"instanceType\": \"node\",\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"existingVersion\": 123,\n \"sources\": [\n {\n \"source\": {\n \"type\": \"view\",\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"version\": \"<string>\"\n },\n \"properties\": {\n \"someStringProperty\": \"someStringValue\",\n \"someDirectRelation\": {\n \"space\": \"mySpace\",\n \"externalId\": \"someNode\"\n },\n \"someIntArrayProperty\": [\n 1,\n 2,\n 3,\n 4\n ]\n }\n }\n ]\n }\n ],\n \"delete\": [\n {\n \"space\": \"<string>\",\n \"externalId\": \"<string>\",\n \"existingVersion\": 123\n }\n ],\n \"autoCreateDirectRelations\": true,\n \"autoCreateStartNodes\": false,\n \"autoCreateEndNodes\": false,\n \"skipOnVersionConflict\": false,\n \"replace\": false\n}"
response = http.request(request)
puts response.read_body{
"items": [
{
"instanceType": "node",
"version": 123,
"wasModified": true,
"space": "<string>",
"externalId": "<string>",
"createdTime": 1730204346000,
"lastUpdatedTime": 1730204346000
}
],
"deleted": [
{
"instanceType": "node",
"externalId": "<string>",
"space": "<string>"
}
]
}{
"error": {
"code": 401,
"message": "Could not authenticate.",
"missing": [
{}
],
"duplicated": [
{}
]
}
}{
"error": {
"code": 409,
"message": "<string>",
"isAutoRetryable": true
}
}