API-8Required
Send money as an integer with its currency
Send amounts as an integer count of the currency's smallest unit, together with the ISO 4217 currency code. Never use floating-point numbers for money.
How to do it
Write 129.50 Swedish kronor as { "amount": 12950, "currency": "SEK" }. Look up each currency's number of decimals in ISO 4217, because it is not always two. Japanese yen has none and Kuwaiti dinar has three. Floats can't store 0.1 exactly, so sums drift. A decimal string such as "129.50" also works, if the whole API uses it. Store it with exact types. See also hold money as integer minor units everywhere.
Examples
JSON example
{ "total": { "amount": 12950, "currency": "SEK" } }
How to check
Search the OpenAPI file for money fields typed number. None remain, and each amount has a currency beside it.
Sources
Related rules
- ARC-50 Wrap domain values in their own types in Clean code and architecture
- DAT-2 Choose exact types in Data and databases
- PAY-10 Hold money as integer minor units everywhere in Payments and billing
- PAY-11 Split amounts without losing a cent in Payments and billing
- APP-15 Read numbers the way people type them in Web app interface
- LOC-27 Take the currency from the amount and not from the locale in Localization
- LOC-32 Send raw values with standard codes instead of formatted text in Localization
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/api-design/requests-and-responses/money">Agent Wranglers API design playbook</a>. In code you write, add a comment next to it with the same link.