B2B Second Inventory Reports API Reference
Endpoint reference for the bb phase 6 admin report API — summary reads, the table catalogue and runner, and CSV or XLSX export jobs — with permissions, throttles, limits and error codes.
B2B Second Inventory (bb) — Reports API Reference
Audience: Admin engineers integrating with the bb phase 6 backend. Reports are read-only views over orders, refunds, returns, stock and customers. The only write is queuing, cancelling and downloading an export.
All endpoints are prefixed with /api, need a JWT, and need BB_Reports_READ. Superadmin passes every permission check. Every JSON response uses the ResponseDto envelope (message, errorCode, data); a download streams the file.
Base path: /api/admin/bb/reports. Every :publicId is a UUID (a malformed one is a 400).
Routes
Every route checks BB_Reports_READ and nothing else, including export queue and cancel. No other BB_Reports_ code is checked by any route.
| Method and path | Throttle | What it returns |
|---|---|---|
GET overview | 60 per 60 s, per user (ADMIN_ANALYTICS_READ) | The overview figures |
GET kpis | same | KPI figures for the window and its comparison window |
GET trends | same | One row per day of the window |
GET payment-methods | same | Revenue by payment method |
GET top-products | same | Top products; limit 1 to 20 (default 5), sortBy default units |
GET refunds-returns | same | Refund and return figures |
GET tables | same | The report catalogue; takes no query |
GET tables/:reportId | same | One report's rows; sortBy, sortDirection, page, size |
POST exports | 5 per 3600 s, per user (ADMIN_ANALYTICS_EXPORT) | Queues a file; needs Idempotency-Key |
POST exports/:publicId/cancel | 10 per 60 s, per IP (ADMIN_WRITE) | Cancels the caller's own queued or running export |
GET exports | 60 per 60 s, per user | The caller's own exports; page (max 1000, default 1), size (max 100, default 20) |
GET exports/:publicId | same | One of the caller's own exports |
GET exports/:publicId/download | same | The file, Cache-Control: no-store |
The summary routes take period, from and to (BbReportPeriodQueryDto). With no query the window is last_30_days. from and to are YYYY-MM-DD in Asia/Kathmandu, to is inclusive and may not be later than today, and both are allowed only with period=custom. A query key the DTO does not list answers 400.
Create an export
POST /api/admin/bb/reports/exports queues a CSV or XLSX of one report. Exports reuse the bb_catalog queue as bb_catalog_job rows with entity = 'report'.
Request, with the header Idempotency-Key: 5b0c3f7e-6a1d-4f0e-9d52-2c7f1a9e4b10:
{
"reportId": "orders_list",
"format": "xlsx",
"period": "custom",
"from": "2026-10-01",
"to": "2026-10-31",
"sortBy": "placedAt",
"sortDirection": "desc"
}Only reportId and format are required. A point-in-time report ignores the period and stores null period keys.
Response, 201:
{
"message": "Bb report export queued",
"errorCode": null,
"data": {
"publicId": "019a2f10-7c1e-7a3b-9c11-3f2a1b4c5d6e",
"reportId": "orders_list",
"reportTitle": "Orders",
"format": "xlsx",
"status": "queued",
"periodFrom": "2026-10-01",
"periodTo": "2026-10-31",
"requestedAt": "2026-11-01T04:00:00.000Z",
"completedAt": null,
"expiresAt": null,
"rowCount": null,
"error": null
}
}The envelope above shows the shape, built from ResponseDto and BbReportExport; the example values are illustrative. completedAt is set only on a completed export. expiresAt is completedAt plus 48 hours. error is set only on a failed export and is one of a closed set of sentences, never a raw error.
Idempotency
| Situation | Answer |
|---|---|
| Same key, same body, again | The same job is returned |
| Same key, different body | 409 IDEMPOTENCY_KEY_CONFLICT |
| No key | 400 IDEMPOTENCY_KEY_REQUIRED |
Download
GET exports/:publicId/download answers 200 with the file as text/csv or the XLSX type application/vnd.openxmlformats-officedocument.spreadsheetml.sheet. Another admin's export, and a catalog job's id, are both 404 BB_JOB_NOT_FOUND. A job with no finished file is 409 BB_JOB_RESULT_NOT_AVAILABLE. A file past 48 hours is 410, whether or not the daily sweep has deleted it yet.
Limits
| Limit | Value | Source |
|---|---|---|
| Longest window | 366 * 3 days | BB_REPORT_MAX_PERIOD_DAYS |
| Export formats | csv, xlsx | BB_REPORT_EXPORT_FORMATS |
| XLSX rows | 10,000 | BB_REPORT_EXCEL_EXPORT_MAX_ROWS |
| Export rows, any format | 50,000 | BB_CATALOG_EXPORT_MAX_ROWS |
| File retention | 48 hours from finished_at | BB_REPORT_EXPORT_RETENTION_MS |
| Active export or import jobs per admin | 2 | BB_CATALOG_JOB_MAX_ACTIVE_PER_ADMIN |
| Read statement budget | 10 s per statement | BB_REPORT_READ_STATEMENT_TIMEOUT_MS |
| Export worker statement budget | 60 s | BB_REPORT_WORKER_STATEMENT_TIMEOUT_MS |
| Concurrent read snapshots per process | 3, then a 5 s wait for a permit | BB_REPORT_MAX_CONCURRENT_SNAPSHOTS, BB_REPORT_PERMIT_WAIT_MS |
The row caps are enforced by the worker, not at queue time. A report with more rows than the cap ends the job failed with BB_JOB_EXPORT_TOO_LARGE (the worker raises it as a permanent error) and the export's error carries a closed sentence naming the cap.
The active-job cap counts catalog and inventory jobs too, so its message says "export or import jobs".
Error codes
From common/types/error-codes.ts and the controller's @ApiResponse list.
| Status | Code | When |
|---|---|---|
| 400 | BB_REPORT_PERIOD_INVALID | period, from or to is unknown, not a calendar date, or past today in Asia/Kathmandu |
| 400 | BB_REPORT_PERIOD_TOO_LONG | A custom range longer than the window limit |
| 400 | BB_REPORT_SORT_INVALID | sortBy is not a key the report sorts by. A bad sortDirection gets the validation pipe's generic 400 |
| 400 | BB_REPORT_NOT_FOUND | An export names a report id that does not exist. On a read route the same code is a 404 |
| 400 | IDEMPOTENCY_KEY_REQUIRED | Export queued without the header |
| 403 | BB_Reports_READ is missing | |
| 404 | BB_JOB_NOT_FOUND | Export not found, or not the caller's |
| 409 | BB_JOB_TOO_MANY_ACTIVE | A third active job for this admin |
| 409 | BB_JOB_NOT_CANCELLABLE | Cancel on a finished export |
| 409 | BB_JOB_RESULT_NOT_AVAILABLE | Download before a file exists |
| 409 | BB_CATALOG_BUSY | Queue or cancel while the catalog is busy |
| 409 | IDEMPOTENCY_KEY_CONFLICT | Same key, different body |
| 410 | BB_JOB_RESULT_GONE | Download past retention |
| 429 | Rate limit exceeded | |
| 503 | BB_REPORT_TIMEOUT | A read ran out of time, or waited too long for a snapshot permit |
Guardrail summary
BB_Reports_READshows buyer names and what each buyer spent, with or withoutBB_Customers_READ. Grant it on purpose.- An export is the caller's own. Cancel and download both treat another admin's export as not found.
- A CSV cell that starts with
=,+,-,@, TAB, CR, LF or'gets a leading'. A negative money cell therefore prints as the text'-1000.01. This is a known limit. - An export file is deleted 48 hours after it finishes; the job row stays.
- Money is
bigintpaisa. The rules are in Reports backend.
See Also
B2B Second Inventory Reports Backend
Module layout, the money rules every report figure follows, the read snapshot and its budget, and the export worker and file lifecycle for bb phase 6.
B2B Second Inventory Reports Deploy Notes
Deploy order, migration 0069, the locks it takes, the required permissions:sync, the manual grant step, why the API and worker deploy together, and rollback limits for bb phase 6. Phase 6 ships as its own release.