API Reference
PBCP
Context API

Context API

อ่าน Business Context ที่ Context Builder ประมวลผลจาก events แล้ว — aggregated state ต่อ entity พร้อมใช้ สำหรับ rule engines, automation, workflow, และ batch jobs

Base: https://pbcp-api.pcampus.co

Story vs Context: Context API คืน raw aggregated state ต่อ entity — Story API คืน composed narrative พร้อม timeline และ insights สำหรับ Pulse และ end-user surfaces ให้ใช้ Story API แทน

Versioning

Path prefixStatus
/v1/context/...Preferred — ใช้สำหรับ new integrations
/context/...Alias — backward compatible กับ pilot; may deprecate

Common headers

x-tenant-id: pharmacy-demo

GET /v1/context/customers/:customerId

Returns Customer Context for tenant.

Response 200

{
  "contextType": "customer",
  "tenantId": "pharmacy-demo",
  "customerId": "CUST-1",
  "displayName": "Somchai Jai-dee",
  "email": "somchai@example.com",
  "status": "active",
  "updatedAt": "2026-06-28T10:15:00.000Z",
  "lastEventId": "550e8400-e29b-41d4-a716-446655440000",
  "lastEventName": "order.completed"
}

Errors

StatusCodeWhen
400VALIDATION_ERRORMissing x-tenant-id
404NOT_FOUNDNo context for this customer in tenant

GET /v1/context/orders/:orderId

Response 200

{
  "contextType": "order",
  "tenantId": "pharmacy-demo",
  "orderId": "ORD-1001",
  "status": "completed",
  "total": 2500,
  "currency": "THB",
  "customerId": "CUST-1",
  "sessionId": "SES-abc123",
  "lineItems": [
    { "productId": "PROD-9", "qty": 2, "amount": 1200, "name": "Vitamin C" }
  ],
  "updatedAt": "2026-06-28T10:15:00.000Z",
  "lastEventName": "order.completed"
}

GET /v1/context/sessions/:sessionId

Returns Session Context including search history and cart state.

Response 200

{
  "contextType": "session",
  "tenantId": "pharmacy-demo",
  "sessionId": "SES-abc123",
  "customerId": "CUST-1",
  "channel": "pos",
  "searches": [
    {
      "query": "paracetamol",
      "resultCount": 12,
      "occurredAt": "2026-06-28T10:05:00.000Z",
      "eventId": "550e8400-e29b-41d4-a716-446655440002"
    }
  ],
  "cart": {
    "total": 450,
    "itemCount": 1,
    "lineItems": [{ "productId": "PROD-9", "qty": 1, "amount": 450 }],
    "updatedAt": "2026-06-28T10:10:00.000Z"
  },
  "updatedAt": "2026-06-28T10:10:00.000Z",
  "lastEventName": "cart.updated"
}

GET /v1/context/products/:productId

Response 200

{
  "contextType": "product",
  "tenantId": "pharmacy-demo",
  "productId": "PROD-1",
  "sku": "SKU-001",
  "name": "Vitamin C 1000mg",
  "status": "active",
  "updatedAt": "2026-06-28T10:15:00.000Z",
  "lastEventName": "product.created"
}

GET /v1/context/entities/:entityId

Returns raw accepted facts linked to a stable entity ID. For systems that need evidence by entity without Story composition.

Response 200

{
  "contextType": "entity",
  "tenantId": "pharmacy-demo",
  "entityId": "ent_abc123",
  "factIds": ["550e8400-e29b-41d4-a716-446655440000"],
  "facts": [
    {
      "factId": "550e8400-e29b-41d4-a716-446655440000",
      "eventName": "order.created",
      "occurredAt": "2026-06-28T10:15:00.000Z",
      "payload": { "orderId": "ORD-1001" },
      "roles": [
        { "role": "actor", "entityId": "ent_abc123", "entityType": "party" }
      ]
    }
  ]
}

GET /metrics/business-pulse

Business pulse summary for Pulse dashboard.

Query params:

ParamDefaultOptions
windowHours2424 (today) or 720 (30 days)

Response 200

{
  "collectedAt": "2026-06-28T12:00:00.000Z",
  "windowHours": 24,
  "tenantId": "pharmacy-demo",
  "topOrderedProducts": [
    { "productId": "P-1", "sku": "SKU-001", "name": "Vitamin C", "count": 12 }
  ],
  "topEngagedProducts": [
    { "productId": "P-2", "sku": "SKU-002", "name": "Paracetamol", "count": 8 }
  ],
  "topCustomers": [
    {
      "customerId": "USER-1",
      "displayName": "Jane Doe",
      "sessionCount": 5,
      "totalActiveMs": 1800000,
      "avgSessionMs": 360000
    }
  ],
  "timing": {
    "avgSessionDurationMs": 420000,
    "avgTimeToOrderMs": 840000,
    "avgCartDwellMs": 360000,
    "sessionsAnalyzed": 42,
    "ordersAnalyzed": 8
  }
}

Example

# Today's pulse
curl -s 'https://pbcp-api.pcampus.co/metrics/business-pulse' \
  -H 'x-tenant-id: pharmacy-demo'
 
# Last 30 days
curl -s 'https://pbcp-api.pcampus.co/metrics/business-pulse?windowHours=720' \
  -H 'x-tenant-id: pharmacy-demo'

Context API

Read Business Context that the Context Builder has processed from events — aggregated state per entity, ready to use for rule engines, automation, workflows, and batch jobs.

Base: https://pbcp-api.pcampus.co

Story vs Context: Context API returns raw aggregated state per entity — Story API returns a composed narrative with timeline and insights. For Pulse and end-user surfaces, use Story API instead.

Versioning

Path prefixStatus
/v1/context/...Preferred — use for new integrations
/context/...Alias — backward compatible with pilot; may deprecate

Common headers

x-tenant-id: pharmacy-demo

GET /v1/context/customers/:customerId

Returns Customer Context for tenant.

Response 200

{
  "contextType": "customer",
  "tenantId": "pharmacy-demo",
  "customerId": "CUST-1",
  "displayName": "Somchai Jai-dee",
  "email": "somchai@example.com",
  "status": "active",
  "updatedAt": "2026-06-28T10:15:00.000Z",
  "lastEventId": "550e8400-e29b-41d4-a716-446655440000",
  "lastEventName": "order.completed"
}

Errors

StatusCodeWhen
400VALIDATION_ERRORMissing x-tenant-id
404NOT_FOUNDNo context for this customer in tenant

GET /v1/context/orders/:orderId

Response 200

{
  "contextType": "order",
  "tenantId": "pharmacy-demo",
  "orderId": "ORD-1001",
  "status": "completed",
  "total": 2500,
  "currency": "THB",
  "customerId": "CUST-1",
  "sessionId": "SES-abc123",
  "lineItems": [
    { "productId": "PROD-9", "qty": 2, "amount": 1200, "name": "Vitamin C" }
  ],
  "updatedAt": "2026-06-28T10:15:00.000Z",
  "lastEventName": "order.completed"
}

GET /v1/context/sessions/:sessionId

Returns Session Context including search history and cart state.

Response 200

{
  "contextType": "session",
  "tenantId": "pharmacy-demo",
  "sessionId": "SES-abc123",
  "customerId": "CUST-1",
  "channel": "pos",
  "searches": [
    {
      "query": "paracetamol",
      "resultCount": 12,
      "occurredAt": "2026-06-28T10:05:00.000Z",
      "eventId": "550e8400-e29b-41d4-a716-446655440002"
    }
  ],
  "cart": {
    "total": 450,
    "itemCount": 1,
    "lineItems": [{ "productId": "PROD-9", "qty": 1, "amount": 450 }],
    "updatedAt": "2026-06-28T10:10:00.000Z"
  },
  "updatedAt": "2026-06-28T10:10:00.000Z",
  "lastEventName": "cart.updated"
}

GET /v1/context/products/:productId

Response 200

{
  "contextType": "product",
  "tenantId": "pharmacy-demo",
  "productId": "PROD-1",
  "sku": "SKU-001",
  "name": "Vitamin C 1000mg",
  "status": "active",
  "updatedAt": "2026-06-28T10:15:00.000Z",
  "lastEventName": "product.created"
}

GET /v1/context/entities/:entityId

Returns raw accepted facts linked to a stable entity ID. For systems that need evidence by entity without Story composition.

Response 200

{
  "contextType": "entity",
  "tenantId": "pharmacy-demo",
  "entityId": "ent_abc123",
  "factIds": ["550e8400-e29b-41d4-a716-446655440000"],
  "facts": [
    {
      "factId": "550e8400-e29b-41d4-a716-446655440000",
      "eventName": "order.created",
      "occurredAt": "2026-06-28T10:15:00.000Z",
      "payload": { "orderId": "ORD-1001" },
      "roles": [
        { "role": "actor", "entityId": "ent_abc123", "entityType": "party" }
      ]
    }
  ]
}

GET /metrics/business-pulse

Business pulse summary for Pulse dashboard.

Query params:

ParamDefaultOptions
windowHours2424 (today) or 720 (30 days)

Response 200

{
  "collectedAt": "2026-06-28T12:00:00.000Z",
  "windowHours": 24,
  "tenantId": "pharmacy-demo",
  "topOrderedProducts": [
    { "productId": "P-1", "sku": "SKU-001", "name": "Vitamin C", "count": 12 }
  ],
  "topEngagedProducts": [
    { "productId": "P-2", "sku": "SKU-002", "name": "Paracetamol", "count": 8 }
  ],
  "topCustomers": [
    {
      "customerId": "USER-1",
      "displayName": "Jane Doe",
      "sessionCount": 5,
      "totalActiveMs": 1800000,
      "avgSessionMs": 360000
    }
  ],
  "timing": {
    "avgSessionDurationMs": 420000,
    "avgTimeToOrderMs": 840000,
    "avgCartDwellMs": 360000,
    "sessionsAnalyzed": 42,
    "ordersAnalyzed": 8
  }
}

Example

# Today's pulse
curl -s 'https://pbcp-api.pcampus.co/metrics/business-pulse' \
  -H 'x-tenant-id: pharmacy-demo'
 
# Last 30 days
curl -s 'https://pbcp-api.pcampus.co/metrics/business-pulse?windowHours=720' \
  -H 'x-tenant-id: pharmacy-demo'