Search.../

convertCurrency( )

Returns an array of amounts converted from the original (from) currency to the target (to) currency and the timestamp for the conversion rate used.

Description

Use the convertCurrency() function to convert an array of one or more amounts between two currencies. The convertCurrency() function returns an array of converted amounts and the timestamp for the conversion rate used.

Note: The currency codes used must exist in the array of supported currencies returned by the listCurrencies() function.

Admin Method

This function requires elevated permissions to run. This function is not universal and runs only on the backend.

Syntax

function convertCurrency(identifiers: ConvertCurrencyIdentifiers, amounts: Array<DecimalValue>): Promise<ConvertCurrencyResponse>

convertCurrency Parameters

NAME
TYPE
DESCRIPTION
identifiers
ConvertCurrencyIdentifiers

Identifying details needed to determine which currency rate to convert. The combination of the from and to properties together comprise the unique ID.

amounts
Array<
DecimalValue
>

Amounts to convert.

Returns

Return Type:

Promise<
ConvertCurrencyResponse
>
NAME
TYPE
DESCRIPTION
amounts
Array<
DecimalValue
>

Converted amounts.

rateTimestamp
Date

Date and time the conversion rate was last updated.

Was this helpful?

Convert an array of amounts from one currency to another (dashboard page code)

Copy Code
1import { currencies } from 'wix-ecom.v2';
2
3const identifiers = {
4 'from': 'USD',
5 'to': 'GBP'
6};
7
8const amounts = [
9 {
10 'decimalPlaces': 2,
11 'value': '1000'
12 },
13 {
14 'decimalPlaces': 2,
15 'value': '20324'
16 }
17];
18
19currencies.convertCurrency(identifiers, amounts)
20 .then((convertedAmounts) => {
21 const firstConvertedAmount = convertedAmounts.amounts[0];
22 const timestamp = convertedAmounts.rateTimestamp;
23 });
24
25/* Promise resolves to:
26 * {
27 * "amounts": [
28 * {
29 * "decimalPlaces": 2,
30 * "value": "795"
31 * },
32 * {
33 * "decimalPlaces": 2,
34 * "value": "1616287"
35 * }
36 * ],
37 * "rateTimestamp": "2023-04-30T21:00:00.277Z"
38 * }
39 */
Convert an array of amounts from one currency to another (export from backend code)

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { currencies } from 'wix-ecom.v2';
3
4export const convertCurrency = webMethod(Permissions.Anyone, async (identifiers, amounts) => {
5 try {
6 const convertedAmounts = await currencies.convertCurrency(identifiers, amounts);
7 const firstConvertedAmount = convertedAmounts.amounts[0];
8 const timestamp = convertedAmounts.rateTimestamp;
9
10 return convertedAmounts;
11 } catch (error) {
12 console.error(error);
13 // Handle the error
14 }
15});
16
17const identifiers = {
18 'from': 'USD',
19 'to': 'GBP'
20};
21
22const amounts = [
23 {
24 'decimalPlaces': 2,
25 'value': '1000'
26 },
27 {
28 'decimalPlaces': 2,
29 'value': '20324'
30 }
31];
32
33convertCurrency(identifiers, amounts)
34 .then((convertedAmounts) => {
35 const firstConvertedAmount = convertedAmounts.amounts[0];
36 const timestamp = convertedAmounts.rateTimestamp;
37 });
38
39/* Promise resolves to:
40 * {
41 * "amounts": [
42 * {
43 * "decimalPlaces": 2,
44 * "value": "795"
45 * },
46 * {
47 * "decimalPlaces": 2,
48 * "value": "1616287"
49 * }
50 * ],
51 * "rateTimestamp": "2023-04-30T21:00:00.277Z"
52 * }
53 */
54