DEV Community

Germán Alberto Gimenez Silva
Germán Alberto Gimenez Silva

Posted on • Originally published at rubystacknews.com on

Exploring Crypto APIs with Ruby: Fetching Prices & Wallet Data

February 28, 2025

Cryptocurrencies have become an integral part of modern finance, and as developers, we often need to interact with blockchain data—whether for tracking prices, retrieving wallet balances, or processing transactions. In this article, I’ll demonstrate how to use Ruby to fetch and manipulate data from popular crypto APIs.


Need Expert Ruby on Rails Developers to Elevate Your Project?

Fill out our form! >>


Need Expert Ruby on Rails Developers to Elevate Your Project?


1. Setting Up

Before we begin, make sure you have Ruby installed. We’ll use httparty and json gems to fetch and process API responses:

require 'httparty'
require 'json' 
Enter fullscreen mode Exit fullscreen mode

Install the gems if you haven’t already:

gem install httparty json 
Enter fullscreen mode Exit fullscreen mode

2. Fetching Cryptocurrency Prices (CoinGecko API)

To get the latest prices of cryptocurrencies, we’ll use the CoinGecko API :

def fetch_crypto_price(crypto_id = 'bitcoin', currency = 'usd')
  url = "https://api.coingecko.com/api/v3/simple/price?ids=#{crypto_id}&vs_currencies=#{currency}"
  response = HTTParty.get(url)
  data = JSON.parse(response.body)
  puts "The price of #{crypto_id.capitalize} in #{currency.upcase} is $#{data[crypto_id][currency]}"
end

fetch_crypto_price('ethereum', 'usd') 
Enter fullscreen mode Exit fullscreen mode

3. Checking Wallet Balance (Etherscan API)

If you want to track wallet balances on Ethereum, the Etherscan API is a great tool. You need an API key from Etherscan:

ETHERSCAN_API_KEY = 'your_api_key_here'

def fetch_eth_balance(wallet_address)
  url = "https://api.etherscan.io/api?module=account&action=balance&address=#{wallet_address}&tag=latest&apikey=#{ETHERSCAN_API_KEY}"
  response = HTTParty.get(url)
  data = JSON.parse(response.body)
  balance_in_ether = data['result'].to_f / 10**18 # Convert from Wei to Ether
  puts "Wallet Balance: #{balance_in_ether} ETH"
end

fetch_eth_balance('your_wallet_address_here') 
Enter fullscreen mode Exit fullscreen mode

4. Tracking Bitcoin Transactions (Blockchain.info API)

To track Bitcoin transactions, we can use Blockchain.info API :

def fetch_btc_transactions(wallet_address)
  url = "https://blockchain.info/rawaddr/#{wallet_address}"
  response = HTTParty.get(url)
  data = JSON.parse(response.body)
  puts "Recent Transactions for #{wallet_address}"
  data['txs'].first(5).each do |tx|
    puts "TX Hash: #{tx['hash']} - Value: #{tx['out'][0]['value'].to_f / 10**8} BTC"
  end
end

fetch_btc_transactions('your_btc_wallet_address_here') 
Enter fullscreen mode Exit fullscreen mode

5. Manipulating & Analyzing Data

Once we retrieve wallet balances or transactions, we can process them in various ways. For example, let’s calculate the total value of recent Ethereum transactions:

def total_eth_sent(wallet_address)
  url = "https://api.etherscan.io/api?module=account&action=txlist&address=#{wallet_address}&startblock=0&endblock=99999999&sort=desc&apikey=#{ETHERSCAN_API_KEY}"
  response = HTTParty.get(url)
  data = JSON.parse(response.body)
  total_sent = data['result'].map { |tx| tx['value'].to_f / 10**18 }.sum
  puts "Total ETH Sent from #{wallet_address}: #{total_sent} ETH"
end

total_eth_sent('your_wallet_address_here') 
Enter fullscreen mode Exit fullscreen mode

Conclusion

With just a few lines of Ruby, you can interact with crypto APIs to fetch prices, retrieve wallet balances, and analyze transactions. Whether you’re building trading bots, portfolio trackers, or blockchain applications, these APIs provide powerful tools for working with cryptocurrency data.

🚀 What are you building with crypto APIs? Share your thoughts in the comments!

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

If you found this post useful, consider leaving a ❤️ or a nice comment!

Got it