When using calculated properties we have access to a dated_exchange_rate function.How can I replicate that functionallity in a nodejs script?
Hi @ajinvise, I hope that you are well!
Great question, thanks for asking the HubSpot Community!
I have found for you this similar post “[SOLVED] dated_exchange_rate formula for calculated property” that might help you!
Let us know if that did the trick for you!
Have a beautiful day! ![]()
Warmly,
Bérangère
Thank you @BérangèreL , though that is in the UI, Im looking for the equivalent when not in the UI, but in code ![]()
Hi @ajinvise, thanks for the clarification!
I’d love to invite our Top Experts to this discussion: Hi @stefen, @miljkovicmisa and @Teun do you have tips to help @ajinvise use the “dated_exchange_rate” in nodejs script, please?
Have a great day and thanks so much for your help! ![]()
Warmly,
Bérangère
Hi @Anton, @Upvise and @zach_threadint, I hope that you are well!
Do you have any tips to share with @ajinvise to use the “dated_exchange_rate” in nodejs script, please?
Have a lovely day and thanks so much for your valuable contributions!
Bérangère
Hi @ajinvise
Great question! The dated_exchange_rate function in HubSpot’s calculated properties lets you fetch historical exchange rates for currency conversion based on a specific date. There’s no public API directly named dated_exchange_rate, but you can replicate the logic in Node.js like this:
Option 1: Use a 3rd-party exchange rate API
You’ll need a service that supports historical rates. Examples:
- ExchangeRate.host (free, no API key required)
- Open Exchange Rates
- CurrencyStack
- Fixer.io
const axios = require('axios');
async function getHistoricalExchangeRate(date, fromCurrency, toCurrency) {
const url = `https://api.exchangerate.host/${date}?base=${fromCurrency}&symbols=${toCurrency}`;
const response = await axios.get(url);
return response.data.rates[toCurrency];
}
// Example usage
getHistoricalExchangeRate('2023-04-10', 'USD', 'EUR')
.then(rate => {
console.log(`Exchange rate on 2023-04-10: ${rate}`);
})
.catch(console.error);
Then multiply your amount:
const amountInUSD = 100;
const convertedAmount = amountInUSD * rate;
Option 2: Use HubSpot workflows or Operations Hub custom code action
If you want to run it inside HubSpot (e.g. using a workflow with a custom code action), you can do the same logic in a @hubspot/api-client script and call an external exchange rate API in there.