Hello! I am wondering how to get the current price of Ethereum from an API I found. But, my code isn’t working. There isn’t any clear tutorials online on how to do this, so I’m just hoping that I can get some help from here.
Here is my code:
local data = game.HttpService:GetAsync("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd")
local new = game.HttpService:JSONDecode(data)
print(new.ethereum.current_price)
It should print the current Ethereum price, but all I get is “attempt to index nil with ‘current_price’”. Any help would be appreciated!
oh whoops, my bad, thats if you used JSONEncode (and I put it wrong, you have to do the .Data inside of the JSONEncode), but I do believe it has something to do with the JSONDecode.
The JSON returned isn’t a dictionary, it’s an array. You have to find which index contains Ethereum then find the price through that.
local HttpService = game:GetService("HttpService")
local data = HttpService:GetAsync("https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd")
local currencies = HttpService:JSONDecode(data)
local ethereum
for _, currency in ipairs(currencies) do
if currency.id == "ethereum" then
ethereum = currency
break
end
end
print(ethereum.current_price)