# ContX IQ v2: Read a Whole Relationship with Its Endpoints

> A CIQ v2 (2.0-ciq) read that returns a whole relationship: it comes back as its two endpoints, each with its external_id and type, and its properties. The example captures a three-node graph with a property on OWNS, saves the 2.0-ciq policy and the query, and executes as the owner.

**Category:** ContX IQ  
**API:** ContX IQ  
**Tags:** ContX IQ, CIQ v2, 2.0-ciq, CIQ Policy, Knowledge Query, Relationship
**Last Updated:** 2026-11-11
**OpenAPI Endpoints:** /capture/v1/nodes, /capture/v1/relationships, /configs/v1/authorization-policies, /configs/v1/knowledge-queries, /contx-iq/v1/execute
**Related Guides:** /guides/guide-contx-iq, /guides/guide-ciq-cypher

## Summary

When a Knowledge Query on a 2.0-ciq policy asks for a whole relationship (its variable alone, such as "owns"), the relationship comes back by its endpoints:

- _startNode_external_id and _startNode_type: the node the relationship starts from.
- _endNode_external_id and _endNode_type: the node it points to.
- Props: the relationship's id (a GID), its timestamps and its own properties.

So a client reads who is linked to what straight from the answer, in the same identifiers it uses everywhere else (external_id and type), without asking for the endpoint nodes separately. A variable-length relationship comes back as a list of these objects, one per hop.

Other ways to read a relationship:
- "owns.since" returns the one property, and "owns.*" all of them.
- A filter attribute on a 2.0-ciq policy carries the @ prefix, as in "@subject.external_id".

## Use Case

Scenario: a fleet dashboard lists, for the signed-in person, the buses they own and since when. It needs the ownership itself, not only the buses: who owns, what is owned, and the since property of OWNS.

Graph:
- Person(karel), an identity node
- Bus(harmonika)
- LicenseNumber(ln-yyy)
- karel -[OWNS {since: 2024-05-01}]-> harmonika -[HAS]-> ln-yyy

Walk:
1. Capture the three nodes, and the relationships with since on OWNS.
2. Save the 2.0-ciq policy: a Person reads the buses they own and the OWNS relationship, bound to the caller through $token.sub.
3. Save the Knowledge Query: bus.external_id and the whole owns relationship.
4. Execute as karel: one record, with owns as karel (Person) to harmonika (Bus) and its properties.

## Requirements

Prerequisites:
- ServiceAccount credentials: For creating the policy and the Knowledge Query.
- AppAgent credentials: For the Capture calls and the execute (X-IK-ClientKey). The agent must hold Capture and ContXIQ.
- A user access token whose sub is "karel", validated by a Token Introspect configuration of the project (the policy binds the subject through $token.sub).

Required API access:
- POST /capture/v1/nodes, POST /capture/v1/relationships
- POST /configs/v1/authorization-policies
- POST /configs/v1/knowledge-queries
- POST /contx-iq/v1/execute

## Steps

Step 1: Capture the graph
- Authentication: AppAgent credential.
- Action: POST the three nodes, then the OWNS relationship with its since property and the HAS relationship.

Step 2: Create the 2.0-ciq policy
- Authentication: ServiceAccount credential.
- Action: POST the policy. Its condition matches the buses the caller owns; the filter binds @subject.external_id to $token.sub; allowed_reads covers bus.external_id and the owns relationship.
- Result: the policy ID.

Step 3: Create the Knowledge Query
- Action: POST the query with policy_id set to the policy from step 2: nodes bus.external_id, relationships owns.
- Result: the query ID.

Step 4: Execute as the owner
- Authentication: AppAgent credential plus Authorization: Bearer with karel's token.
- Action: POST /contx-iq/v1/execute with the query name and empty input_params.
- Result: 200 with one record: bus.external_id "harmonika", and owns as {_startNode_external_id "karel", _startNode_type "Person", _endNode_external_id "harmonika", _endNode_type "Bus", Props with id, timestamps and since}.

## Graph

The graph the example reads: karel OWNS the bus harmonika since 2024-05-01, and harmonika HAS the licence number ln-yyy. Hover the OWNS relationship for its property.

- Person `karel` (identity): email=karel@demo.com, name=Karel Plihal
- Bus `harmonika`: manufacturer=Ikarus, model=280
- LicenseNumber `ln-yyy`: value=ln-yyy-value
- `karel` -[OWNS]-> `harmonika`: since=2024-05-01
- `harmonika` -[HAS]-> `ln-yyy`

## Code Examples

### Step 1

Capture the owner, the bus and its licence number.

**POST https://eu.api.indykite.com/capture/v1/nodes**

```json
{
  "nodes": [
    {
      "external_id": "karel",
      "type": "Person",
      "is_identity": true,
      "properties": [
        {
          "type": "email",
          "value": "karel@demo.com"
        },
        {
          "type": "name",
          "value": "Karel Plihal"
        }
      ]
    },
    {
      "external_id": "harmonika",
      "type": "Bus",
      "properties": [
        {
          "type": "manufacturer",
          "value": "Ikarus"
        },
        {
          "type": "model",
          "value": "280"
        }
      ]
    },
    {
      "external_id": "ln-yyy",
      "type": "LicenseNumber",
      "properties": [
        {
          "type": "value",
          "value": "ln-yyy-value"
        }
      ]
    }
  ]
}
```


karel OWNS harmonika since 2024-05-01; harmonika HAS ln-yyy.

**POST https://eu.api.indykite.com/capture/v1/relationships**

```json
{
  "relationships": [
    {
      "source": {
        "external_id": "karel",
        "type": "Person"
      },
      "target": {
        "external_id": "harmonika",
        "type": "Bus"
      },
      "type": "OWNS",
      "properties": [
        {
          "type": "since",
          "value": "2024-05-01"
        }
      ]
    },
    {
      "source": {
        "external_id": "harmonika",
        "type": "Bus"
      },
      "target": {
        "external_id": "ln-yyy",
        "type": "LicenseNumber"
      },
      "type": "HAS"
    }
  ]
}
```

### Step 2

The 2.0-ciq policy. The filter attribute carries the @ prefix that CIQ v2 requires; owns is listed whole in allowed_reads.relationships.

**policy.json**

```json
{
  "meta": {
    "policy_version": "2.0-ciq"
  },
  "subject": {
    "type": "Person"
  },
  "condition": {
    "cypher": "MATCH (subject:Person)-[owns:OWNS]->(bus:Bus)",
    "filter": [
      {
        "attribute": "@subject.external_id",
        "operator": "=",
        "value": "$token.sub"
      }
    ]
  },
  "allowed_reads": {
    "nodes": [
      "bus.external_id"
    ],
    "relationships": [
      "owns"
    ]
  }
}
```


Request to create the policy; the document is sent as a string in the policy field.

**POST https://eu.api.indykite.com/configs/v1/authorization-policies**

```json
{
  "project_id": "your_project_gid",
  "name": "ciq-v2-policy-own-buses-ownership",
  "display_name": "CIQ v2 policy - own buses with the ownership relationship",
  "description": "Read the buses the caller owns and the OWNS relationship that links the caller to each.",
  "policy": "{\"meta\":{\"policy_version\":\"2.0-ciq\"},\"subject\":{\"type\":\"Person\"},\"condition\":{\"cypher\":\"MATCH (subject:Person)-[owns:OWNS]->(bus:Bus)\",\"filter\":[{\"attribute\":\"@subject.external_id\",\"operator\":\"=\",\"value\":\"$token.sub\"}]},\"allowed_reads\":{\"nodes\":[\"bus.external_id\"],\"relationships\":[\"owns\"]}}",
  "status": "ACTIVE",
  "tags": []
}
```

### Step 3

The Knowledge Query: the bus's external_id and the whole owns relationship.

**knowledge_query.json**

```json
{
  "nodes": [
    "bus.external_id"
  ],
  "relationships": [
    "owns"
  ]
}
```


Request to create the Knowledge Query.

**POST https://eu.api.indykite.com/configs/v1/knowledge-queries**

```json
{
  "project_id": "your_project_gid",
  "name": "kq-own-buses-with-ownership",
  "display_name": "Knowledge Query - own buses with the ownership relationship",
  "description": "Returns the buses the caller owns, each with the whole OWNS relationship: its two endpoints and its properties.",
  "policy_id": "your_policy_gid",
  "query": "{\"nodes\":[\"bus.external_id\"],\"relationships\":[\"owns\"]}",
  "status": "ACTIVE"
}
```

### Step 4

Execute as karel: X-IK-ClientKey plus Authorization: Bearer <karel's token>. No input parameters; the subject comes from the token.

**POST https://eu.api.indykite.com/contx-iq/v1/execute**

```json
{
  "id": "kq-own-buses-with-ownership",
  "input_params": {}
}
```


One record: owns named by its endpoints, karel (Person) to harmonika (Bus), with its id as a GID, its timestamps and since in Props.

**Response 200**

```json
{
  "data": [
    {
      "nodes": {
        "bus.external_id": "harmonika"
      },
      "relationships": {
        "owns": {
          "_startNode_external_id": "karel",
          "_startNode_type": "Person",
          "_endNode_external_id": "harmonika",
          "_endNode_type": "Bus",
          "Props": {
            "id": "gid:AAAAF...",
            "create_time": "2026-11-11T09:12:04.731Z",
            "update_time": "2026-11-11T09:12:04.731Z",
            "since": "2024-05-01"
          }
        }
      }
    }
  ]
}
```

## Common Errors

### 400: requested variable owns is not in the allowed_reads.relationships list

**Solution:** The query asks for a relationship the policy does not let it read. List the relationship variable in allowed_reads.relationships (owns for the whole relationship, owns.* or owns.<prop> for its properties).

### 200: {"data": []}

**Solution:** A 2.0-ciq filter attribute without the @ prefix ("subject.external_id") is a literal string and matches nothing. Write "@subject.external_id". Also check that the token's sub is the external_id of a Person who OWNS a Bus, with GET /contx-iq/v1/whoami.

---

Source: https://developer.indykite.com/resources/ciq-27