Documentation Index
Fetch the complete documentation index at: https://docs.kaireonai.com/llms.txt
Use this file to discover all available pages before exploring further.
POST /api/v1/respond/bulk records multiple outcome events in a single round trip and returns a per-item success/failure manifest. The route is designed for batch loaders, file imports, and replay tools that need to ingest historical conversions without the per-call overhead of POST /api/v1/respond.
What it does
The handler atsrc/app/api/v1/respond/bulk/route.ts accepts an outcomes: [...] array (1 to 1000 items) validated against BulkRespondSchema declared inline at route.ts:37-39. Each item is processed sequentially in chunks of 50 (CHUNK_SIZE at route.ts:43). Per-item logic mirrors the singular /respond route: outcome-type lookup, creative resolution for channelId and placementId, offer existence check, idempotency-key check, then a single prisma.$transaction that writes one interaction_history row plus one outbox_events row of topic outcome.recorded (route.ts:142-192).
A failed item never aborts the batch — the route accumulates errors[] with { index, error } entries and continues. The response status is 200 when at least one item succeeded and 422 when every item failed (route.ts:239). Per-item summary aggregation runs as fire-and-forget after the transaction commits (route.ts:196-209).
Quick start
How it works
Authentication and rate limiting
Every call passes throughrequireTenant at src/lib/tenant.ts:88 (same surface as /recommend). After tenant resolution, the handler checks isPlaygroundTenant(t.tenantId) at src/lib/playground-guard.ts:15 and applies playgroundRateLimit window (route.ts:58-60). The rate limiter runs in failMode: "closed" — when the limiter cannot reach Redis the request is rejected.
Validation
The body is parsed byparseJsonBody and validated by validateBody(BulkRespondSchema, ...) from src/lib/api-validate.ts (route.ts:62-66). The schema enforces:
outcomeslength is between 1 and 1000 (route.ts:38).- Each item must include
customerId,offerId, andoutcome(allz.string().min(1)). directionif supplied must be"inbound"or"outbound".conversionValue, when supplied, must be a number.
Outcome-type lookup
Outcome types are loaded once for the whole batch (route.ts:69-72). When an item names an outcome that is not registered for the tenant, the per-item error is Unknown outcome type: "<key>" and the item is skipped (route.ts:91-95).
Idempotency
Each item gets anidempotencyKey. If the caller supplied one it is used verbatim; otherwise the route synthesizes one from customerId + offerId + creativeId + outcomeKey + 5-minute time bucket (route.ts:137-139). The deduplicationId is customerId:offerId:creativeId:outcomeKey:idempotencyKey (route.ts:140).
The transaction at route.ts:143-192 first runs tx.interactionHistory.findFirst({ where: { idempotencyKey, tenantId } }). When a row already exists, the transaction returns { alreadyRecorded: true } and the item counts as succeeded without a duplicate insert.
Outbox-backed event delivery
After each successful insert, the same transaction callscreateOutboxEvent(tx, { topic: "outcome.recorded", payload: {...} }) (route.ts:178-189) — the event row is durable in the same commit as the interaction row. The dedicated outbox publisher pod picks the row up and delivers to the configured EventPublisher backend. See Outbox publisher.
Conversion value resolution
WhenconversionValue is omitted and the outcome’s classification is "positive", the route falls back to the offer’s businessValue (route.ts:131-134). For neutral or negative outcomes the conversion value stays 0.
Audit log
A singlebulk_create audit entry summarizing { processed, succeeded, failed } is written after the loop completes (route.ts:223-230). Per-item entries are not created — the audit chain records the batch as one unit.
Reference
Request body
Array of 1 to 1000 outcome items. Validated by
BulkRespondSchema at route.ts:37-39.outcomes[] per-item shape
Validated by BulkOutcomeItem at route.ts:20-35.
Customer identifier the outcome is attributed to.
Offer identifier the outcome targets. Must exist for the tenant or the item is rejected with
Offer not found.Outcome type key. Must match an
OutcomeType.key registered for the tenant via /api/v1/outcome-types (route.ts:69-72).Optional creative identifier. When supplied, the route auto-resolves
channelId and placementId from the creative row (route.ts:103-115). Required for impression-class outcomes that need channel attribution.Channel identifier. Defaults to the creative’s
channelId when creativeId is supplied.Placement identifier. Defaults to the creative’s
placementId when creativeId is supplied.Channel name passed through to
interaction.context.channel for analytics filters (route.ts:162). Not used for routing.Placement name passed through to
interaction.context.placement (route.ts:163).ISO timestamp the outcome occurred. Defaults to
new Date() when omitted (route.ts:171).Operator-supplied idempotency key. When omitted the route synthesizes one from
customerId + offerId + creativeId + outcomeKey + 5-minute time bucket (route.ts:137-139).Free-form context merged into
interaction.context JSON. The route always adds bulk: true (route.ts:160-165).Free-form details merged into
interaction.outcome JSON alongside conversionValue (route.ts:167-170).Monetary value of the outcome. Falls back to the offer’s
businessValue for positive outcomes when omitted (route.ts:131-134)."inbound" or "outbound". Defaults to "outbound" for impression-category outcomes and "inbound" for everything else (route.ts:128).Response
Returned atroute.ts:232-240.
Total items in the request body — equals
outcomes.length.Items that produced an
interaction_history insert OR were idempotency-deduplicated against an existing row.Items that produced an error. Per-item details are in
errors[].Present only when
failed > 0. Each entry: { index: number, error: string }. index is the 0-based position in the request outcomes[] array.Status codes
| Code | When | Source |
|---|---|---|
| 200 | At least one item succeeded | route.ts:239 |
| 400 | Invalid JSON body or schema validation failure | parseJsonBody / validateBody at route.ts:62-65 |
| 401 | Missing tenant context | tenant.ts:117 |
| 403 | Invalid tenant identifier | tenant.ts:128 |
| 422 | Every item in the batch failed | route.ts:239 |
| 429 | Rate limit exceeded | playgroundRateLimit at playground-guard.ts:37 |
| 500 | Unexpected error before the per-item loop | serverError from src/lib/api-error.ts:90 |
Required headers
| Header | Required | Read at | Purpose |
|---|---|---|---|
Content-Type | Yes | Next.js framework | application/json |
X-API-Key | Yes (one of the two) | tenant.ts:97 | API key (krn_…) |
X-Tenant-Id | Yes (one of the two) | tenant.ts:113 | Direct tenant id |
Configuration
Environment variables
| Variable | Effect |
|---|---|
NODE_ENV=test | Disables rate limiting in the test runner (rate-limit-unified.ts:101) |
OUTBOX_LIVENESS_FILE and the outbox-reaper cron reads OUTBOX_REAPER_STALENESS_SECONDS.
Limits
| Setting | Value | Source |
|---|---|---|
| Maximum items per request | 1000 | BulkRespondSchema.outcomes.max(1000) at route.ts:38 |
| Chunk size for sequential processing | 50 | CHUNK_SIZE at route.ts:43 |
| Rate limit (playground tenants) | 100 / 60s | playgroundRateLimit at playground-guard.ts:37 |
| Rate limit (non-playground) | 1000 / 60s | playgroundRateLimit at playground-guard.ts:37 |
Honest limits
- Per-item processing is sequential, not parallel. A 1000-item batch produces up to 1000 sequential
prisma.$transactioncalls — wall time scales linearly with batch size. Sustained throughput is bounded by Postgres write contention, not API replicas. - Summary updates (
updateSummaries) run fire-and-forget after each transaction commits (route.ts:196-209). When the call rejects, the warning is logged but the item still counts assucceeded. Aggregate counters ininteraction_summarymay briefly lag the underlying interaction rows. - The route does not expose a
dryRunflag. To validate a batch shape without writing, the caller has to send a single-item request and discard the result. - The
errors[]array is included only whenfailed > 0. Callers checkingresponse.errors.lengthmust handle the missing-field case. - An idempotency-deduplicated item is reported as
succeeded, not as a separate counter. Callers cannot distinguish “newly recorded” from “already recorded” from the response alone.
Related
- Respond API — single-outcome endpoint with the same per-item contract.
- Outcome Types — register the outcome keys this endpoint validates against.
- Outbox publisher — delivers the
outcome.recordedevents written by this endpoint. - Cron tier — runs
/api/v1/cron/outbox-reaperto recover stuck outbox rows.