{
  "id": "ciq-7",
  "title": "ContX IQ: Loyalty Program - Which payment method belongs to this license plate?",
  "description": "Real-world loyalty program scenario: Given a vehicle's license plate number, retrieve the credit card associated with an authorized user. Demonstrates multi-condition authorization with consent validation and loyalty plan membership checks.",
  "category": "ContX IQ",
  "tags": [
    "ContX IQ Policy",
    "ContX IQ Query",
    "ContX IQ Execution",
    "Loyalty Program",
    "Consent Validation",
    "Complex Authorization"
  ],
  "api": "ContX IQ",
  "language": "Json",
  "lastUpdated": "2026-03-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",
    "/guides/guide-sandbox"
  ],
  "commonErrors": [
    {
      "code": "401",
      "message": "UNAUTHENTICATED",
      "solution": "Check credentials: ServiceAccount Bearer token for config APIs, X-IK-ClientKey for execution"
    },
    {
      "code": "404",
      "message": "NOT_FOUND",
      "solution": "Verify the query/policy ID exists and belongs to your project"
    }
  ],
  "summary": "This example demonstrates a complex real-world authorization scenario for loyalty programs:\n\nInput: License plate number\nOutput: Credit card external_id (for authorized requests only)\n\nAuthorization conditions checked (all expressed as graph patterns in the policy cypher):\n1. The Application node named by $app_external_id USES a ConsentPayment that the person GRANTED\n2. The person HAS the PaymentMethod, IS_MEMBER of a Loyalty plan, and OWNS a car that HAS a LicenseNumber\n3. The query filter matches the LicenseNumber's number property against $license\n\nThis showcases ContX IQ's ability to enforce business rules through graph relationships.",
  "usecase": "Scenario: A parking loyalty app wants to auto-charge a registered payment method when a vehicle arrives.\n\nBusiness rules enforced by the policy:\n1. License plate registration: the car owner's car must HAVE a LicenseNumber in the graph\n2. Loyalty membership: the owner must be a member (IS_MEMBER) of a Loyalty plan\n3. Consent: the owner must have GRANTED a ConsentPayment that the requesting Application USES, and that consent must be GRANTED to the PaymentMethod\n\nGraph structure (ingested in step 1):\nPerson(ole) -[OWNS]-> Car(carOle) -[HAS]-> LicenseNumber(licenseOle, number AL98745)\nPerson(ole) -[IS_MEMBER]-> Loyalty(loyalty1); Person(alice) -[IS_MEMBER]-> Loyalty(loyalty1)\nPerson(ole) -[HAS]-> PaymentMethod(cb2563)\nPerson(ole) -[GRANTED]-> ConsentPayment(consent1) -[GRANTED]-> PaymentMethod(cb2563)\nApplication(applicationParking) -[USES]-> ConsentPayment(consent1); Company(companyParking) -[OWNS]-> Application(applicationParking)\n\nQuery flow:\n1. The policy cypher matches the Application ($app_external_id) that USES a ConsentPayment GRANTED by a person who HAS a PaymentMethod\n2. The same person must be IS_MEMBER of a Loyalty and OWNS a car that HAS a LicenseNumber\n3. The query filter keeps the LicenseNumber whose number equals $license\n4. If the pattern matches, paymentmethod.external_id is returned (cb2563)",
  "requirements": "Prerequisites:\n- ServiceAccount credentials: For creating policies and queries (Bearer token)\n- AppAgent credentials: For data ingestion and query execution (X-IK-ClientKey)\n\nRequired API access:\n- POST /capture/v1/nodes/ and /capture/v1/relationships/ (loyalty data)\n- POST /configs/v1/authorization-policies (create policy)\n- POST /configs/v1/knowledge-queries (create query)\n- POST /contx-iq/v1/execute (run query)",
  "steps": "Step 1: Ingest Loyalty Program Graph Data\n- Authentication: AppAgent credential (X-IK-ClientKey header)\n- Action: POST nodes: Person (alice, ole), PaymentMethod (cb2563), Car (carOle), LicenseNumber (licenseOle), Loyalty (loyalty1), ConsentPayment (consent1), Company (companyParking), Application (applicationParking)\n- Action: POST relationships: OWNS, HAS, IS_MEMBER, GRANTED, USES\n- Result: Complete loyalty program graph ready for queries\n\nStep 2: Create Loyalty Authorization Policy\n- Authentication: ServiceAccount credential (Bearer token)\n- Action: POST policy with subject type _Application and cypher:\n  - MATCH (app:Application)-[:USES]->(consentpayment:ConsentPayment)<-[:GRANTED]-(person:Person)-[:HAS]->(paymentmethod:PaymentMethod)\n  - MATCH (person)-[:IS_MEMBER]->(loyalty:Loyalty)\n  - MATCH (person)-[:OWNS]->(car:Car)-[:HAS]->(ln:LicenseNumber)\n- Filter: app.external_id = $app_external_id AND subject.external_id = $_appId\n- Allowed reads: ln.*, app.*, paymentmethod.external_id\n- Result: Policy ID returned\n\nStep 3: Create Loyalty Query\n- Authentication: ServiceAccount credential (Bearer token)\n- Action: POST query that returns paymentmethod.external_id, with filter ln.property.number = $license\n- Input parameters: $license (string), $app_external_id (policy filter)\n- Result: Query ID returned\n\nStep 4: Execute Loyalty Query\n- Authentication: AppAgent credential (X-IK-ClientKey header)\n- Action: POST to /contx-iq/v1/execute with input_params license = AL98745 and app_external_id = applicationParking\n- Result: paymentmethod.external_id = cb2563 (if the pattern matches) or an empty data array (if not)\n\nStep 5: Cleanup\n- Action: DELETE query and policy configurations",
  "graph": {
    "caption": "The graph the example runs on: 9 nodes (2 Person, 1 PaymentMethod, 1 Car, 1 LicenseNumber, 1 Loyalty, 1 ConsentPayment, 1 Company, 1 Application) and 9 relationships (HAS, OWNS, IS_MEMBER, GRANTED, USES). 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": "given_name",
            "value": "Alice"
          },
          {
            "type": "last_name",
            "value": "Smith"
          }
        ]
      },
      {
        "external_id": "ole",
        "type": "Person",
        "is_identity": true,
        "properties": [
          {
            "type": "email",
            "value": "ole@yahoo.co.uk"
          },
          {
            "type": "given_name",
            "value": "ole"
          },
          {
            "type": "last_name",
            "value": "einar"
          }
        ]
      },
      {
        "external_id": "cb2563",
        "type": "PaymentMethod",
        "is_identity": false,
        "properties": [
          {
            "type": "payment_name",
            "value": "Credit Card Parking"
          },
          {
            "type": "preference",
            "value": "Pay as you go"
          }
        ]
      },
      {
        "external_id": "carOle",
        "type": "Car",
        "is_identity": false,
        "properties": [
          {
            "type": "category",
            "value": "Car"
          },
          {
            "type": "is_active",
            "value": true
          },
          {
            "type": "vin",
            "value": "pcfjnm78"
          }
        ]
      },
      {
        "external_id": "licenseOle",
        "type": "LicenseNumber",
        "is_identity": false,
        "properties": [
          {
            "type": "status",
            "value": "Active"
          },
          {
            "type": "number",
            "value": "AL98745"
          }
        ]
      },
      {
        "external_id": "loyalty1",
        "type": "Loyalty",
        "is_identity": false,
        "properties": [
          {
            "type": "name",
            "value": "Parking Loyalty Plan"
          }
        ]
      },
      {
        "external_id": "consent1",
        "type": "ConsentPayment",
        "is_identity": false,
        "properties": [
          {
            "type": "name",
            "value": "Consent Parking"
          }
        ]
      },
      {
        "external_id": "companyParking",
        "type": "Company",
        "is_identity": false,
        "properties": [
          {
            "type": "name",
            "value": "City Parking Inc"
          }
        ]
      },
      {
        "external_id": "applicationParking",
        "type": "Application",
        "is_identity": false,
        "properties": [
          {
            "type": "name",
            "value": "City Mall Parking"
          }
        ]
      }
    ],
    "relationships": [
      {
        "source": "ole",
        "target": "cb2563",
        "type": "HAS",
        "properties": []
      },
      {
        "source": "ole",
        "target": "carOle",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "ole",
        "target": "loyalty1",
        "type": "IS_MEMBER",
        "properties": []
      },
      {
        "source": "alice",
        "target": "loyalty1",
        "type": "IS_MEMBER",
        "properties": []
      },
      {
        "source": "ole",
        "target": "consent1",
        "type": "GRANTED",
        "properties": []
      },
      {
        "source": "carOle",
        "target": "licenseOle",
        "type": "HAS",
        "properties": []
      },
      {
        "source": "consent1",
        "target": "cb2563",
        "type": "GRANTED",
        "properties": []
      },
      {
        "source": "companyParking",
        "target": "applicationParking",
        "type": "OWNS",
        "properties": []
      },
      {
        "source": "applicationParking",
        "target": "consent1",
        "type": "USES",
        "properties": []
      }
    ]
  },
  "code": [
    {
      "step": "Step 1a",
      "description": "Capture loyalty program nodes: Person (ole, the car owner, and alice), Car, LicenseNumber, Loyalty, ConsentPayment, PaymentMethod, Company and Application.",
      "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": "given_name",
                "value": "Alice"
              },
              {
                "type": "last_name",
                "value": "Smith"
              }
            ]
          },
          {
            "external_id": "ole",
            "is_identity": true,
            "type": "Person",
            "properties": [
              {
                "type": "email",
                "value": "ole@yahoo.co.uk"
              },
              {
                "type": "given_name",
                "value": "ole"
              },
              {
                "type": "last_name",
                "value": "einar"
              }
            ]
          },
          {
            "external_id": "cb2563",
            "type": "PaymentMethod",
            "properties": [
              {
                "type": "payment_name",
                "value": "Credit Card Parking"
              },
              {
                "type": "preference",
                "value": "Pay as you go"
              }
            ]
          },
          {
            "external_id": "carOle",
            "type": "Car",
            "properties": [
              {
                "type": "category",
                "value": "Car"
              },
              {
                "type": "is_active",
                "value": true
              },
              {
                "type": "vin",
                "value": "pcfjnm78"
              }
            ]
          },
          {
            "external_id": "licenseOle",
            "type": "LicenseNumber",
            "properties": [
              {
                "type": "status",
                "value": "Active"
              },
              {
                "type": "number",
                "value": "AL98745",
                "metadata": {
                  "assurance_level": 3,
                  "source": "BRREG"
                }
              }
            ]
          },
          {
            "external_id": "loyalty1",
            "type": "Loyalty",
            "properties": [
              {
                "type": "name",
                "value": "Parking Loyalty Plan"
              }
            ]
          },
          {
            "external_id": "consent1",
            "type": "ConsentPayment",
            "properties": [
              {
                "type": "name",
                "value": "Consent Parking"
              }
            ]
          },
          {
            "external_id": "companyParking",
            "type": "Company",
            "properties": [
              {
                "type": "name",
                "value": "City Parking Inc"
              }
            ]
          },
          {
            "external_id": "applicationParking",
            "type": "Application",
            "properties": [
              {
                "type": "name",
                "value": "City Mall Parking"
              }
            ]
          }
        ]
      }
    },
    {
      "step": "Step 1b",
      "description": "Capture loyalty relationships: Person -[OWNS]-> Car, Car -[HAS]-> LicenseNumber, Person -[IS_MEMBER]-> Loyalty, Person -[GRANTED]-> ConsentPayment -[GRANTED]-> PaymentMethod, Person -[HAS]-> PaymentMethod, Application -[USES]-> ConsentPayment, Company -[OWNS]-> Application.",
      "filename": "POST https://eu.api.indykite.com/capture/v1/relationships/",
      "language": "Json",
      "ingest": {
        "relationships": [
          {
            "source": {
              "external_id": "ole",
              "type": "Person"
            },
            "target": {
              "external_id": "cb2563",
              "type": "PaymentMethod"
            },
            "type": "HAS"
          },
          {
            "source": {
              "external_id": "ole",
              "type": "Person"
            },
            "target": {
              "external_id": "carOle",
              "type": "Car"
            },
            "type": "OWNS"
          },
          {
            "source": {
              "external_id": "ole",
              "type": "Person"
            },
            "target": {
              "external_id": "loyalty1",
              "type": "Loyalty"
            },
            "type": "IS_MEMBER"
          },
          {
            "source": {
              "external_id": "alice",
              "type": "Person"
            },
            "target": {
              "external_id": "loyalty1",
              "type": "Loyalty"
            },
            "type": "IS_MEMBER"
          },
          {
            "source": {
              "external_id": "ole",
              "type": "Person"
            },
            "target": {
              "external_id": "consent1",
              "type": "ConsentPayment"
            },
            "type": "GRANTED"
          },
          {
            "source": {
              "external_id": "carOle",
              "type": "Car"
            },
            "target": {
              "external_id": "licenseOle",
              "type": "LicenseNumber"
            },
            "type": "HAS"
          },
          {
            "source": {
              "external_id": "consent1",
              "type": "ConsentPayment"
            },
            "target": {
              "external_id": "cb2563",
              "type": "PaymentMethod"
            },
            "type": "GRANTED"
          },
          {
            "source": {
              "external_id": "companyParking",
              "type": "Company"
            },
            "target": {
              "external_id": "applicationParking",
              "type": "Application"
            },
            "type": "OWNS"
          },
          {
            "source": {
              "external_id": "applicationParking",
              "type": "Application"
            },
            "target": {
              "external_id": "consent1",
              "type": "ConsentPayment"
            },
            "type": "USES"
          }
        ]
      }
    },
    {
      "step": "Step 2a",
      "description": "Policy JSON with multi-condition authorization expressed in the cypher: (1) the Application named by $app_external_id USES a ConsentPayment GRANTED by the person who HAS the PaymentMethod, (2) the person IS_MEMBER of a Loyalty, (3) the person OWNS a car that HAS a LicenseNumber. Grants READ on ln.*, app.* and paymentmethod.external_id.",
      "filename": "policy.json",
      "language": "Json",
      "ingest": {
        "meta": {
          "policy_version": "1.0-ciq"
        },
        "subject": {
          "type": "_Application"
        },
        "condition": {
          "cypher": "MATCH (subject:_Application) MATCH (app:Application)-[:USES]->(consentpayment:ConsentPayment)<-[:GRANTED]-(person:Person)-[:HAS]->(paymentmethod:PaymentMethod) MATCH (person)-[:IS_MEMBER]->(loyalty:Loyalty) MATCH (person)-[:OWNS]->(car:Car)-[:HAS]->(ln:LicenseNumber)",
          "filter": [
            {
              "operator": "AND",
              "operands": [
                {
                  "attribute": "app.external_id",
                  "operator": "=",
                  "value": "$app_external_id"
                },
                {
                  "attribute": "subject.external_id",
                  "operator": "=",
                  "value": "$_appId"
                }
              ]
            }
          ]
        },
        "allowed_reads": {
          "nodes": [
            "ln.*",
            "app.*",
            "paymentmethod.external_id"
          ]
        }
      }
    },
    {
      "step": "Step 2b",
      "description": "POST request to create the loyalty authorization policy.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/authorization-policies",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "description": "description of policy",
        "display_name": "policy name",
        "name": "policy-name",
        "policy": "{\"meta\":{\"policy_version\":\"1.0-ciq\"},\"subject\":{\"type\":\"_Application\"},\"condition\":{\"cypher\":\"MATCH (subject:_Application) MATCH (app:Application)-[:USES]->(consentpayment:ConsentPayment)<-[:GRANTED]-(person:Person)-[:HAS]->(paymentmethod:PaymentMethod) MATCH (person)-[:IS_MEMBER]->(loyalty:Loyalty) MATCH (person)-[:OWNS]->(car:Car)-[:HAS]->(ln:LicenseNumber)\",\"filter\":[{\"operator\":\"AND\",\"operands\":[{\"attribute\":\"app.external_id\",\"operator\":\"=\",\"value\":\"$app_external_id\"},{\"attribute\":\"subject.external_id\",\"operator\":\"=\",\"value\":\"$_appId\"}]}]},\"allowed_reads\":{\"nodes\":[\"ln.*\",\"app.*\",\"paymentmethod.external_id\"]}}",
        "status": "ACTIVE",
        "tags": []
      }
    },
    {
      "step": "Step 2c",
      "description": "GET request to verify the policy was created.",
      "filename": "GET https://eu.api.indykite.com/configs/v1/authorization-policies/{policy_id}",
      "language": "Json",
      "ingest": {
        "id": "your_policy_configuration_gid"
      }
    },
    {
      "step": "Step 3a",
      "description": "Query JSON that returns paymentmethod.external_id, filtered on ln.property.number = $license; the graph traversal itself comes from the policy cypher.",
      "filename": "knowledge_query.json",
      "language": "Json",
      "ingest": {
        "nodes": [
          "paymentmethod.external_id"
        ],
        "filter": {
          "attribute": "ln.property.number",
          "operator": "=",
          "value": "$license"
        }
      }
    },
    {
      "step": "Step 3b",
      "description": "POST request to create the loyalty query.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/knowledge-queries",
      "language": "Json",
      "ingest": {
        "project_id": "your_project_gid",
        "description": "description of knowledge query",
        "display_name": "knowledge query name",
        "name": "knowledge-query-name",
        "policy_id": "your_policy_gid",
        "query": "{\"nodes\":[\"paymentmethod.external_id\"],\"filter\":{\"attribute\":\"ln.property.number\",\"operator\":\"=\",\"value\":\"$license\"}}",
        "status": "ACTIVE"
      }
    },
    {
      "step": "Step 3c",
      "description": "GET request to verify the query was created.",
      "filename": "GET https://eu.api.indykite.com/configs/v1/knowledge-queries/{query_id}",
      "language": "Json",
      "ingest": {
        "id": "your_knowledge_query_configuration_gid"
      }
    },
    {
      "step": "Step 4a",
      "description": "Execute the loyalty query with a license plate value. The system checks all authorization conditions before returning the credit card ID.",
      "filename": "POST https://eu.api.indykite.com/contx-iq/v1/execute",
      "language": "Json",
      "ingest": {
        "id": "knowledge_query_gid",
        "input_params": {
          "license": "AL98745",
          "app_external_id": "applicationParking"
        }
      }
    },
    {
      "step": "Step 4b",
      "description": "Response containing the payment method external_id (cb2563). Empty data array if the graph pattern does not match (e.g., no consent GRANTED to the Application, no loyalty membership, or no LicenseNumber with that number).",
      "filename": "response.json",
      "language": "Json",
      "ingest": {
        "data": [
          {
            "nodes": {
              "paymentmethod.external_id": "cb2563"
            }
          }
        ]
      }
    },
    {
      "step": "Step 5a",
      "description": "DELETE request to remove the query.",
      "filename": "DELETE https://eu.api.indykite.com/configs/v1/knowledge-queries/{query_id}",
      "language": "Json",
      "ingest": {
        "id": "your_knowledge_query_configuration_gid"
      }
    },
    {
      "step": "Step 5b",
      "description": "DELETE request to remove the policy.",
      "filename": "DELETE https://eu.api.indykite.com/configs/v1/authorization-policies/{policy_id}",
      "language": "Json",
      "ingest": {
        "id": "your_policy_configuration_gid"
      }
    }
  ],
  "links": {
    "html": "/resources/ciq-7",
    "markdown": "/resources/ciq-7.md",
    "openApi": "https://openapi.indykite.com/"
  }
}