Free Ruby Exchange Rate API

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.

With UnirateAPI, you get:

  • Real-time exchange rates from reliable sources
  • Simple RESTful API endpoints
  • Support for 590+ currencies
  • Completely free with no credit card required
  • 99.9% uptime guarantee
  • Fast response times (avg. 50ms)
  • Automatic currency updates every hour

Using our API with Ruby is:

  • Simple to implement with built-in Net::HTTP or gems like HTTParty
  • Seamless to integrate with any Ruby framework (Rails, Sinatra, etc.)
  • Perfect for both synchronous and asynchronous applications
  • Easy to combine with Ruby's JSON parsing capabilities
  • Ready for production use with proper error handling

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.

Getting Started with ruby

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

Response Example


# 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...

Important Notes

  • Replace 'YOUR_API_KEY' with your actual API key
  • For Rails applications, consider adding these functions to a service object
  • Net::HTTP is built into Ruby's standard library, so no external gems are required
  • HTTParty simplifies HTTP requests and is recommended for complex applications
  • For production use, implement caching to reduce API calls
  • Use environment variables like ENV['UNIRATE_API_KEY'] to store your API key securely
  • Add proper error handling and retries for production applications

Ready to Get Started?

Sign up now to get your API key and start converting currencies in your ruby applications.

Get Your API Key