Site icon About Monero Coin

How to call a Monero Price API

I will show you how to use a simple Python script to call an API and get the current price of Monero in US dollars.

First, you need to find an API that provides the data you need. There are many options available, but for this example, I will use the CoinGecko API, which is free and easy to use. You can find the documentation here.

To get the price of Monero, you need to make a GET request to this endpoint.

This will return a JSON object with the following format:

{
"monero": {
"usd": 469.03
}
}

The value of “usd” is the current price of Monero in US dollars.

To make the request in Python, you need to import the requests library, which is a standard module for working with HTTP requests. You can install it with pip if you don’t have it already:

pip install requests

Then, you can write a simple function that takes the endpoint as an argument and returns the price of Monero:

import requests
def get_monero_price(endpoint):
response = requests.get(endpoint)
data = response.json()
price = data["monero"]["usd"]
return price

Now, you can call the function with the endpoint and print the result:

endpoint = "https://api.coingecko.com/api/v3/simple/price?ids=monero&vs_currencies=usd"
price = get_monero_price(endpoint)
print(f"The current price of Monero is ${price}")

This should output something like:

The current price of Monero is $469.03

Of course, you can modify the function to get the price in other currencies or for other cryptocurrencies. You can also use other APIs that offer more features or data. The CoinGecko API has many other endpoints that you can explore in the documentation.

Exit mobile version