# ContX IQ v2: Write Values Read from the Graph with @ References

> A CIQ v2 (2.0-ciq) write: one Knowledge Query copies the caller's email and a licence number value onto the bus the caller owns, with @ references in its upsert values, so the client sends no values at all. The example captures a three-node graph, saves the 2.0-ciq policy and the query, executes as the owner, and reads the written values back in the same call.

**Category:** ContX IQ  
**API:** ContX IQ  
**Tags:** ContX IQ, CIQ v2, 2.0-ciq, CIQ Policy, Knowledge Query, Upsert, @ reference
**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

A CIQ v2 policy ("policy_version": "2.0-ciq") lets a Knowledge Query take its upsert values from the graph itself. A property value written as "@<var>.property.<name>" is read, for each row the policy's condition matches, from a variable of the policy's Cypher, then written: the client sends no value and cannot send a wrong one.

What the example shows:
- An upsert whose two values are @ references: "@subject.property.email" and "@ln.property.value".
- The 2.0-ciq filter form: the attribute carries the @ prefix ("@subject.external_id"); the token reference ($token.sub) is unchanged.
- Reading the written values back in the same execute, through the query's nodes.

Rules for @ references (checked when the Knowledge Query is saved):
- Only on 2.0-ciq policies, and only in property values of upsert_nodes and upsert_relationships.
- The reference points at a variable of the policy's Cypher, not at a node the query creates: values are read before anything is written.
- A node needs a path: an attribute, property, metadata or trust score. A bare variable is allowed only for an aggregate alias.
- A token claim is written $token.<claim>, without an @ in front.
- A literal that starts with @ is escaped as \@...

## Use Case

Scenario: a fleet application lets each person stamp the buses they own with their contact email and the bus's licence number, so that the bus record carries them directly. The values already live in the graph, on the person and on the licence number node; the application should not read them first and send them back, and must not be able to write someone else's email.

Graph:
- Person(karel), an identity node with the email karel@demo.com
- Bus(harmonika)
- LicenseNumber(ln-yyy) with the value ln-yyy-value
- karel -[OWNS]-> harmonika -[HAS]-> ln-yyy

Walk:
1. Capture the three nodes and two relationships.
2. Save the 2.0-ciq policy: a Person may update the buses they own, bound to the caller through $token.sub.
3. Save the Knowledge Query: upsert owner_email = @subject.property.email and licence = @ln.property.value on bus, and return both.
4. Execute as karel, with no input parameters: one record with the two values, now stored on harmonika.

## 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 and HAS relationships (the same graph as resource ciq-25).

Step 2: Create the 2.0-ciq policy
- Authentication: ServiceAccount credential.
- Action: POST the policy. Its condition matches the buses the caller owns and their licence numbers; the filter binds @subject.external_id to $token.sub; allowed_upserts lists bus as an existing node; allowed_reads covers bus.*.
- Result: the policy ID.

Step 3: Create the Knowledge Query
- Action: POST the query with policy_id set to the policy from step 2. upsert_nodes updates bus with two properties whose values are @ references; nodes reads them back.
- Result: the query ID. A reference that breaks a rule is refused here, before anything runs.

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.property.owner_email "karel@demo.com" and bus.property.licence "ln-yyy-value", both now stored on harmonika.

## Graph Before

Before the write: karel OWNS the bus harmonika, which HAS the licence number ln-yyy. The bus has no owner_email or licence property yet.

- 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`
- `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; 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"
    },
    {
      "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; bus is the existing node a query may update.

**policy.json**

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


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-stamp-own-buses",
  "display_name": "CIQ v2 policy - stamp the buses the caller owns",
  "description": "Update the buses the caller owns, with values read from the caller and from each bus's licence number.",
  "policy": "{\"meta\":{\"policy_version\":\"2.0-ciq\"},\"subject\":{\"type\":\"Person\"},\"condition\":{\"cypher\":\"MATCH (subject:Person)-[owns:OWNS]->(bus:Bus)-[:HAS]->(ln:LicenseNumber)\",\"filter\":[{\"attribute\":\"@subject.external_id\",\"operator\":\"=\",\"value\":\"$token.sub\"}]},\"allowed_reads\":{\"nodes\":[\"bus.*\"]},\"allowed_upserts\":{\"nodes\":{\"existing_nodes\":[\"bus\"]}}}",
  "status": "ACTIVE",
  "tags": []
}
```

### Step 3

The Knowledge Query. Both upsert values are @ references to variables of the policy's Cypher: subject's email property and the licence number's value. nodes returns the two written properties.

**knowledge_query.json**

```json
{
  "nodes": [
    "bus.property.owner_email",
    "bus.property.licence"
  ],
  "upsert_nodes": [
    {
      "name": "bus",
      "properties": [
        {
          "type": "owner_email",
          "value": "@subject.property.email"
        },
        {
          "type": "licence",
          "value": "@ln.property.value"
        }
      ]
    }
  ]
}
```


Request to create the Knowledge Query.

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

```json
{
  "project_id": "your_project_gid",
  "name": "kq-stamp-own-buses",
  "display_name": "Knowledge Query - stamp the buses the caller owns",
  "description": "Writes onto each bus the caller owns the caller's email and the bus's licence number value, both read from the graph, and returns them.",
  "policy_id": "your_policy_gid",
  "query": "{\"nodes\":[\"bus.property.owner_email\",\"bus.property.licence\"],\"upsert_nodes\":[{\"name\":\"bus\",\"properties\":[{\"type\":\"owner_email\",\"value\":\"@subject.property.email\"},{\"type\":\"licence\",\"value\":\"@ln.property.value\"}]}]}",
  "status": "ACTIVE"
}
```

### Step 4

Execute as karel: X-IK-ClientKey plus Authorization: Bearer <karel's token>. No input parameters: every written value comes from the graph.

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

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


One record: the two values, read from karel and from ln-yyy, written onto harmonika and read back.

**Response 200**

```json
{
  "data": [
    {
      "nodes": {
        "bus.property.owner_email": "karel@demo.com",
        "bus.property.licence": "ln-yyy-value"
      }
    }
  ]
}
```

## Graph After

After the write: the bus harmonika carries owner_email, copied from karel's email, and licence, copied from the value of ln-yyy. Nothing was sent in input_params.

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

## Common Errors

### 400: upsert value @<ref> must reference a variable of the policy cypher

**Solution:** The reference names a variable the policy's Cypher does not define, or one the query itself creates. Reference a node, relationship or aggregate of the policy's condition; values are read before anything is written.

### 400: upsert value @<ref> must reference an attribute, a property, a trust score or an aggregate value

**Solution:** A bare node or relationship variable cannot be a value. Give a path, such as @subject.property.email or @subject.external_id.

### 400: upsert value @$token.<claim> references a token claim, use $token.<claim> instead

**Solution:** Token claims are not read from the graph: write the value as $token.<claim>, without the @.

### 424: a write query cannot update, delete or reference the variable-length relationship <name>

**Solution:** A 2.0-ciq write query may read a variable-length relationship such as -[r:HAS*1..2]-> but not write through it or reference it. Use a relationship of fixed length in the policy's Cypher.

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

**Solution:** The token's sub is not the external_id of a Person who OWNS a Bus with a licence number, so nothing matched and nothing was written. Check GET /contx-iq/v1/whoami for the subject the token resolves to.

---

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