Reading products
Load a product's public data and show its prices correctly in any currency.
const product = await Sorai.product("0199b0f0-0000-7000-8000-000000000021");
if (product) {
console.log(product.name, product.prices);
}Sorai.product() loads one product of the store with the publishable key.
It resolves to the product, or to null when the store has no active product
with that id, and it rejects when the request fails. The fields are listed in
the reference; any other field the API
returns comes through as it is.
Prices
A product has one price per currency, and Sorai never converts between them. Pick the price for the currency you want to show, and fall back to one the product does have:
const product = await Sorai.product("0199b0f0-0000-7000-8000-000000000021");
const price = product?.prices.find((p) => p.currency === "IDR") ?? product?.prices[0];Minor units
Amounts are integers in the currency's minor units, and how many minor
units make one major unit depends on the currency. amount_minor: 149000 is
Rp 149.000 in IDR, which has no subdivision, but $1,490.00 in USD.
Dividing by 100 is wrong for every currency without two decimals. To show a price, divide by the currency's own factor:
const minorDigits = { IDR: 0, JPY: 0, KRW: 0, VND: 0, BHD: 3, KWD: 3, OMR: 3 };
function formatPrice({ amount_minor, currency }, locale) {
const digits = minorDigits[currency] ?? 2;
return new Intl.NumberFormat(locale, {
style: "currency",
currency,
minimumFractionDigits: digits,
maximumFractionDigits: digits,
}).format(amount_minor / 10 ** digits);
}
formatPrice({ amount_minor: 149000, currency: "IDR" }, "id-ID"); // "Rp 149.000"<sorai-price> does this for you, for every currency.

