{
  "id": "ciq-23",
  "title": "ContX IQ: Paginated Query Execution with page_token and page_size",
  "description": "Walk a large CIQ result set across multiple /contx-iq/v1/execute calls using the page_token and page_size request fields. Demonstrates client-side pagination of an authorized read query.",
  "category": "ContX IQ",
  "tags": [
    "ContX IQ Execution",
    "Pagination",
    "page_token",
    "page_size",
    "Large Result Sets"
  ],
  "api": "ContX IQ",
  "language": "Json",
  "lastUpdated": "2026-05-20",
  "openApiEndpoints": [
    "/capture/v1/nodes",
    "/capture/v1/relationships",
    "/configs/v1/authorization-policies",
    "/configs/v1/knowledge-queries",
    "/contx-iq/v1/execute"
  ],
  "relatedGuides": [
    "/guides/guide-contx-iq"
  ],
  "commonErrors": [
    {
      "code": "400",
      "message": "page_size must be a positive integer",
      "solution": "Use a positive integer for page_size. Omit the field to fall back to the default of 100."
    },
    {
      "code": "401",
      "message": "UNAUTHENTICATED",
      "solution": "Each page is a fresh /contx-iq/v1/execute call and re-authorizes. Re-send X-IK-ClientKey and the bearer token on every page."
    }
  ],
  "summary": "This example demonstrates pagination on the CIQ Execute endpoint.\n\nRequest fields:\n- page_token (integer, optional): 1-based page number. Omitted, 0 or negative all mean page 1.\n- page_size (integer, optional): Records per page. Default is 100.\n\nPagination model:\n- Plain offset paging: page n returns records (n - 1) * page_size to n * page_size - 1 of the authorized result. The client increments page_token by 1 between calls (page 1, 2, 3, ...), keeping page_size constant.\n- The response carries no page token. When fewer than page_size records come back, you've hit the last page.\n- Pagination applies to the records inside data[]; partial filters and authorization are re-evaluated on every call.\n\nWhen to use:\n- Knowledge Queries that may return hundreds or thousands of nodes/relationships.\n- Streaming-style consumers that prefer fixed-size chunks.",
  "usecase": "Scenario: A back-office UI lists every car license alice owns, two rows at a time.\n\nGraph structure:\n- Person(alice) -[OWNS]-> Car(kitt) -[HAS]-> LicenseNumber(\"KITT 0001\")\n- Person(alice) -[OWNS]-> Car(caddilacv16) -[HAS]-> LicenseNumber(\"CADV16-007\")\n- Person(alice) -[OWNS]-> Car(skodaOctavia) -[HAS]-> LicenseNumber(\"OCT-2021-XX\")\n\nWalk:\n1. Call /execute with page_token=1, page_size=2 -> returns kitt + caddilacv16.\n2. Call /execute with page_token=2, page_size=2 -> returns skodaOctavia (one record => last page).\n3. The UI knows there are no more rows and stops paging.\n\nTip: Use page_size to bound memory and latency, not to limit returned data. Authorization is applied to every record on every call.",
  "requirements": "Prerequisites:\n- ServiceAccount credentials: For creating the policy and Knowledge Query.\n- AppAgent credentials: For ingesting the graph and executing the query.\n- Bearer token for the subject (alice) when subject type is not _Application.\n\nRequired API access:\n- POST /capture/v1/nodes/ and /capture/v1/relationships/\n- POST /configs/v1/authorization-policies\n- POST /configs/v1/knowledge-queries\n- POST /contx-iq/v1/execute (called once per page)",
  "steps": "Step 1: Capture the Graph\n- Action: POST Person (alice), her Cars, and their LicenseNumber nodes, plus OWNS and HAS relationships.\n- Result: Three car/license rows for alice to page through.\n\nStep 2: Create a Read-Only Policy\n- Action: POST a policy authorizing the Person -> Car -> LicenseNumber traversal, filtered to the subject.\n- Result: Policy ID.\n\nStep 3: Create the Knowledge Query\n- Action: POST a Knowledge Query that reads car.external_id and ln.property.number.\n- Result: Knowledge Query ID.\n\nStep 4: Execute Page 1\n- Authentication: AppAgent credential.\n- Action: POST to /contx-iq/v1/execute with page_token=1 and page_size=2.\n- Result: First 2 records.\n\nStep 5: Execute Page 2\n- Action: POST to /contx-iq/v1/execute with page_token=2 (the next page number) and page_size=2.\n- Result: Remaining record(s). Fewer than page_size means this is the last page.",
  "graph": {
    "caption": "The graph the example runs on: 7 nodes (1 Person, 3 Car, 3 LicenseNumber) and 6 relationships (OWNS, HAS). Hover a node for its properties, click it to highlight its neighbourhood.",
    "nodes": [
      {
        "external_id": "alice",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "alice@email.com"
          },
          {
            "type": "name",
            "value": "Alice Smith"
          }
        ]
      },
      {
        "external_id": "kitt",
        "type": "Car",
        "is_identity": false,
        "properties": [
          {
            "type": "model",
            "value": "Firebird"
          }
        ]
      },
      {
        "external_id": "caddilacv16",
        "type": "Car",
        "is_identity": false,
        "properties": [
          {
            "type": "model",
            "value": "V16"
          }
        ]
      },
      {
        "external_id": "skodaOctavia",
        "type": "Car",
        "is_identity": false,
        "properties": [
          {
            "type": "model",
            "value": "Octavia"
          }
        ]
      },
      {
        "external_id": "ln-kitt-0001",
        "type": "LicenseNumber",
        "is_identity": false,
        "properties": [
          {
            "type": "number",
            "value": "KITT 0001"
          }
        ]
      },
      {
        "external_id": "ln-cad-007",
        "type": "LicenseNumber",
        "is_identity": false,
        "properties": [
          {
            "type": "number",
            "value": "CADV16-007"
          }
        ]
      },
      {
        "external_id": "ln-oct-2021",
        "type": "LicenseNumber",
        "is_identity": false,
        "properties": [
          {
            "type": "number",
            "value": "OCT-2021-XX"
          }
        ]
      }
    ],
    "relationships": [
      {
        "source": "alice",
        "target": "kitt",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "alice",
        "target": "caddilacv16",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "alice",
        "target": "skodaOctavia",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "kitt",
        "target": "ln-kitt-0001",
        "type": "HAS",
        "properties": []
      },
      {
        "source": "caddilacv16",
        "target": "ln-cad-007",
        "type": "HAS",
        "properties": []
      },
      {
        "source": "skodaOctavia",
        "target": "ln-oct-2021",
        "type": "HAS",
        "properties": []
      }
    ]
  },
  "code": [
    {
      "step": "Step 1",
      "description": "Capture the Person (alice), her Cars, and their LicenseNumber nodes.",
      "filename": "POST https://eu.api.indykite.com/capture/v1/nodes/",
      "language": "Json",
      "ingest": {
        "nodes": [
          {
            "external_id": "alice",
            "is_identity": true,
            "type": "Person",
            "properties": [
              {
                "type": "email",
                "value": "alice@email.com"
              },
              {
                "type": "name",
                "value": "Alice Smith"
              }
            ]
          },
          {
            "external_id": "kitt",
            "type": "Car",
            "properties": [
              {
                "type": "model",
                "value": "Firebird"
              }
            ]
          },
          {
            "external_id": "caddilacv16",
            "type": "Car",
            "properties": [
              {
                "type": "model",
                "value": "V16"
              }
            ]
          },
          {
            "external_id": "skodaOctavia",
            "type": "Car",
            "properties": [
              {
                "type": "model",
                "value": "Octavia"
              }
            ]
          },
          {
            "external_id": "ln-kitt-0001",
            "type": "LicenseNumber",
            "properties": [
              {
                "type": "number",
                "value": "KITT 0001"
              }
            ]
          },
          {
            "external_id": "ln-cad-007",
            "type": "LicenseNumber",
            "properties": [
              {
                "type": "number",
                "value": "CADV16-007"
              }
            ]
          },
          {
            "external_id": "ln-oct-2021",
            "type": "LicenseNumber",
            "properties": [
              {
                "type": "number",
                "value": "OCT-2021-XX"
              }
            ]
          }
        ]
      }
    },
    {
      "description": "Capture the relationships: alice OWNS each Car, and each Car HAS its LicenseNumber.",
      "filename": "POST https://eu.api.indykite.com/capture/v1/relationships/",
      "language": "Json",
      "ingest": {
        "relationships": [
          {
            "source": {
              "external_id": "alice",
              "type": "Person"
            },
            "target": {
              "external_id": "kitt",
              "type": "Car"
            },
            "type": "OWNS"
          },
          {
            "source": {
              "external_id": "alice",
              "type": "Person"
            },
            "target": {
              "external_id": "caddilacv16",
              "type": "Car"
            },
            "type": "OWNS"
          },
          {
            "source": {
              "external_id": "alice",
              "type": "Person"
            },
            "target": {
              "external_id": "skodaOctavia",
              "type": "Car"
            },
            "type": "OWNS"
          },
          {
            "source": {
              "external_id": "kitt",
              "type": "Car"
            },
            "target": {
              "external_id": "ln-kitt-0001",
              "type": "LicenseNumber"
            },
            "type": "HAS"
          },
          {
            "source": {
              "external_id": "caddilacv16",
              "type": "Car"
            },
            "target": {
              "external_id": "ln-cad-007",
              "type": "LicenseNumber"
            },
            "type": "HAS"
          },
          {
            "source": {
              "external_id": "skodaOctavia",
              "type": "Car"
            },
            "target": {
              "external_id": "ln-oct-2021",
              "type": "LicenseNumber"
            },
            "type": "HAS"
          }
        ]
      }
    },
    {
      "step": "Step 2",
      "description": "Read-only CIQ Policy authorizing the alice -> Car -> LicenseNumber traversal (subject bound to the caller's token through $token.sub).",
      "filename": "policy.json",
      "language": "Json",
      "ingest": {
        "meta": {
          "policy_version": "1.0-ciq"
        },
        "subject": {
          "type": "Person"
        },
        "condition": {
          "cypher": "MATCH (subject:Person)-[:OWNS]->(car:Car)-[:HAS]->(ln:LicenseNumber)",
          "filter": [
            {
              "operator": "=",
              "attribute": "subject.external_id",
              "value": "$token.sub"
            }
          ]
        },
        "allowed_reads": {
          "nodes": [
            "car",
            "car.*",
            "ln",
            "ln.*"
          ]
        }
      }
    },
    {
      "description": "Request to create the CIQ Policy configuration using REST.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/authorization-policies",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "description": "Authorize the Person -> Car -> LicenseNumber traversal so a read Knowledge Query can return each car and its license number for the subject.",
        "display_name": "policy - person owns cars with license numbers",
        "name": "policy-person-owns-car-licenses",
        "policy": "{\"meta\":{\"policy_version\":\"1.0-ciq\"},\"subject\":{\"type\":\"Person\"},\"condition\":{\"cypher\":\"MATCH (subject:Person)-[:OWNS]->(car:Car)-[:HAS]->(ln:LicenseNumber)\",\"filter\":[{\"operator\":\"=\",\"attribute\":\"subject.external_id\",\"value\":\"$token.sub\"}]},\"allowed_reads\":{\"nodes\":[\"car\",\"car.*\",\"ln\",\"ln.*\"]}}",
        "status": "ACTIVE",
        "tags": []
      }
    },
    {
      "step": "Step 3",
      "description": "Knowledge Query that returns car.external_id and ln.property.number with no filter - pagination will limit how many records come back per call.",
      "filename": "knowledge_query.json",
      "language": "Json",
      "ingest": {
        "nodes": [
          "car.external_id",
          "ln.property.number"
        ]
      }
    },
    {
      "description": "Request to create the Knowledge Query.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/knowledge-queries",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "description": "Return every car license-number tuple this subject is authorized to see. Designed for pagination via page_token / page_size on /contx-iq/v1/execute.",
        "display_name": "knowledge query - paginated car licenses",
        "name": "kq-paginated-licenses",
        "policy_id": "your_policy_gid",
        "query": "{\"nodes\":[\"car.external_id\",\"ln.property.number\"]}",
        "status": "ACTIVE"
      }
    },
    {
      "step": "Step 4",
      "description": "Page 1: page_token=1, page_size=2 - returns the first two records.",
      "filename": "POST https://eu.api.indykite.com/contx-iq/v1/execute",
      "language": "Json",
      "ingest": {
        "id": "your_query_gid_or_name",
        "input_params": {},
        "page_token": 1,
        "page_size": 2
      }
    },
    {
      "description": "Page 1 response - 2 records.",
      "filename": "response_page_1.json",
      "language": "Json",
      "ingest": {
        "data": [
          {
            "nodes": {
              "car.external_id": "kitt",
              "ln.property.number": "KITT 0001"
            }
          },
          {
            "nodes": {
              "car.external_id": "caddilacv16",
              "ln.property.number": "CADV16-007"
            }
          }
        ]
      }
    },
    {
      "step": "Step 5",
      "description": "Page 2: advance page_token to 2, the next page number. Same page_size keeps result chunks consistent.",
      "filename": "POST https://eu.api.indykite.com/contx-iq/v1/execute",
      "language": "Json",
      "ingest": {
        "id": "your_query_gid_or_name",
        "input_params": {},
        "page_token": 2,
        "page_size": 2
      }
    },
    {
      "description": "Page 2 response - 1 record. Fewer than page_size, so this is the last page.",
      "filename": "response_page_2.json",
      "language": "Json",
      "ingest": {
        "data": [
          {
            "nodes": {
              "car.external_id": "skodaOctavia",
              "ln.property.number": "OCT-2021-XX"
            }
          }
        ]
      }
    }
  ],
  "links": {
    "html": "/resources/ciq-23",
    "markdown": "/resources/ciq-23.md",
    "openApi": "https://openapi.indykite.com/"
  }
}