Amounts are decimal strings. Release pays a seller; cancellation returns funds to the same user. Sync compares balances and records discrepancies, but does not adjust the ledger.
Source-verified HTTP and TypeScript examples for all nine Balance operations, including request validation, response envelopes, errors, and idempotency.
Read, move, and verify a user's available and reserved balances. This reference covers all nine routes under /api/v1/users/:userId/balance.
Amounts are decimal strings. Release pays a seller; cancellation returns funds to the same user. Sync compares balances and records discrepancies, but does not adjust the ledger.
Every route takes a required userId string in the path, conventionally usr_...; the controller does not attach an ID-format validation pipe. There are no query parameters or pagination controls. Use an existing user when changing balances.
All successful routes return HTTP 200. The global interceptor wraps the controller result as { data, meta: { requestId, timestamp } }. Errors use { error: { code, message, details? } }. Example IDs, timestamps, and balances below are illustrative fixtures checked against the source, not live account data.
The six amount-changing POST routes inherit these DTO fields. Each endpoint lists its complete body below.
Credit and debit additionally allow optional reference: string. Reserve, release, cancel-reservation, and deduct-reserved require non-empty orderId: string; release also requires non-empty sellerId: string. These DTOs have no reason field. Unknown body fields are rejected by the global validation pipe. Optional fields use IsOptional, so explicit null bypasses their validators; omit unused fields in integrations.
orderId is passed as metadata, not looked up to determine an order-specific reserved amount. The service checks the user's aggregate reserved balance. Cancellation affects only the supplied amount, not all reservations for that order.
The service's declared SyncResult union also includes "adjusted", but no current return branch emits it. Balance mutations return the stored currency, which can differ from a supplied currency string.
Set OFFERHUB_API_URL to your deployment's full API prefix, for example http://localhost:4000/api/v1, and OFFERHUB_API_KEY to your server-side key. Use test users and test funds for these mutation examples. For each POST, generate and persist a fresh UUID v4 as IDEMPOTENCY_KEY before the first request; reuse it only for retries of that same operation.
At the pinned source revision, SDK request and response declarations lag behind the HTTP API. The SDK HTTP client returns the JSON envelope unchanged. It does not unwrap data or translate reference into orderId, or recipientId into sellerId. The examples below explicitly bridge these declaration mismatches; they do not modify the SDK or imply runtime validation. Prefer the cURL contract when checking your installed SDK version.
Use the response types above with the TypeScript examples. as unknown as Envelope<...> acknowledges the SDK's inaccurate return declaration. For order-related requests, the argument assertion bridges the SDK's old request type while sending only real DTO fields. Do not add reference or recipientId to satisfy those old declarations: the API rejects those fields on order-related routes.
GET get/verify do not use idempotency middleware. All seven POST routes support an optional Idempotency-Key through the globally registered guard and interceptor. Without it, repeating a mutation can apply it again. reference and orderId are not deduplication keys.
With a key, the service compares a hash of the request body, replays a stored completed response, and adds Idempotency-Replay: true. Changed payloads produce 409 IDEMPOTENCY_KEY_REUSED; an in-flight request produces 409 IDEMPOTENCY_KEY_IN_PROGRESS. Malformed UUID v4 keys produce 400 VALIDATION_ERROR. Use a distinct key for every new operation, even across endpoints and users: the stored identity does not include the URL or HTTP method. The source records a 24-hour expiry, but the lookup does not itself enforce expiry; do not rely on reusing an old key.
The SDK supports withIdempotencyKey(key) but does not generate a key automatically. Its HTTP client retries POST requests for selected transient statuses, so supply a stable key before issuing a mutation.
These errors apply to every endpoint unless marked POST-only. Endpoint sections add their domain-specific errors.
Missing Authorization example (401):
The six amount operations can additionally return 400 INVALID_AMOUNT_FORMAT after DTO validation (for example "0.00"). Their service requires positive amounts. Credit, debit, reserve, cancel-reservation, and deduct-reserved use the atomic helper, which can throw 409 BALANCE_CONCURRENCY_CONFLICT when a calculated balance would be negative. Release uses its own two-user transaction and does not throw that custom exception. Raw transaction failures can instead reach the global 500 INTERNAL_ERROR handler.
/api/v1/users/:userId/balanceScope: readReturns the local ledger balance. If no balance record exists, returns a zero USD balance with a current timestamp, even if the user ID is unknown. This method does not throw USER_NOT_FOUND.
Source: controller; BalanceService.getBalance.
Parameters: required path userId: string; no query parameters.
Body: none; no DTO is bound to this route.
Idempotency: GET is not intercepted by the idempotency guard.
200 response: Envelope<Balance> (see Response schemas).
Errors: No additional domain error is thrown by getBalance; see Common errors.
401 example:
/api/v1/users/:userId/balance/verifyScope: readReturns the local balance plus providerVerified. True means provider synchronization succeeded with no discrepancy. A nonzero discrepancy of at most 1.00 returns false; an absolute discrepancy greater than 1.00 throws 409. Not-ready providers or caught balance-fetch failures also return false. Although this is GET, it can write a BALANCE_SYNC audit entry.
Source: controller; BalanceService.getBalanceWithProviderCheck.
Parameters: required path userId: string; no query parameters.
Body: none; no DTO is bound to this route.
Idempotency: GET is not intercepted by the idempotency guard.
200 response: Envelope<Balance & { providerVerified: boolean }> (see Response schemas).
Errors: 404 USER_NOT_FOUND for an unknown user; 409 BALANCE_DISCREPANCY when the absolute difference exceeds 1.00. Also see Common errors.
409 example (provider exceeds local available by 2.00):
/api/v1/users/:userId/balance/creditScope: writeAdds the supplied amount to available funds; reserved funds are unchanged. A missing balance row is created for an existing user.
Source: controller; BalanceService.creditAvailable. DTO inheritance.
Parameters: required path userId: string; no query parameters.
Idempotency: Optional UUID v4 header; use one stable key for this operation and its retries.
200 response: Envelope<BalanceOperationResult> (see Response schemas).
Each example starts independently with available 100.00 and reserved 25.00.
Errors: 400 INVALID_AMOUNT_FORMAT, 409 BALANCE_CONCURRENCY_CONFLICT, and Common errors. No available-funds check is needed.
400 example (well-formatted but zero amount):
/api/v1/users/:userId/balance/debitScope: writeSubtracts from available funds; reserved funds are unchanged.
Source: controller; BalanceService.debitAvailable. DTO inheritance.
Parameters: required path userId: string; no query parameters.
Idempotency: Optional UUID v4 header; use one stable key for this operation and its retries.
200 response: Envelope<BalanceOperationResult> (see Response schemas).
Each example starts independently with available 100.00 and reserved 25.00.
Errors: 422 INSUFFICIENT_FUNDS if available funds are too low; 400 INVALID_AMOUNT_FORMAT, 409 BALANCE_CONCURRENCY_CONFLICT, and Common errors.
422 example:
/api/v1/users/:userId/balance/reserveScope: writeMoves the supplied amount from available to reserved funds; total is unchanged.
Source: controller; BalanceService.reserve. DTO inheritance.
Parameters: required path userId: string; no query parameters.
Idempotency: Optional UUID v4 header; use one stable key for this operation and its retries.
200 response: Envelope<BalanceOperationResult> (see Response schemas).
Each example starts independently with available 100.00 and reserved 25.00.
Errors: 422 INSUFFICIENT_FUNDS if available funds are too low; 400 INVALID_AMOUNT_FORMAT, 409 BALANCE_CONCURRENCY_CONFLICT, and Common errors.
422 example:
/api/v1/users/:userId/balance/releaseScope: writeThe path user is the buyer. Atomically deducts the amount from the buyer's reserved balance and credits the seller's available balance. The returned balance, previous/new amounts, and auditLogId belong to the seller, not the buyer.
Source: controller; BalanceService.release. DTO inheritance.
Parameters: required path userId: string; no query parameters.
Idempotency: Optional UUID v4 header; use one stable key for this operation and its retries.
200 response: Envelope<BalanceOperationResult> (see Response schemas).
Each example starts independently with available 100.00 and reserved 25.00 on the seller; the buyer has at least 10.00 reserved.
Errors: 422 RESERVE_NOT_FOUND if the buyer has insufficient aggregate reserved funds; 400 INVALID_AMOUNT_FORMAT and Common errors.
422 example:
/api/v1/users/:userId/balance/cancel-reservationScope: writeReturns the supplied amount from this user's reserved balance to their available balance; total is unchanged. It does not cancel every reservation for an order.
Source: controller; BalanceService.cancelReservation. DTO inheritance.
Parameters: required path userId: string; no query parameters.
Idempotency: Optional UUID v4 header; use one stable key for this operation and its retries.
200 response: Envelope<BalanceOperationResult> (see Response schemas).
Each example starts independently with available 100.00 and reserved 25.00.
Errors: 422 RESERVE_NOT_FOUND if reserved funds are too low; 400 INVALID_AMOUNT_FORMAT, 409 BALANCE_CONCURRENCY_CONFLICT, and Common errors.
422 example:
/api/v1/users/:userId/balance/deduct-reservedScope: writeSubtracts the supplied amount from reserved funds without crediting another user. Available funds are unchanged; total decreases.
Source: controller; BalanceService.deductReserved. DTO inheritance.
Parameters: required path userId: string; no query parameters.
Idempotency: Optional UUID v4 header; use one stable key for this operation and its retries.
200 response: Envelope<BalanceOperationResult> (see Response schemas).
Each example starts independently with available 100.00 and reserved 25.00.
Errors: 422 RESERVE_NOT_FOUND if reserved funds are too low; 400 INVALID_AMOUNT_FORMAT, 409 BALANCE_CONCURRENCY_CONFLICT, and Common errors.
422 example:
/api/v1/users/:userId/balance/syncScope: writeCompares provider funds with the local available balance and records an audit result. A discrepancy returns synced: true, action: "flagged"; no discrepancy returns synced: true, action: "none". Neither branch changes the local balance.
Source: controller; BalanceService.syncBalanceFromProvider.
Parameters: required path userId: string; no query parameters.
Body: none; no DTO is bound to this route.
Idempotency: Optional UUID v4 header; use one stable key for this operation and its retries.
200 response: Envelope<SyncResult> (see Response schemas).
Errors: 404 USER_NOT_FOUND for an unknown user, plus Common errors. A failure caught around provider getBalance returns 200 with synced: false, not a guaranteed 504.
404 example:
If the provider is not ready, the response has synced: false, all three amount strings "0.00", and action: "none" (those zeros do not prove the real ledger is empty). If fetching the provider balance fails after readiness succeeds:
Verified against Orchestrator revision b399e0800298. The executable controller decorators take precedence over comments (the sync comment still says GET, while the route is POST).
Continue with the API overview, authentication and scopes, or SDK Balance guide.