Fetch live and historical exchange rates in Ruby. Use net/http and json from the standard library to build a currency converter in minutes.
Fetch your first exchange rate using Ruby's standard library. No gems required.
require 'net/http'
require 'json'
uri = URI("https://exchange-rateapi.com/api/v1/rates?source=USD&target=EUR")
req = Net::HTTP::Get.new(uri)
req["Authorization"] = "Bearer era_live_YOUR_API_KEY"
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http|
http.request(req)
}
data = JSON.parse(res.body)
puts "1 USD = #{data[0]['rate']} EUR"
# Output: 1 USD = 0.9215 EUR
era_live_. The free plan includes 300 requests per month.
Here is a production-ready example with a reusable client class, error handling, and support for multiple targets.
require 'net/http'
require 'json'
require 'uri'
class ExchangeRateClient
BASE_URL = "https://exchange-rateapi.com/api/v1/rates"
def initialize(api_key)
@api_key = api_key
end
def get_rates(source, target)
uri = URI("#{BASE_URL}?source=#{source}&target=#{target}")
req = Net::HTTP::Get.new(uri)
req["Authorization"] = "Bearer #{@api_key}"
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true,
open_timeout: 5, read_timeout: 10) { |http|
http.request(req)
}
case res.code.to_i
when 200
JSON.parse(res.body)
when 401
raise "Invalid API key. Check your Authorization header."
when 429
raise "Rate limit exceeded. Try again later."
else
raise "HTTP #{res.code}: #{res.message}"
end
end
end
# Usage
client = ExchangeRateClient.new("era_live_YOUR_API_KEY")
# Single pair
rates = client.get_rates("USD", "EUR")
puts "1 USD = #{rates[0]['rate']} EUR"
# Multiple targets
rates = client.get_rates("USD", "EUR,GBP,JPY")
rates.each do |r|
puts "1 USD = #{r['rate']} #{r['target']}"
end
# Output:
# 1 USD = 0.9215 EUR
# 1 USD = 0.7891 GBP
# 1 USD = 151.42 JPY
gem install httparty
require 'httparty'
response = HTTParty.get(
"https://exchange-rateapi.com/api/v1/rates",
query: { source: "USD", target: "EUR" },
headers: { "Authorization" => "Bearer era_live_YOUR_API_KEY" }
)
data = response.parsed_response
puts "1 USD = #{data[0]['rate']} EUR"
For a quick one-off script, you can call the API in just a few lines.
require 'net/http'
require 'json'
uri = URI("https://exchange-rateapi.com/api/v1/rates?source=USD&target=EUR")
req = Net::HTTP::Get.new(uri)
req["Authorization"] = "Bearer era_live_YOUR_API_KEY"
res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http|
http.request(req)
}
data = JSON.parse(res.body)
puts data
[
{
"rate": 0.9215,
"source": "USD",
"target": "EUR",
"time": "2026-05-15T12:00:00Z"
}
]
[
{ "rate": 0.9215, "source": "USD", "target": "EUR", "time": "2026-05-15T12:00:00Z" },
{ "rate": 0.7891, "source": "USD", "target": "GBP", "time": "2026-05-15T12:00:00Z" },
{ "rate": 151.42, "source": "USD", "target": "JPY", "time": "2026-05-15T12:00:00Z" }
]
net/http for HTTP requests and json for JSON parsing. No external gems required.
Get your free API key and start fetching exchange rates in Ruby in under a minute.
Get Free API Key →