I’m sure I’m overthinking this, but I just can’t seem to wrap my head around how to use an exchange rate API in a repeat table. The API takes a from_currency and a to_currency. I have these two values but how do I use them in a repeat table to convert each price to the appropriate currency?
I would set the exchange rate in a variable and use the variable to calculate the relevant currency.
e.g
{{price * varExchange.value}}
Thanks Ben. How does that work with a repeat table?
I am not sure what you mean. To me this is straight forward.
Taking the dynamic real estate site as an example, I first add the variable as in
where the value can be a dynamic value retrieved from your API.
I then change line 99
to
<li class="list-group-item bg-dark text-light" dmx-text="(Price * varExchange.value).formatCurrency('€', '.', ',', 2)"></li>
This changes the values to Euros.
I’d use a .where on the client side which takes the currency from the current repeat item to index your exchange rate api result and give the rate.
.where(api_from_currency_code, currency_code, ==).where(api_to_currency_code, base_currency_code.value, ==)[0].exchange_rate
I’ve not used joins on the Server side… could you create a new array with a join here which would be faster?
This would also mean only one call to the exchange rate API. I've noticed the free APIs have limits of around 250 requests per month so my thinking is to call it once a day, store the rate and use that locally throughout the day. It means the rate may not be completely up-to-date but a line saying prices are charged in [local currency] and other currencies are there for illustrative purposes only and are not guaranteed to be accurate.
The API I’m using is - https://exchangerate.host/ - it’s free so no limits (at least that I’ve run up against yet).
I don’t think I explained the issue very well. Each line in the repeat table can have a different from_currency (to_currency is the same for each line every time the table is called, but it can vary as well). So line 1 might be EUR to USD, line 2 CHF to USD, line 3 GBP to USD etc.
I have figured out that I can do one call to get all the rates for a given base currency (USD in example above) which returns all of the currencies. I can’t seem to get the rates though. I thought using <td dmx-text="ExchangeRatesAPI.data.rates.{{currency_code}}"></td>
would work, but it returns the text like ExchangeRatesAPI.data.rates.EUR, not the value (note, <td dmx-text="ExchangeRatesAPI.data.rates.EUR"></td>
does return the correct value)
I like this method as it only requires one call per session as the base currency will be the users home currency so not something that would change often and I’m not needing rates to be up to the minute rates. Even though there aren’t any limits on how many calls I can do, this would seem the most efficient.
Any thoughts on how I can format it to get the data?
Thanks for all the ideas!
Mind blown. Very curious now how you solve this.
Note edit - I forgot to put tags around my code.
I really thought ExchangeRatesAPI.data.rates.{{currency_code}} would work!
Heather, I think you need to use the .where() structure that I mentioned!
Hey Antony. I haven’t used the .where before so a bit confused about how that works. I now have that server connect which is returning all of the currencies for a base currency so ExchangeRatesAPI.data.rates.EUR returns the USD/EUR rate.
I tried dmx-html=“ExchangeRatesAPI.data.where(‘rates’, currency_code, ‘==’)” but that doesn’t return anything, or throw an error, nor does dmx-html=“ExchangeRatesAPI.data.rates.where(currency_code, currency_code, ‘==’)”
Thanks!
Hi Heather!
You should use the GUI to create the .where(). My syntax was not perfect as I’m moving house this week and I’m using my phone as my laptop is in a box somewhere!
After the .where() you need [0].rate… like in my example.
The GUI will create you a .where() with ampersand-quot; in place of the quote signs… (can’t put that code in with my phone as I have no back quote! )
Yikes, I’d go spare if I didn’t know where my laptop was! Hope your move goes smoothly!
The GUI didn’t have a Where option. I did get it to work though!!!
ExchangeRatesAPI.data.rates.getValueOrKey(currency_code)
Yay! Thanks for your help!!
For anyone reading this, here’s the simplified version…
In the API call below, replace sc_current_triptaker.data.query.currency_code with your base currency code, or dynamic reference to it and local1 with the ID of your local storage manager. I’ve set this to cache for 12 hours (43200 seconds).
<dmx-api-datasource id="ExchangeRatesAPI" is="dmx-fetch" dmx-param:base="sc_current_triptaker.data.query.currency_code" url="https://api.exchangerate.host/latest" cache="local1" ttl="43200"></dmx-api-datasource>
Then, in your repeat table, assuming currency_code is in your repeat…
<td dmx-html="ExchangeRatesAPI.data.rates.getValueOrKey(currency_code)"></td>
Voila!