> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cognite.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Cognite Data Fusion (CDF) is an industrial DataOps platform. Prefer English docs unless the user asks for another locale.
> Follow /_llms/ index links recursively until you reach page URLs ending in .md. Fetch those Markdown twins instead of HTML.
> For REST APIs, default to calendar version 20230101 (stable). Use 20230101-beta or 20230101-alpha only if the user is on a preview API. Cite paths under /api-reference/concepts/<version>/ and the Cdf-Version header. Prefer the Python SDK (cognite-sdk) unless the user is in JavaScript/TypeScript.
> Start here by task: platform overview /cdf/index.md; data modeling /cdf/dm/index.md; data integration /cdf/integration/index.md; access /cdf/access/index.md; CDF Toolkit /cdf/deploy/cdf_toolkit/index.md; REST quickstart /dev/quickstart.md; Python SDK https://cognite-sdk-python.readthedocs-hosted.com/en/stable/; Atlas AI /cdf/atlas_ai/concepts/index.md; Flows apps /cdf/flows/index.md; MCP and IDE setup /dev/guides/ide_ai_integration.md.
> For implementation work, also load /skill.md.

# Create relationships

> Creates up to 1000 relationships. `externalId` must be unique. Requires `relationshipsAcl:WRITE`. See [Create and update](/api-reference/concepts/20230101/relationships#create-and-update).



## OpenAPI

````yaml https://storage.googleapis.com/cognitedata-api-docs/dist/20230101.json post /relationships
openapi: 3.1.0
info:
  title: Cognite API
  description: >-
    Reference documentation for the Cognite Data Fusion (CDF) API. See the [API
    introduction](/api-reference/concepts/20230101/api-description) and [API
    changelog](/api-reference/changelog).
  version: v1
  contact:
    name: Cognite Support
    url: https://support.cognite.com
    email: support@cognite.com
servers:
  - url: https://{cluster}.cognitedata.com/api/v1/projects/{project}
    description: The URL for the CDF cluster to connect to
    variables:
      cluster:
        enum:
          - api
          - az-tyo-gp-001
          - az-eastus-1
          - az-power-no-northeurope
          - westeurope-1
          - asia-northeast1-1
          - gc-dsm-gp-001
        default: api
        description: The CDF cluster to connect to
      project:
        default: publicdata
        description: The CDF project name.
security:
  - oidc-token:
      - https://{cluster}.cognitedata.com/.default
  - oauth2-client-credentials:
      - https://{cluster}.cognitedata.com/.default
  - oauth2-open-industrial-data:
      - https://api.cognitedata.com/.default
  - oauth2-auth-code:
      - https://{cluster}.cognitedata.com/.default
tags:
  - name: Token
  - name: Assets
  - name: Data Modeling
    x-uri-format-fragments: []
  - name: Streams
  - name: Records
  - name: Events
  - name: Files
  - name: Functions
  - name: Function calls
  - name: Function schedules
  - name: Function Apps
  - name: Function App Calls
  - name: Function App MCP
  - name: 3D
  - name: Time series
  - name: Data point subscriptions
  - name: Synthetic Time Series
  - name: Raw
    x-uri-format-fragments: []
  - name: Groups
  - name: Projects
  - name: Security categories
  - name: Data sets
  - name: Sequences
  - name: Labels
  - name: Relationships
  - name: Entity matching
  - name: Vision
  - name: Cognite AI
  - name: Documents
  - name: Document preview
    x-displayName: Preview
  - name: Geospatial
  - name: SessionsInternal
  - name: Sessions
  - name: Extraction Pipelines
  - name: Extraction Pipelines Runs
  - name: Extraction Pipelines Config
  - name: Extractors
  - name: Transformations
  - name: Transformation Jobs
  - name: Transformation Schedules
  - name: Transformation Notifications
  - name: Query
  - name: Schema
  - name: Transformation External Data Sources
  - name: Annotations
  - name: User profiles
  - name: Workflows
  - name: Workflow versions
  - name: Workflow executions
  - name: Tasks
  - name: Workflow triggers
  - name: Sources
  - name: Jobs
  - name: Destinations
  - name: Mappings
  - name: Previews
  - name: Postgres Gateway Users
  - name: Postgres Gateway Tables
  - name: SAP Instances
  - name: SAP Endpoints
  - name: Schema Mappings
  - name: Writeback Requests
  - name: Simulators
  - name: Simulator Integrations
  - name: Simulator Routines
  - name: Simulator Models
  - name: Simulation Runs
  - name: Simulator Logs
  - name: Units
  - name: Unit Systems
  - name: Application Data Storage
  - name: Location Filters
  - name: User Preferences
  - name: User History
  - name: Search Config
  - name: Organizations
  - name: Applications
  - name: Integrations
  - name: Integration Tasks
  - name: Integration Actions
  - name: Integration Configuration
  - name: Integration Errors
  - name: Principals
  - name: Signals
  - name: Data products
paths:
  /relationships:
    post:
      tags:
        - Relationships
      summary: Create relationships
      description: >-
        Creates up to 1000 relationships. `externalId` must be unique. Requires
        `relationshipsAcl:WRITE`. See [Create and
        update](/api-reference/concepts/20230101/relationships#create-and-update).
      operationId: createRelationships
      requestBody:
        $ref: '#/components/requestBodies/newRelationship'
      responses:
        '201':
          $ref: '#/components/responses/persistedRelationships'
        '400':
          $ref: '#/components/responses/400GeneralError'
        '409':
          $ref: '#/components/responses/409GeneralError'
        '500':
          $ref: '#/components/responses/generalError'
      deprecated: true
      x-code-samples:
        - lang: JavaScript
          label: JavaScript SDK
          source: >-
            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);
        - lang: Python
          label: Python SDK
          source: |
            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])
        - lang: Java
          label: Java SDK
          source: >+
            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); 

components:
  requestBodies:
    newRelationship:
      description: >-
        Data required to create relationships. You can request a maximum of 1000
        relationships per request.
      required: true
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/relationshipRequestWrapper'
  responses:
    persistedRelationships:
      description: Relationships stored by CDF.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/relationshipResponseWrapper'
    400GeneralError:
      description: >-
        Bad request error. Typically issued if relationships in a create request
        have illegal/unsupported values.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/generalErrorWrapper'
    409GeneralError:
      description: >-
        Conflict error. Typically issued if external IDs on any relationship
        already exists, or if there are duplicate external IDs in the request
        itself.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/generalErrorWrapper'
    generalError:
      description: General error object.
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/generalErrorWrapper'
  schemas:
    relationshipRequestWrapper:
      type: object
      required:
        - items
      properties:
        items:
          type: array
          minItems: 1
          maxItems: 1000
          items:
            $ref: '#/components/schemas/relationship'
    relationshipResponseWrapper:
      type: object
      required:
        - items
      properties:
        items:
          minItems: 1
          maxItems: 1000
          type: array
          items:
            $ref: '#/components/schemas/relationshipResponse'
    generalErrorWrapper:
      type: object
      required:
        - error
      description: Error wrapper Error message
      properties:
        error:
          $ref: '#/components/schemas/generalError'
    relationship:
      description: >-
        The representation of a relationship consists of a source and a target
        and additional parameters.
      type: object
      required:
        - externalId
        - sourceExternalId
        - sourceType
        - targetExternalId
        - targetType
      properties:
        externalId:
          description: External ID of the relationship, must be unique within the project.
          allOf:
            - $ref: '#/components/schemas/relationshipExternalId'
        sourceExternalId:
          $ref: '#/components/schemas/sourceExternalId'
        sourceType:
          $ref: '#/components/schemas/sourceType'
        targetExternalId:
          $ref: '#/components/schemas/targetExternalId'
        targetType:
          $ref: '#/components/schemas/targetType'
        startTime:
          $ref: '#/components/schemas/startTime'
        endTime:
          $ref: '#/components/schemas/endTime'
        confidence:
          $ref: '#/components/schemas/confidence'
        dataSetId:
          $ref: '#/components/schemas/dataSetId'
        labels:
          $ref: '#/components/schemas/LabelList'
    relationshipResponse:
      type: object
      allOf:
        - $ref: '#/components/schemas/relationship'
        - $ref: '#/components/schemas/persistedObject'
    generalError:
      type: object
      required:
        - code
        - message
      description: Cognite API error
      properties:
        code:
          type: integer
          description: HTTP status code
          format: int32
          example: 409
        message:
          type: string
          description: Error message
          example: Could not authenticate.
        missing:
          type: array
          description: List of lookup objects that do not match any results.
          items:
            type: object
            additionalProperties: true
        duplicated:
          type: array
          description: List of objects that are not unique.
          items:
            type: object
            additionalProperties: true
    relationshipExternalId:
      type: string
      minLength: 1
      maxLength: 255
      description: The external ID of the relationship.
    sourceExternalId:
      description: The external ID of the resource that is the relationship source.
      allOf:
        - $ref: '#/components/schemas/resourceExternalId'
    sourceType:
      description: >-
        The resource type of the relationship source. Must be one of the
        specified values.
      allOf:
        - $ref: '#/components/schemas/resourceType'
    targetExternalId:
      description: The external ID of the resource that is the relationship target.
      allOf:
        - $ref: '#/components/schemas/relationshipExternalId'
    targetType:
      description: >-
        The resource type of the relationship target. Must be one of the
        specified values.
      allOf:
        - $ref: '#/components/schemas/resourceType'
    startTime:
      description: >-
        The time, in milliseconds since Jan. 1, 1970, when the relationship
        became active. If there is no startTime, the relationship is active from
        the beginning of time until endTime.
      allOf:
        - $ref: '#/components/schemas/EpochTimestamp'
    endTime:
      description: >-
        The time, in milliseconds since Jan. 1, 1970, when the relationship
        became inactive. If there is no endTime, the relationship is active from
        startTime until the present or any point in the future. If endTime and
        startTime are set, the endTime must be greater than startTime.
      allOf:
        - $ref: '#/components/schemas/EpochTimestamp'
    confidence:
      description: >-
        Confidence value of the existence of the relationship. Generated
        relationships provide a score of the likelihood of the relationship
        existing. Relationships without a confidence value can be interpreted at
        the discretion of each project.
      type: number
      format: float
      minimum: 0
      maximum: 1
    dataSetId:
      description: The ID of the dataset the relationship belongs to.
      allOf:
        - $ref: '#/components/schemas/CogniteInternalId'
    LabelList:
      type: array
      description: A list of the labels associated with this resource item.
      minItems: 0
      maxItems: 10
      uniqueItems: true
      items:
        $ref: '#/components/schemas/Label'
    persistedObject:
      type: object
      properties:
        externalId:
          $ref: '#/components/schemas/relationshipExternalId'
        createdTime:
          description: >-
            The time, in milliseconds since Jan. 1, 1970, when the relationship
            was created.
          allOf:
            - $ref: '#/components/schemas/EpochTimestamp'
        lastUpdatedTime:
          description: >-
            The time, in milliseconds since Jan. 1, 1970, when the relationship
            was last updated.
          allOf:
            - $ref: '#/components/schemas/EpochTimestamp'
      required:
        - externalId
        - createdTime
        - lastUpdatedTime
    resourceExternalId:
      type: string
      minLength: 1
      maxLength: 255
    resourceType:
      type: string
      enum:
        - asset
        - timeSeries
        - file
        - event
        - sequence
    EpochTimestamp:
      description: >-
        The number of milliseconds since 00:00:00 Thursday, 1 January 1970,
        Coordinated Universal Time (UTC), minus leap seconds.
      type: integer
      minimum: 0
      format: int64
      example: 1730204346000
    CogniteInternalId:
      description: A server-generated ID for the object.
      type: integer
      minimum: 1
      maximum: 9007199254740991
      format: int64
    Label:
      type: object
      title: Label
      required:
        - externalId
      description: A label assigned to a resource.
      properties:
        externalId:
          description: An external ID to a predefined label definition.
          allOf:
            - $ref: '#/components/schemas/CogniteExternalId'
    CogniteExternalId:
      description: >-
        The external ID provided by the client. Must be unique for the resource
        type.
      type: string
      maxLength: 255
      example: my.known.id
  securitySchemes:
    oidc-token:
      type: http
      scheme: bearer
      bearerFormat: OpenID Connect or OAuth2 token
      description: >-
        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.
    oauth2-client-credentials:
      type: oauth2
      description: >-
        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.
      flows:
        clientCredentials:
          tokenUrl: https://your-idps.token.url/
          scopes:
            default: https://{cluster}.cognitedata.com/.default
    oauth2-open-industrial-data:
      type: oauth2
      description: >-
        Auth flow for Open Industrial Data. Get your client secret from
        https://hub.cognite.com/open-industrial-data-211.
      flows:
        clientCredentials:
          tokenUrl: >-
            https://login.microsoftonline.com/48d5043c-cf70-4c49-881c-c638f5796997/oauth2/v2.0/token
          scopes:
            default: https://api.cognitedata.com/.default
    oauth2-auth-code:
      type: oauth2
      description: >-
        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.
      flows:
        authorizationCode:
          authorizationUrl: https://your-idps.authorization.url/
          tokenUrl: https://your-idps.token.url/
          scopes:
            default: https://{cluster}.cognitedata.com/.default

````