Skip to content
← Public packages

@kentcdodds/wise

Call Wise profiles, balances, quotes, recipients, and transfers with wiseApiToken.

src/list-balances.ts

41 lines · 1.5 KB · TypeScript
import { resolveProfileId, wiseRequest, type ProfileRef } from './core.ts'

/**
 * List Wise balances for a profile (defaults to the business profile).
 * Read-only; uses GET /v4/profiles/{profileId}/balances.
 *
 * @param input.profileId - Wise profile id; omit to use profileType.
 * @param input.profileType - personal or business when profileId is omitted (default business).
 * @param input.types - Balance types filter (default STANDARD).
 *
 * @example
 * import listBalances from 'kody:@kentcdodds/wise/list-balances'
 * const { balances } = await listBalances({ profileType: 'business' })
 */
export default async function listBalances(
	input: ProfileRef & { types?: string } = {},
) {
	const profileId = await resolveProfileId(input)
	// Wise requires `types` (e.g. STANDARD). Default covers the usual cash balances.
	const types = String(input.types || 'STANDARD').trim() || 'STANDARD'
	const raw = (await wiseRequest({
		path: `/v4/profiles/${profileId}/balances`,
		query: { types },
		label: 'Wise balances',
	})) as Array<{
		id?: number
		currency?: string
		type?: string
		amount?: { value?: number; currency?: string }
		cashAmount?: { value?: number; currency?: string }
		totalWorth?: { value?: number; currency?: string }
	}>
	const balances = (Array.isArray(raw) ? raw : []).map((b) => ({
		id: b.id ?? null,
		currency: b.currency || b.amount?.currency || null,
		type: b.type || null,
		amount: b.amount?.value ?? b.cashAmount?.value ?? null,
		totalWorth: b.totalWorth?.value ?? null,
	}))
	return { profileId, balances, count: balances.length }
}