Getting Currency Exchange Rate via API [SOLVED]

Hi Devs,

I went through the ‘Accessing 3rd Party Service’ Documentation and I saw that in your example for getting an exchange rate, you have hard-coded the Base Currency to ‘USD’

This is my Page Code:

import {getCurrency} from 'backend/ArenaApi';

export function button1_click(event, $w) {
	getCurrency($w("#currencysymbol").value)
       .then(rates => $w("#currencyrate").text = rates);
        $w("#currencyrate").show();
}

& This is my Backend:

import {fetch} from 'wix-fetch'; 

export function getCurrency(currency) {
	const url = 'https://api.fixer.io/latest?base=USD'; //base currency hardcoded
    
    let fullUrl = url + '&symbols=' + currency;
  
    return fetch(fullUrl, {method: 'get'})
      .then(response => response.json())
      .then(json => json.rates[currency].toString());
}

Now here is my question…
I want to be able to send the Base currency from my side too

Can I try something like the below on the Backend:

import {fetch} from 'wix-fetch'; 

export function getCurrency(currency) {
	const url = 'https://api.fixer.io/latest?base=';
    
    let fullUrl = url + (BASE CURRENCY) + '&symbols=' + currency; // base currency
  
    return fetch(fullUrl, {method: 'get'})
      .then(response => response.json())
      .then(json => json.rates[currency].toString());
}

How should I go about in getting the Base Currency $w(“#basecurrency”).value from the Page ?

Thanks :slight_smile:

Add another parameter to your function like getCurrency(currency,baseCurrency) and use that in your code and it should work.

Thanks Andreas.

Just a question, How will the backend module recognize what is what on the page code ?

My Current Page Code is like this

import {getCurrency} from 'backend/ArenaApi';

export function button1_click(event, $w) {
	getCurrency($w("#currencysymbol").value)
         .then(rates => $w("#currencyrate").text = rates);
          $w("#currencyrate").show();
}

Should I enter something like this

import {getCurrency} from 'backend/ArenaApi';

export function button1_click(event, $w) {
	getCurrency($w("#currencysymbol").value)
        getCurrency($w("#basecurrency").value)
          .then(rates => $w("#currencyrate").text = rates);
          $w("#currencyrate").show();
}

Any ideas ?

getCurrency($w(" #currencysymbol “).value,$w(” #basecurrency ").value)

So you send two values into the function and you get them by adding another parameter as I wrote in the earlier post. Ok?

Got it!!
Thanks Andreas!

1 Like