B2B Second Inventory API Reference
Endpoint reference for the bb phase 1 admin API — roles, permissions, staff, customers and audit — with contracts and error codes.
B2B Second Inventory (bb) — API Reference
Audience: Admin engineers integrating with the bb phase 1 backend. Phase 1 has no storefront or public endpoint.
All endpoints are prefixed with /api, need a JWT, and need the listed permission. Superadmin passes every permission check. Reads are throttled with RATE_LIMITS.ADMIN_READ (30 per 60 s per IP) and writes with RATE_LIMITS.ADMIN_WRITE (10 per 60 s per IP).
Each POST that creates a record requires an Idempotency-Key header; without it the response is 400 IDEMPOTENCY_KEY_REQUIRED.
Route Surfaces
| Surface | Base Path | Auth |
|---|---|---|
| Roles | /api/admin/bb/roles | JWT + BB_Roles_* |
| Permissions | /api/admin/bb/permissions | JWT + BB_Roles_READ |
| Staff | /api/admin/bb/staff | JWT + BB_Staff_* |
| Customers | /api/admin/bb/customers | JWT + BB_Customers_* |
| Audit | /api/admin/bb/audit | JWT + BB_Audit_READ |
Permission codes are BB_<Module>_<ACTION>. BB_Roles and BB_Staff cannot be assigned to a bb role, so only superadmin reaches those surfaces.
Endpoint Reference
Roles
| Method | Path | Auth | Description |
|---|---|---|---|
| GET | /api/admin/bb/roles | BB_Roles_READ | List bb roles. Query: search. Not paginated; capped at 100 |
| GET | /api/admin/bb/roles/:publicId | BB_Roles_READ | One role with its permission codes |
| POST | /api/admin/bb/roles | BB_Roles_CREATE | Create a role. Needs Idempotency-Key. Returns 201 |
| PATCH | /api/admin/bb/roles/:publicId | BB_Roles_UPDATE | Rename or re-describe |
| PUT | /api/admin/bb/roles/:publicId/permissions | BB_Roles_UPDATE | Replace the full permission code set |
| DELETE | /api/admin/bb/roles/:publicId | BB_Roles_DELETE | Delete a role no staff member holds |
Permissions
| Method | Path | Auth | Description |
|---|---|---|---|
| GET | /api/admin/bb/permissions | BB_Roles_READ | Assignable codes grouped by module |
Staff
| Method | Path | Auth | Description |
|---|---|---|---|
| GET | /api/admin/bb/staff | BB_Staff_READ | Paginated list. Query: page, size, search, sort, order |
| GET | /api/admin/bb/staff/:id | BB_Staff_READ | One staff member |
| POST | /api/admin/bb/staff | BB_Staff_CREATE | Create. Needs Idempotency-Key. Returns 201 |
| PATCH | /api/admin/bb/staff/:id | BB_Staff_UPDATE | Update profile or role (banned included) |
| DELETE | /api/admin/bb/staff/:id | BB_Staff_DELETE | Ban. Body: reason |
| POST | /api/admin/bb/staff/:id/unban | BB_Staff_UPDATE | Unban. Returns 200 |
Customers
| Method | Path | Auth | Description |
|---|---|---|---|
| GET | /api/admin/bb/customers | BB_Customers_READ | Paginated list of live customers |
| GET | /api/admin/bb/customers/deleted | BB_Customers_RESTORE | Paginated list of soft-deleted customers |
| GET | /api/admin/bb/customers/:publicId | BB_Customers_READ | One live customer |
| POST | /api/admin/bb/customers | BB_Customers_CREATE | Create. Needs Idempotency-Key. Returns 201 |
| PATCH | /api/admin/bb/customers/:publicId | BB_Customers_UPDATE | Update; null clears a field |
| DELETE | /api/admin/bb/customers/:publicId | BB_Customers_DELETE | Soft delete. Returns 200 with the customer |
| POST | /api/admin/bb/customers/:publicId/restore | BB_Customers_RESTORE | Restore. Returns 200 with the customer |
Audit
| Method | Path | Auth | Description |
|---|---|---|---|
| GET | /api/admin/bb/audit | BB_Audit_READ | Cursor list, newest first |
| GET | /api/admin/bb/audit/:publicId | BB_Audit_READ | One audit row |
Query Parameters
| Endpoint | Parameter | Rule |
|---|---|---|
| Staff list | page | Integer, min 1, default 1 |
| Staff list | size | Integer 1 to 100, default 20 |
| Staff list | search | Max 100 characters |
| Staff list | sort | updatedAt (default), createdAt, name, email |
| Staff list | order | asc, desc (default) |
| Customer list | page, size, search, order | As staff. search matches name, phone, email and company name |
| Customer list | sort | updatedAt (default), createdAt, name |
| Customer list | status | active, disabled |
| Deleted customers | page, size, search | As above. Order is fixed: deleted_at descending, then id descending |
| Audit list | limit | Default 20, max 100 |
| Audit list | before_seq | Return rows with id below this; pass the previous nextCursor |
| Audit list | entityType | role, staff, customer |
| Audit list | entityPublicId, actorId | UUID |
| Audit list | action | Pattern ^bb\.[a-z_]+\.[a-z_]+$, max 64 characters, for example bb.customer.update |
| Audit list | from, to | Inclusive ISO 8601 with a timezone offset, for example 2026-10-30T00:00:00Z |
An unknown query key, size over 100, an unknown sort, or ?pagination=false on a customer list is 400.
Key Contracts
POST /api/admin/bb/roles
Header: Idempotency-Key: 5d0c6c52-2b1e-4a1a-9f43-6c1d9a3e8b10
Request:
{
"name": "Sales Desk",
"description": "Handles B2B customers",
"permissionCodes": ["BB_Customers_READ", "BB_Customers_CREATE"]
}name is 1 to 100 characters. description is up to 500. permissionCodes holds up to 50 unique assignable codes.
Response 201:
{
"message": "Bb role created successfully",
"errorCode": null,
"data": {
"publicId": "0193a5c0-6d6c-7b0e-8a39-1f2f6f7a9c11",
"name": "Sales Desk",
"description": "Handles B2B customers",
"staffCount": 0,
"permissions": ["BB_Customers_CREATE", "BB_Customers_READ"]
}
}Role list returns data as { "roles": [ ...without permissions ], "totalCount": 1 }.
PUT /api/admin/bb/roles/:publicId/permissions
Request ([] removes every code):
{
"permissionCodes": ["BB_Customers_READ", "BB_Audit_READ"]
}Response 200 is the same role shape as above, with permissions sorted ascending.
Rejected code response, 400:
{
"message": "These permission codes cannot be assigned to a bb role.",
"errorCode": "BB_PERMISSION_NOT_ASSIGNABLE",
"details": { "codes": ["BB_Staff_READ"] }
}GET /api/admin/bb/permissions
{
"message": "Bb permissions fetched successfully",
"errorCode": null,
"data": {
"BB_Audit": [
{ "code": "BB_Audit_READ", "module": "BB_Audit", "action": "READ", "description": null }
],
"BB_Customers": [
{ "code": "BB_Customers_READ", "module": "BB_Customers", "action": "READ", "description": null }
]
}
}Only BB_Audit and BB_Customers modules appear. data is {} when none are synced. The description values shown are illustrative.
POST /api/admin/bb/staff
Header: Idempotency-Key: 8e0a1b9c-3f41-4d7b-a0a5-52c7e9d4f3a2
{
"name": "Asha Rai",
"email": "asha@example.com",
"password": "Str0ngPassw0rd",
"phone": "+9779800000000",
"rolePublicId": "0193a5c0-6d6c-7b0e-8a39-1f2f6f7a9c11"
}name is 2 to 100 characters. password is 8 to 128 characters with an uppercase letter, a lowercase letter and a digit. phone is optional E.164. rolePublicId must be a bb role.
Response 201:
{
"message": "Bb staff created successfully",
"errorCode": null,
"data": { "id": "0193a5c4-0a11-7c3d-b6d1-8e5f2a4c7b90" }
}GET /api/admin/bb/staff/:id
{
"message": "Bb staff fetched successfully",
"errorCode": null,
"data": {
"id": "0193a5c4-0a11-7c3d-b6d1-8e5f2a4c7b90",
"name": "Asha Rai",
"email": "asha@example.com",
"phone": "+9779800000000",
"image": null,
"banned": false,
"banReason": null,
"role": { "publicId": "0193a5c0-6d6c-7b0e-8a39-1f2f6f7a9c11", "name": "Sales Desk" },
"createdAt": "2026-10-30T09:12:44.000Z",
"updatedAt": "2026-10-30T09:12:44.000Z"
}
}The list endpoint returns an array of this object plus count, currentPage and totalPage.
PATCH /api/admin/bb/staff/:id
All fields optional. phone and image accept null to clear. image is up to 2048 characters.
{
"name": "Asha K. Rai",
"phone": null,
"rolePublicId": "0193a5c0-6d6c-7b0e-8a39-1f2f6f7a9c11"
}DELETE /api/admin/bb/staff/:id (ban)
{ "reason": "Left the company" }{
"message": "Bb staff banned successfully",
"errorCode": null
}reason is 1 to 500 characters.
POST /api/admin/bb/customers
Header: Idempotency-Key: 2a7c4e10-91b3-4f1d-8c55-7d0b6a1e3f44
{
"name": "Himal Traders",
"phone": "9841234567",
"email": "Orders@Himal.example",
"companyName": "Himal Traders Pvt. Ltd.",
"address": "Kathmandu",
"panVatNumber": "123456789",
"notes": "Net 30 terms",
"status": "active"
}Every field is optional, but one of name, phone, companyName is required. Blank strings become null. phone is normalised to E.164 (+9779841234567 here) and email is lower-cased.
Response 201:
{
"message": "Bb customer created successfully",
"errorCode": null,
"data": { "publicId": "0193a5c8-2d4e-7f10-9a6b-3c8d1e5f7a20" }
}GET /api/admin/bb/customers/:publicId
{
"message": "Bb customer fetched successfully",
"errorCode": null,
"data": {
"publicId": "0193a5c8-2d4e-7f10-9a6b-3c8d1e5f7a20",
"name": "Himal Traders",
"phone": "+9779841234567",
"email": "orders@himal.example",
"companyName": "Himal Traders Pvt. Ltd.",
"address": "Kathmandu",
"panVatNumber": "123456789",
"notes": "Net 30 terms",
"status": "active",
"createdAt": "2026-10-30T09:20:01.000Z",
"updatedAt": "2026-10-30T09:20:01.000Z",
"deletedAt": null
}
}PATCH /api/admin/bb/customers/:publicId
An omitted key is left unchanged; null clears it. status cannot be null.
{ "notes": null, "status": "disabled" }POST /api/admin/bb/customers/:publicId/restore
Body is optional. Use the flags after a 409 to restore without the contested value.
{ "clearPhone": true, "clearEmail": false }Phone conflict, 409:
{
"message": "...",
"errorCode": "BB_CUSTOMER_PHONE_TAKEN",
"details": {
"field": "phone",
"conflictingPublicId": "0193a5c9-7a10-7e22-8b13-4d6e0f2a9c33"
}
}The message text is not a contract; branch on errorCode.
GET /api/admin/bb/audit
Example: GET /api/admin/bb/audit?entityType=customer&limit=2
{
"message": "Bb audit fetched successfully",
"errorCode": null,
"data": [
{
"publicId": "0193a5ca-1b2c-7d3e-8f40-5a6b7c8d9e01",
"createdAt": "2026-10-30T09:25:10.000Z",
"actor": {
"id": "0193a5b0-0000-7000-8000-000000000001",
"name": "Super Admin",
"role": "superadmin"
},
"action": "bb.customer.update",
"entityType": "customer",
"entityPublicId": "0193a5c8-2d4e-7f10-9a6b-3c8d1e5f7a20",
"summary": "...",
"changes": [
{ "field": "status", "changed": true, "before": "active", "after": "disabled" },
{ "field": "notes", "changed": true }
]
}
],
"nextCursor": "42"
}Pass nextCursor back as before_seq for the next page. nextCursor is null on the last page. The response never carries the integer id or the client IP. summary is free text and is shown as "..." here.
Error Reference
| Code | HTTP | Endpoint | Meaning |
|---|---|---|---|
BB_ROLE_NOT_FOUND | 404 | Role get, update, permissions, delete | Role is missing or is a retail role |
BB_ROLE_NOT_FOUND | 400 | Staff create, staff update | rolePublicId is missing or is a retail role |
BB_STAFF_NOT_FOUND | 404 | Staff get, update, ban, unban | Missing, or a retail admin |
BB_PERMISSION_NOT_ASSIGNABLE | 400 | Role create, set permissions | Code is not BB_Audit_* or BB_Customers_*, or is not synced; see details.codes |
ROLE_NAME_RESERVED | 403 | Role create, update | superadmin, admin or customer |
ROLE_NAME_ALREADY_EXISTS | 409 | Role create, update | Name in use |
ROLE_SELF_TARGET_FORBIDDEN | 403 | Role update, set permissions, delete | Actor holds this role |
ROLE_IN_USE | 409 | Role delete | At least one staff member holds it |
TEAM_EMAIL_ALREADY_IN_USE | 409 | Staff create | Email taken |
TEAM_PHONE_ALREADY_IN_USE | 409 | Staff create, update | Phone taken |
TEAM_SELF_ROLE_CHANGE_FORBIDDEN | 403 | Staff update | Actor changed their own role |
TEAM_SELF_BAN_FORBIDDEN | 403 | Staff ban | Actor is the target |
TEAM_SUPERADMIN_PROTECTED | 403 | Staff ban | Target is superadmin |
BB_CUSTOMER_NOT_FOUND | 404 | Customer get, update, delete, restore | Missing, or not in the required live/deleted state |
BB_CUSTOMER_IDENTITY_REQUIRED | 400 | Customer create, update | No name, phone or company would remain |
BB_CUSTOMER_PHONE_INVALID | 400 | Customer create, update | Phone is not a valid number |
BB_CUSTOMER_EMAIL_INVALID | 400 | Customer create, update | Email fails the email CHECK |
BB_CUSTOMER_FIELD_INVALID | 400 | Customer create, update | Another field CHECK failed |
BB_CUSTOMER_PHONE_TAKEN | 409 | Customer create, update, restore | A live customer holds the phone |
BB_CUSTOMER_EMAIL_TAKEN | 409 | Customer create, update, restore | A live customer holds the email |
BB_AUDIT_NOT_FOUND | 404 | Audit get | Unknown publicId |
BB_AUDIT_RANGE_INVALID | 400 | Audit list | from is after to |
PAGINATION_LIMIT_INVALID / PAGINATION_LIMIT_TOO_LARGE / PAGINATION_CURSOR_INVALID | 422 | Audit list | Bad limit or before_seq |
IDEMPOTENCY_KEY_REQUIRED | 400 | The three create routes | Header missing |
ROLE_PERMISSION_BB_CODE_NOT_ALLOWED | 400 | Retail role-permissions write | A BB_* code was sent to a retail role |
ROLE_NOT_FOUND / TEAM_USER_NOT_FOUND / TEAM_ROLE_NOT_FOUND | 404 / 404 / 400 | Retail role, team routes | A bb role or bb staff member was addressed through a retail route |
A path parameter that is not a UUID is 400 from ParseUUIDPipe.
Guardrail Summary
- Never send a
BB_*code to a retail role endpoint. It is refused. - Never address a bb role or bb staff member through
/api/rolesor/api/team. They answer as not found. - Do not offer
BB_Roles_*orBB_Staff_*in a bb role editor. UseGET /api/admin/bb/permissionsas the only source of assignable codes. - Send a fresh
Idempotency-Keyper create intent, and reuse it only when retrying that same request. - Treat
PUT .../permissionsas a full replace, not a merge. - Page the audit list with
nextCursor; do not compute offsets. - Do not rely on audit
changescarrying values. Onlystatus,name,descriptionandbanneddo. - Branch on
errorCode, never onmessage.
See Also
B2B Second Inventory Backend Architecture
Internal architecture of the bb phase 1 modules — composition, data model, separation invariants, audit, error contract and file map.
B2B Second Inventory Deploy Notes
Pre-deploy prod-dump replay, rollback runbooks A and B for migration 0060, and post-deploy verification for bb phase 1.