Skip to main content
POST
/
relationships
JavaScript SDK
const relationships = [
  {
     externalId: 'some_relationship',
     sourceExternalId: 'some_source_external_id',
     sourceType: 'asset' as const,
     targetExternalId: 'some_target_external_id',
     targetType: 'event' as const
  }
];
const createdRelationships = await client.relationships.create(relationships);
from cognite.client.data_classes import Relationship
flowrel1 = Relationship(
    external_id="flow_1",
    source_external_id="source_ext_id",
    source_type="asset",
    target_external_id="target_ext_id",
    target_type="event",
    confidence=0.1,
    data_set_id=1234
)
flowrel2 = Relationship(
    external_id="flow_2",
    source_external_id="source_ext_id",
    source_type="asset",
    target_external_id="target_ext_id",
    target_type="event",
    confidence=0.1,
    data_set_id=1234
)
res = client.relationships.create([flowrel1,flowrel2])
List<Asset> assetUpsertList = //list of Asset; 
List<Event> eventUpsertList = //list of Event; 
List<TimeseriesMetadata> timseriesUpsertList = //list of TimeseriesMetadata; 
List<SequenceMetadata> sequenceUpsertList = // list of SequenceMetadata; 

List<Relationship> relationshipList = new ArrayList<>(); 

//EVENT AND ASSET 
relationshipList.add( 
     Relationship.newBuilder() 
          .setStartTime(Instant.now().toEpochMilli()) 
          .setEndTime(Instant.now().toEpochMilli()) 
          .setSourceType(Relationship.ResourceType.EVENT) 
          .setSourceExternalId(eventUpsertList.get(0).getExternalId()) 
          .setTargetType(Relationship.ResourceType.ASSET) 
          .setTargetExternalId(assetUpsertList.get(0).getExternalId()) 
          .setConfidence(ThreadLocalRandom.current().nextFloat()) 
          .build()); 

//TIME_SERIES AND ASSET 
relationshipList.add( 
     Relationship.newBuilder() 
          .setStartTime(Instant.now().toEpochMilli()) 
          .setEndTime(Instant.now().toEpochMilli()) 
          .setSourceType(Relationship.ResourceType.TIME_SERIES) 
          .setSourceExternalId(timseriesUpsertList.get(1).getExternalId()) 
          .setTargetType(Relationship.ResourceType.ASSET) 
          .setTargetExternalId(assetUpsertList.get(1).getExternalId()) 
          .setConfidence(ThreadLocalRandom.current().nextFloat()) 
          .build()); 

//SEQUENCE AND EVENT 
relationshipList.add( 
     Relationship.newBuilder() 
          .setStartTime(Instant.now().toEpochMilli()) 
          .setEndTime(Instant.now().toEpochMilli()) 
          .setSourceType(Relationship.ResourceType.SEQUENCE) 
          .setSourceExternalId(sequenceUpsertList.get(2).getExternalId()) 
          .setTargetType(Relationship.ResourceType.EVENT) 
          .setTargetExternalId(eventUpsertList.get(2).getExternalId()) 
          .setConfidence(ThreadLocalRandom.current().nextFloat()) 
          .build()); 

List<Relationship> upsertRelationshipsList = client.relationships() 
          .upsert(relationshipList); 

curl --request POST \
  --url https://{cluster}.cognitedata.com/api/v1/projects/{project}/relationships \
  --header 'Authorization: Bearer <token>' \
  --header 'Content-Type: application/json' \
  --data '
{
  "items": [
    {
      "externalId": "<string>",
      "sourceExternalId": "<string>",
      "targetExternalId": "<string>",
      "startTime": 1730204346000,
      "endTime": 1730204346000,
      "confidence": 0.5,
      "dataSetId": 4503599627370496,
      "labels": [
        {
          "externalId": "my.known.id"
        }
      ]
    }
  ]
}
'
<?php

$curl = curl_init();

curl_setopt_array($curl, [
  CURLOPT_URL => "https://{cluster}.cognitedata.com/api/v1/projects/{project}/relationships",
  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' => [
        [
                'externalId' => '<string>',
                'sourceExternalId' => '<string>',
                'targetExternalId' => '<string>',
                'startTime' => 1730204346000,
                'endTime' => 1730204346000,
                'confidence' => 0.5,
                'dataSetId' => 4503599627370496,
                'labels' => [
                                [
                                                                'externalId' => 'my.known.id'
                                ]
                ]
        ]
    ]
  ]),
  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}/relationships"

	payload := strings.NewReader("{\n  \"items\": [\n    {\n      \"externalId\": \"<string>\",\n      \"sourceExternalId\": \"<string>\",\n      \"targetExternalId\": \"<string>\",\n      \"startTime\": 1730204346000,\n      \"endTime\": 1730204346000,\n      \"confidence\": 0.5,\n      \"dataSetId\": 4503599627370496,\n      \"labels\": [\n        {\n          \"externalId\": \"my.known.id\"\n        }\n      ]\n    }\n  ]\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))

}
require 'uri'
require 'net/http'

url = URI("https://{cluster}.cognitedata.com/api/v1/projects/{project}/relationships")

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      \"externalId\": \"<string>\",\n      \"sourceExternalId\": \"<string>\",\n      \"targetExternalId\": \"<string>\",\n      \"startTime\": 1730204346000,\n      \"endTime\": 1730204346000,\n      \"confidence\": 0.5,\n      \"dataSetId\": 4503599627370496,\n      \"labels\": [\n        {\n          \"externalId\": \"my.known.id\"\n        }\n      ]\n    }\n  ]\n}"

response = http.request(request)
puts response.read_body
{
  "items": [
    {
      "externalId": "<string>",
      "sourceExternalId": "<string>",
      "targetExternalId": "<string>",
      "createdTime": 1730204346000,
      "lastUpdatedTime": 1730204346000,
      "startTime": 1730204346000,
      "endTime": 1730204346000,
      "confidence": 0.5,
      "dataSetId": 4503599627370496,
      "labels": [
        {
          "externalId": "my.known.id"
        }
      ]
    }
  ]
}
{
  "error": {
    "code": 409,
    "message": "Could not authenticate.",
    "missing": [
      {}
    ],
    "duplicated": [
      {}
    ]
  }
}
{
  "error": {
    "code": 409,
    "message": "Could not authenticate.",
    "missing": [
      {}
    ],
    "duplicated": [
      {}
    ]
  }
}
{
  "error": {
    "code": 409,
    "message": "Could not authenticate.",
    "missing": [
      {}
    ],
    "duplicated": [
      {}
    ]
  }
}

Authorizations

Authorization
string
header
required

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.

Body

application/json

Data required to create relationships. You can request a maximum of 1000 relationships per request.

items
object[]
required
Required array length: 1 - 1000 elements

Response

Relationships stored by CDF.

items
object[]
required
Required array length: 1 - 1000 elements
Last modified on July 13, 2026