Skip to content

Instantly share code, notes, and snippets.

@StevenTso
Created June 17, 2022 16:47
Show Gist options
  • Save StevenTso/557306aa74a40ef1ec5a75856e54aa74 to your computer and use it in GitHub Desktop.
Save StevenTso/557306aa74a40ef1ec5a75856e54aa74 to your computer and use it in GitHub Desktop.
Get gold price in Python
import requests
# get price in USD
url = 'https://api.metalpriceapi.com/v1/latest?api_key=[API_KEY]&base=USD&currencies=XAU'
r = requests.get(url)
gold_dict = r.json()
rate = gold_dict["rates"]["XAU"]
gold_price = 1/rate
print(gold_price)
# get price is MXN
url = 'https://api.metalpriceapi.com/v1/latest?api_key=[API_KEY]&base=MXN&currencies=XAU'
r = requests.get(url)
gold_dict = r.json()
rate = gold_dict["rates"]["XAU"]
gold_price = 1/rate
print(gold_price)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment