Skip to content

Instantly share code, notes, and snippets.

@mike-weiner
Last active July 8, 2021 01:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mike-weiner/24c4d6e3151a33b3af542c7c7ffec63e to your computer and use it in GitHub Desktop.
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.
# 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
@mike-weiner
Copy link
Author

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.

# import required tools
import csv
from datetime import datetime
import json
import os.path
import requests

# 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

# define name of file that will store price data over time
FILENAME = "cryptoprices.csv"

# check for existence of CSV file to store data
file_exists = os.path.isfile(FILENAME)

# if CSV file is being created for the first time -> write the header line
if not (file_exists):
    with open(FILENAME, 'w+') as file:
        file.write("timestamp,crypto,price\n")

# open csv file to append data prices
with open(FILENAME, 'a') as file:

    # open CSV writer to write data row by row
    writer = csv.writer(file, delimiter=',')

    # 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

        # data to write to csv file
        data = [datetime.now(), parsedJSONResponse['data']['base'], parsedJSONResponse['data']['amount']] # print out the crypto name and current price
        
        # write file to data
        writer.writerow(data)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment