Last active
July 8, 2021 01:22
-
-
Save mike-weiner/24c4d6e3151a33b3af542c7c7ffec63e to your computer and use it in GitHub Desktop.
A Python script that uses the Coinbase API to get the current spot price of specified cryptocurrencies.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# import required tools | |
import requests | |
from datetime import datetime | |
# print current date and time | |
print(datetime.now()) | |
print() | |
# define array of cryptos to get the spot price of | |
CRYPTO = ["BTC-USD", "ETH-USD", "LTC-USD"] # Change this array to set what crypto currencies you want a spot price of | |
# for each cryptocurrency defined in CRYPTO | |
for cryptoItem in CRYPTO: | |
URL = "https://api.coinbase.com/v2/prices/" + cryptoItem + "/spot" # generate the URL for the current crypto to make an API call | |
requestResponse = requests.get(URL) # make the GET API call to Coinbase and store the response | |
parsedJSONResponse = requestResponse.json() # parse the JSON data into an array | |
print(parsedJSONResponse['data']['base'] + " : $" + parsedJSONResponse['data']['amount']) # print out the crypto name and current price |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script below will take the prices pulled from the Coinbase API and write it out to a CSV file titled
cryptoprices.csv
to the same directory where the python script is saved.