convex-credits

API reference

The host client API.

All methods take a Convex context and an argument object. The client supplies its configured namespace.

MethodArgumentsResult
grantowner, amount, key, reason, referencemovementId, balance
reserveowner, amount, key, reason, referencechargeId, balance
completechargeId, keychargeId
releasechargeId, keychargeId, balance
refundchargeId, key, amount, reasonmovementId, balance
transferfrom, to, amount, key, reason, referencedebitId, creditId, fromBalance, toBalance
balanceownernumber
getChargechargeIdcharge or null
historyowner, paginationOptsConvex pagination result

Mutation results also include a kind that identifies the operation. Amounts are positive safe integers. Zero, fractions, negative values, and non-finite values are rejected.

History uses Convex cursor pagination. Pass { cursor: null, numItems: 50 } for the first page. Use the returned continueCursor for the next page.

The balance returned by an operation is the balance at that operation. A retry returns the original result, even if later operations changed the account. Use balance to read the current value.

Account imports

MethodArgumentsResult
getAccountowneraccount with active/importing state, or null
beginImportowner, key, openingBalance, expectedBalance, expectedMovements, expectedPendingCharges, occurredAt, referenceaccountId, balance
importMovementowner, key, delta, balanceAfter, occurredAt, reason, reference, pendingChargemovementId, balance, optional chargeId
finishImportowner, keyaccountId, balance

See Migration for the required snapshot, ordering, and cutover rules.

History sorts by occurredAt in descending order. New movements use the current time. Imported movements preserve the source time. Equal timestamps sort by Convex creation order. Each row includes a signed delta, balanceAfter, reason, reference, and operation key. A negative delta is credits spent; it is not the remaining balance.

History uses paginator from convex-helpers. For React pagination, use usePaginatedQuery from convex-helpers/react, as described in Convex component pagination. The built-in React hook does not provide the same page-boundary behavior for components.

Immediate charges

For work that completes within one host mutation, call reserve and then complete in that mutation. Use separate stable keys for each call. The host transaction makes both calls atomic, and the ledger contains one debit.

Errors

Errors use ConvexError({ code }).

CodeMeaning
INVALID_AMOUNTAmount or count is outside its allowed safe-integer range.
INVALID_IDENTIFIERNamespace, operation key, or owner is blank or exceeds 512 characters.
INSUFFICIENT_CREDITSThe debit would make the balance negative.
BALANCE_OVERFLOWThe credit would exceed the safe-integer limit.
OPERATION_KEY_CONFLICTThe key was used for a different request.
CHARGE_NOT_FOUNDThe charge does not exist in this namespace.
CHARGE_RELEASED / CHARGE_COMPLETEDThe requested transition conflicts with the terminal state.
CHARGE_NOT_COMPLETEDA refund requires a completed charge.
REFUND_EXCEEDS_CHARGEThe refund exceeds the unrefunded amount.
SELF_TRANSFERBoth transfer owners are the same.
ACCOUNT_EXISTS / ACCOUNT_NOT_FOUNDThe import requires a new/existing account respectively.
ACCOUNT_IMPORTING / ACCOUNT_NOT_IMPORTINGThe operation is not valid in the account's current state.
INVALID_TIMESTAMPAn import timestamp is invalid or in the future.
INVALID_IMPORT_COUNTSExpected pending charges exceed expected movements.
INVALID_PENDING_CHARGEA pending charge requires a negative source movement.
IMPORT_OUT_OF_ORDERThe timestamp precedes the last imported movement.
IMPORT_BALANCE_MISMATCH / IMPORT_COUNT_MISMATCHThe source snapshot and imported data do not reconcile.

On this page