All monetary amounts in stripe402 are expressed in units, where 1 unit = 1/10,000 of a dollar. This enables sub-cent pricing for high-volume, low-cost API calls.
Conversion Table
Units
Dollars
Cents
Example Use
1
$0.0001
0.01¢
Ultra-low-cost operations
100
$0.01
1¢
Joke API, simple lookups
500
$0.05
5¢
Weather data, translations
1,000
$0.10
10¢
Image generation
10,000
$1.00
100¢
Complex computations
50,000
$5.00
500¢
Default minimum top-up
Constants
Defined in @stripe402/core:
/** Number of units per dollar (1 unit = 1/10,000 of a dollar) */exportconstUNITS_PER_DOLLAR=10_000/** Number of units per cent (1 cent = 100 units) */exportconstUNITS_PER_CENT=100/** Default minimum top-up amount in units ($5.00 = 50,000 units) */exportconstDEFAULT_MIN_TOP_UP=50_000/** Default currency */exportconstDEFAULT_CURRENCY='usd'
Conversion Functions
unitsToCents(units: number): number
Converts units to Stripe cents, rounding up to the nearest cent. Stripe charges in cents, so this conversion happens before every PaymentIntent.create() call.
Implementation: Math.ceil(units / UNITS_PER_CENT) — always rounds up so the server never undercharges.
unitsToDollars(units: number): string
Converts units to a human-readable dollar string for display purposes. Trailing zeros are trimmed.
Implementation: (units / UNITS_PER_DOLLAR).toFixed(4).replace(/0+$/, '').replace(/\.$/, '') — formats to 4 decimal places, then strips trailing zeros and trailing decimal point.
How Units Flow Through the System
Route configuration: Price is set in units (amount: 500 = 5¢ per request)
402 response: Units are sent to the client as-is in PaymentRequirements.amount
Top-up: Client specifies topUpAmount in units (must be >= minTopUp)
Stripe charge: Units are converted to cents via unitsToCents() for the PaymentIntent
Balance: Credits are stored and deducted in units
Display: unitsToDollars() formats units for human-readable output