{
  "id": "ciq-24",
  "title": "ContX IQ: Resolve the Caller's IKG Subject with GET /contx-iq/v1/whoami",
  "description": "Ask IndyKite which IKG node an end-user token resolves to. GET /contx-iq/v1/whoami returns the subject type and external ID that Token Introspect derived from the bearer token - and nothing else - so a client can build subject-bound AuthZEN and CIQ requests without decoding the token itself.",
  "category": "ContX IQ",
  "tags": [
    "ContX IQ Execution",
    "WhoAmI",
    "Token Introspect",
    "End-User Token",
    "Subject Resolution"
  ],
  "api": "ContX IQ",
  "language": "Python",
  "lastUpdated": "2026-09-17",
  "openApiEndpoints": [
    "/configs/v1/token-introspects",
    "/contx-iq/v1/whoami",
    "/access/v1/evaluation"
  ],
  "relatedGuides": [
    "/guides/guide-contx-iq",
    "/guides/guide-token-introspect",
    "/guides/guide-authzen",
    "/guides/guide-credentials"
  ],
  "commonErrors": [
    {
      "code": "401",
      "message": "end-user token is required",
      "solution": "whoami always needs a user access token. Add the Authorization: Bearer <token> header next to X-IK-ClientKey."
    },
    {
      "code": "401",
      "message": "insufficient API access level for appAgent",
      "solution": "The Application Agent lacks the ContXIQ API permission. Add it to the agent's api_permissions (Config API or Hub) and retry."
    },
    {
      "code": "401",
      "message": "UNAUTHENTICATED",
      "solution": "The bearer token failed introspection: expired, or its issuer/audience matches no Token Introspect configuration of the project. Check the jwt_matcher (or opaque validation) settings."
    },
    {
      "code": "200",
      "message": "type and id are empty strings",
      "solution": "The Token Introspect configuration that validated the token has no IKG node type to match against. Set ikg_node_type on the configuration so the subject resolves to a node type."
    }
  ],
  "summary": "This example demonstrates the whoami endpoint of the ContX IQ REST API.\n\nEndpoint: GET /contx-iq/v1/whoami - no body, no parameters.\n\nHeaders:\n- X-IK-ClientKey: AppAgent credential. The agent needs the ContXIQ API permission (the same one /contx-iq/v1/execute uses).\n- Authorization: Bearer <user-access-token> - required. Without it the call fails with 401 \"end-user token is required\".\n\nResponse:\n- type: the IKG node type the token subject was matched to (the ikg_node_type of the Token Introspect configuration that validated the token).\n- id: the token's original subject - the sub claim, or the claim named by sub_claim in the Token Introspect configuration. It equals the node's external_id in the IKG.\n\nNothing else from the token is exposed: no issuer, expiry, or mapped claims. If the Token Introspect configuration had no IKG node type to match against, both fields come back as empty strings with status 200.\n\nWhy it matters:\n- The pair (type, id) is exactly the subject IndyKite will use for this user in CIQ executions and AuthZEN decisions. Send it as subject.type / subject.id.\n- On 3.0-kbac policies a bearer-token AuthZEN call whose subject differs from the token's subject is denied with 403 Forbidden; whoami removes the guesswork.\n- Clients no longer need to parse the JWT or replicate the sub_claim configuration to know who they are acting as.",
  "usecase": "Scenario: A front end holds an OAuth access token issued by an external identity provider for alice@example.com and wants to render \"what can I do?\" using the AuthZEN search endpoints. It needs the subject.type and subject.id IndyKite associates with that token.\n\nFlow:\n1. The Token Introspect configuration maps tokens from the identity provider to Person nodes, matching sub to external_id.\n2. The client calls GET /contx-iq/v1/whoami with its AppAgent credential and the user's bearer token.\n3. IndyKite answers {\"type\": \"Person\", \"id\": \"alice@example.com\"}.\n4. The client sends that pair as the subject of /access/v1/search/resource (or any other AuthZEN or CIQ request) for the same user.\n\nAlso useful for:\n- Debugging Token Introspect: a whoami call shows immediately whether the sub_claim and ikg_node_type settings produce the node you expect.\n- Multi-IdP setups: the same client code works whatever claim each configuration uses as the subject.",
  "requirements": "Prerequisites:\n- ServiceAccount credentials: For creating the Token Introspect configuration.\n- AppAgent credentials with the ContXIQ API permission: For calling /contx-iq/v1/whoami (X-IK-ClientKey).\n- A user access token issued by the identity provider the Token Introspect configuration trusts (issuer and audience must match).\n\nRequired API access:\n- POST /configs/v1/token-introspects\n- GET /contx-iq/v1/whoami",
  "steps": "Step 1: Configure Token Introspect\n- Authentication: ServiceAccount credential as Bearer token.\n- Action: POST /configs/v1/token-introspects with ikg_node_type \"Person\" and a jwt_matcher for your identity provider. With perform_upsert true the Person node is created on first use.\n- Result: Tokens from that issuer now resolve to Person nodes whose external_id is the token's sub claim.\n\nStep 2: Ask who the token is\n- Authentication: AppAgent credential (X-IK-ClientKey) plus the user's token (Authorization: Bearer).\n- Action: GET /contx-iq/v1/whoami.\n- Result: {\"type\": \"Person\", \"id\": \"alice@example.com\"}.\n\nStep 3: See the failure without a user token\n- Action: Repeat the call with only X-IK-ClientKey.\n- Result: 401 {\"message\": \"end-user token is required\"}.\n\nStep 4: Reuse the answer as the AuthZEN subject\n- Action: Put type and id into subject.type / subject.id of an evaluation or search request, sent with the same bearer token.\n- Result: The decision is made for the very node the token resolves to.",
  "code": [
    {
      "step": "Step 1",
      "description": "Token Introspect configuration: tokens from the trusted issuer are matched to Person nodes by their sub claim (external_id). The response type of whoami comes from ikg_node_type; its id from the subject claim.",
      "filename": "POST https://eu.api.indykite.com/configs/v1/token-introspects",
      "language": "Json",
      "ingest": {
        "claims_mapping": {
          "email": {
            "selector": "email"
          },
          "name": {
            "selector": "full_name"
          }
        },
        "description": "Token introspect description",
        "display_name": "Token introspect name",
        "ikg_node_type": "Person",
        "jwt_matcher": {
          "audience": "audience-id",
          "issuer": "https://example.com"
        },
        "name": "rest-token-introspect",
        "online_validation": {
          "cache_ttl": 600
        },
        "perform_upsert": true,
        "project_id": "gid-of-project"
      }
    },
    {
      "step": "Step 2",
      "description": "whoami: no body, no parameters. Send X-IK-ClientKey (AppAgent credential) and Authorization: Bearer <user-access-token>. The response is shown.",
      "filename": "GET https://eu.api.indykite.com/contx-iq/v1/whoami",
      "language": "Json",
      "ingest": {
        "type": "Person",
        "id": "alice@example.com"
      }
    },
    {
      "description": "Same call in Python. The two returned values are used as the AuthZEN subject.",
      "filename": "whoami.py",
      "language": "Python",
      "code": "\nimport http.client\nimport json\n\nconn = http.client.HTTPSConnection(\"eu.api.indykite.com\")\n\nheaders = {\n    'X-IK-ClientKey': \"\",\n    'Authorization': \"Bearer <user-access-token>\"\n}\n\nconn.request(\"GET\", \"/contx-iq/v1/whoami\", headers=headers)\nres = conn.getresponse()\nme = json.loads(res.read())\n\n# me == {\"type\": \"Person\", \"id\": \"alice@example.com\"}\nsubject = {\"type\": me[\"type\"], \"id\": me[\"id\"]}\n"
    },
    {
      "step": "Step 3",
      "description": "Without the Authorization: Bearer header the endpoint refuses the call: there is no _Application form of whoami.",
      "filename": "Response 401",
      "language": "Json",
      "ingest": {
        "message": "end-user token is required"
      }
    },
    {
      "step": "Step 4",
      "description": "The whoami answer plugged into an AuthZEN evaluation for the same user. Send it with the same bearer token; on 3.0-kbac policies the subject must equal the token's subject or the call is denied with 403.",
      "filename": "POST https://eu.api.indykite.com/access/v1/evaluation",
      "language": "Json",
      "code": "{\n  \"subject\": {\n    \"type\": \"Person\",\n    \"id\": \"alice@example.com\"\n  },\n  \"resource\": {\n    \"type\": \"Car\",\n    \"id\": \"kitt\"\n  },\n  \"action\": {\n    \"name\": \"CAN_DRIVE\"\n  }\n}"
    }
  ],
  "links": {
    "html": "/resources/ciq-24",
    "markdown": "/resources/ciq-24.md",
    "openApi": "https://openapi.indykite.com/"
  }
}