Welcome to the UnirateAPI Ruby documentation! Our free exchange rate API provides real-time currency conversion capabilities that you can easily integrate into your Ruby applications. Whether you're building a Rails application, a Sinatra web service, or any Ruby script that needs currency conversion, our API has you covered.
UnirateAPI is designed to be developer-friendly and reliable, with a focus on simplicity and performance. This guide will show you how to make API requests using Ruby's built-in libraries as well as popular gems.
For more detailed information about our API endpoints, rate limits, and advanced features, please check our main API documentation.
Below you'll find examples showing how to use our API with Ruby using different approaches.
You can use our API in Ruby by making HTTP requests using either the built-in Net::HTTP library or popular gems like HTTParty. We'll show you both approaches below.
# Option 1: Using Ruby's built-in Net::HTTP
require 'net/http'
require 'uri'
require 'json'
class CurrencyConverter
BASE_URL = 'https://api.unirateapi.com/api'
def initialize(api_key)
@api_key = api_key
end
def convert(from_currency, to_currency, amount)
uri = URI("#{BASE_URL}/convert")
params = {
api_key: @api_key,
from: from_currency,
to: to_currency,
amount: amount
}
uri.query = URI.encode_www_form(params)
response = Net::HTTP.get_response(uri)
if response.is_a?(Net::HTTPSuccess)
JSON.parse(response.body)
else
raise "API request failed with status #{response.code}: #{response.message}"
end
end
def get_rate(from_currency, to_currency)
uri = URI("#{BASE_URL}/rates")
params = {
api_key: @api_key,
base: from_currency,
symbols: to_currency
}
uri.query = URI.encode_www_form(params)
response = Net::HTTP.get_response(uri)
if response.is_a?(Net::HTTPSuccess)
data = JSON.parse(response.body)
data['rates'][to_currency]
else
raise "API request failed with status #{response.code}: #{response.message}"
end
end
def get_currencies
uri = URI("#{BASE_URL}/currencies")
params = { api_key: @api_key }
uri.query = URI.encode_www_form(params)
response = Net::HTTP.get_response(uri)
if response.is_a?(Net::HTTPSuccess)
JSON.parse(response.body)
else
raise "API request failed with status #{response.code}: #{response.message}"
end
end
end
# Example usage
api_key = 'YOUR_API_KEY'
converter = CurrencyConverter.new(api_key)
begin
# Convert 100 USD to EUR
result = converter.convert('USD', 'EUR', 100)
puts "Converted amount: #{result['result']} #{result['to']}"
# Get the exchange rate from USD to EUR
rate = converter.get_rate('USD', 'EUR')
puts "1 USD = #{rate} EUR"
# Get list of available currencies
currencies = converter.get_currencies
puts "Available currencies: #{currencies.keys.first(5).join(', ')}..."
rescue => e
puts "Error: #{e.message}"
end
# Option 2: Using the HTTParty gem
# First, install the gem: gem install httparty
require 'httparty'
require 'json'
class UnirateApiClient
include HTTParty
base_uri 'https://api.unirateapi.com/api'
def initialize(api_key)
@api_key = api_key
end
def convert(from_currency, to_currency, amount)
options = {
query: {
api_key: @api_key,
from: from_currency,
to: to_currency,
amount: amount
}
}
response = self.class.get('/convert', options)
if response.success?
response.parsed_response
else
raise "API request failed with status #{response.code}: #{response.message}"
end
end
def get_rate(from_currency, to_currency)
options = {
query: {
api_key: @api_key,
base: from_currency,
symbols: to_currency
}
}
response = self.class.get('/rates', options)
if response.success?
response.parsed_response['rates'][to_currency]
else
raise "API request failed with status #{response.code}: #{response.message}"
end
end
end
# Example usage with HTTParty
client = UnirateApiClient.new('YOUR_API_KEY')
begin
result = client.convert('USD', 'EUR', 100)
puts "Converted amount: #{result['result']} #{result['to']}"
rate = client.get_rate('USD', 'EUR')
puts "1 USD = #{rate} EUR"
rescue => e
puts "Error: #{e.message}"
end
# API Response:
{
"from": "USD",
"to": "EUR",
"amount": 100,
"result": 92.45
}
# Console Output:
Converted amount: 92.45 EUR
1 USD = 0.9245 EUR
Available currencies: USD, EUR, GBP, JPY, AUD...
Sign up now to get your API key and start converting currencies in your ruby applications.
Get Your API Key