Skip to content

Instantly share code, notes, and snippets.

@mononz
Last active March 11, 2024 13:26
Show Gist options
  • Select an option

  • Save mononz/b7ea3a66db706ae4336abbbe31090899 to your computer and use it in GitHub Desktop.

Select an option

Save mononz/b7ea3a66db706ae4336abbbe31090899 to your computer and use it in GitHub Desktop.
Bitcoin Scheduled Price Notifier
const alerts = require('apialerts-js')
const { CronJob } = require("cron");
// Your Project API Key
const apiAlertsKey = 'YOUR PROJECT API KEY'
// Cron job to notify Bitcoin price every day at 10am
new CronJob('0 10 * * *', notifyBitcoinPrice, null, true, 'Australia/Melbourne')
// Fetch the bitcoin price from the public Coinbase API
async function notifyBitcoinPrice() {
try {
// Fetch the Bitcoin price
const response = await fetch('https://api.coinbase.com/v2/exchange-rates?currency=USD')
if (response.status !== 200) {
return // HTTP Error
}
const body = await response.json()
// Get the BTC price
const btc = body.data.rates.BTC
if (!btc) {
return // Bitcoin price not found
}
// Calculate the price
let price = 1 / parseFloat(btc)
// Format the price as currency string
let formatted = price.toLocaleString('en-US', { style: 'currency', currency: 'USD' })
console.log(`Bitcoin price: ${formatted}`)
// Send notification to your device via API Alerts
alerts.send({
message: `BTC now ${formatted}`,
tags: ['crypto', 'bitcoin'],
link: 'https://coinbase.com',
api_key: apiAlertsKey
})
} catch (error) {
console.error(`Error fetching Bitcoin price: ${error}`);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment