Convert a USD Price to EUR in Ruby

Cloudmersive
2 min readMay 24, 2021

Thanks to the extensive reach of the internet across borders and cultures, online global commerce is booming. One of the steps you can take to provide a better customer experience for international users is by integrating a currency conversion tool into your website. The following API can be used in Ruby to automatically convert the price in the source currency (such as USD) into the destination currency (such as EUR) by referencing the latest available currency exchange rate data.

To begin, we will add the Ruby client to your Gemfile:

gem 'cloudmersive-currency-api-client', '~> 1.7.8'

Next, we can call the function with the following code:

# load the gem
require 'cloudmersive-currency-api-client'
# setup authorization
CloudmersiveCurrencyApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
end
api_instance = CloudmersiveCurrencyApiClient::CurrencyExchangeApi.newsource = 'source_example' # String | Source currency three-digit code (ISO 4217), e.g. USD, EUR, etc.destination = 'destination_example' # String | Destination currency three-digit code (ISO 4217), e.g. USD, EUR, etc.source_price = 1.2 # Float | Input price, such as 19.99 in source currencybegin
#Converts a price from the source currency into the destination currency
result = api_instance.currency_exchange_convert_currency(source, destination, source_price)
p result
rescue CloudmersiveCurrencyApiClient::ApiError => e
puts "Exception when calling CurrencyExchangeApi->currency_exchange_convert_currency: #{e}"
end

This process supports a wide range of countries, and the returned response will indicate the converted price, ISO currency code, and the currency symbol. To retrieve your API key, head to the Cloudmersive website to register for a free account; this will provide 800 monthly calls across any of our APIs.

--

--

Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.