The API is currently in beta and we cannot guarantee the stability of the data. Please be aware that the data might be inaccurate or incomplete and therefore should not be used for any critical applications. If you find any issues, please contact us.

Overview

This tutorial guides you through building a real-time cryptocurrency price alert system that monitors prices using the DexPaprika API and sends notifications to your Telegram when price thresholds are met. Perfect for traders and developers who want to stay updated on market movements without constant manual checking.

The complete code for this tutorial is available on GitHub.


Features

  • Track any cryptocurrency available on DexPaprika API
  • Set custom price thresholds for buy/sell opportunities
  • Get instant alerts when prices go above or below your targets
  • Configure check intervals to match your trading strategy
  • Receive notifications directly on Telegram

Prerequisites

  • Node.js (v14 or higher)
  • npm (Node Package Manager)
  • A Telegram account
  • A Telegram Bot (created using BotFather)

Step 1: Create Your Telegram Bot

  1. Open Telegram and search for “BotFather” (@BotFather)
  2. Start a chat and send the command /newbot
  3. Follow the instructions to create your bot
  4. Save the bot token BotFather provides you

Step 2: Get Your Telegram Chat ID

  1. Start a conversation with your new bot
  2. Send any message to your bot
  3. Visit this URL in your browser (replace with your actual token):
https://api.telegram.org/botYOUR_BOT_TOKEN/getUpdates
  1. Find the "chat":{"id":XXXXXXXXX, value in the response - this is your chat ID

Step 3: Set Up the Project

  1. Clone the repository or set up a new project:
git clone https://github.com/coinpaprika/tutorials/tree/main/crypto-alert-bot
# OR
mkdir crypto-alert-bot
cd crypto-alert-bot
npm init -y
  1. Install required dependencies:
npm install axios dotenv node-telegram-bot-api
  1. Create the following files in your project directory:
    • .env (configuration file)
    • index.js (main application)

Step 4: Configure Your Settings

Create a .env file in the project directory with the following parameters:

# Telegram Bot Token (Get this from BotFather on Telegram)
TELEGRAM_BOT_TOKEN=your_telegram_bot_token_here

# Telegram Chat ID (The chat where alerts will be sent)
TELEGRAM_CHAT_ID=your_telegram_chat_id_here

# Cryptocurrency to track (token address)
CRYPTO_TOKEN_ID=So11111111111111111111111111111111111111112
CRYPTO_NETWORK=solana

# Price threshold for alert (in USD)
TARGET_PRICE=135

# Alert type: "above" or "below" - to trigger when price goes above or below target
ALERT_TYPE=above

# How often to check price (in minutes)
CHECK_INTERVAL=1

Make sure to replace placeholder values with your actual configuration details.


Step 5: Create the Alert Bot

Create an index.js file with the following code:

expandable
require('dotenv').config();
const axios = require('axios');
const TelegramBot = require('node-telegram-bot-api');

// Configuration from .env file
const TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN;
const TELEGRAM_CHAT_ID = process.env.TELEGRAM_CHAT_ID;
const CRYPTO_TOKEN_ID = process.env.CRYPTO_TOKEN_ID;
const CRYPTO_NETWORK = process.env.CRYPTO_NETWORK;
const TARGET_PRICE = parseFloat(process.env.TARGET_PRICE);
const ALERT_TYPE = process.env.ALERT_TYPE.toLowerCase();
const CHECK_INTERVAL = parseInt(process.env.CHECK_INTERVAL) * 60 * 1000; // Convert minutes to milliseconds

// Initialize Telegram Bot
const bot = new TelegramBot(TELEGRAM_BOT_TOKEN);

// Validate configuration
if (!TELEGRAM_BOT_TOKEN || !TELEGRAM_CHAT_ID || !CRYPTO_TOKEN_ID || !CRYPTO_NETWORK || 
    isNaN(TARGET_PRICE) || !ALERT_TYPE || isNaN(CHECK_INTERVAL)) {
  console.error('Invalid configuration. Please check your .env file.');
  process.exit(1);
}

// Send startup message
bot.sendMessage(TELEGRAM_CHAT_ID, 
  `🤖 Crypto Alert Bot Started!\n\n` +
  `Monitoring: ${CRYPTO_TOKEN_ID} on ${CRYPTO_NETWORK}\n` +
  `Alert when price goes ${ALERT_TYPE} $${TARGET_PRICE}\n` +
  `Checking every ${CHECK_INTERVAL / 60000} minute(s)`
);

// Variables to track alert state
let alertSent = false;
let lastPrice = 0;

// Main function to check price and send alerts
async function checkPrice() {
  try {
    // Fetch current price from DexPaprika API
    const response = await axios.get(
      `https://api.dexpaprika.com/networks/${CRYPTO_NETWORK}/tokens/${CRYPTO_TOKEN_ID}`
    );

    // Extract price from response
    const currentPrice = response.data.summary.price_usd;
    lastPrice = currentPrice;
    
    console.log(`Current price of ${CRYPTO_TOKEN_ID}: $${currentPrice}`);
    
    // Check if alert condition is met
    let shouldAlert = false;
    
    if (ALERT_TYPE === 'above' && currentPrice > TARGET_PRICE) {
      shouldAlert = true;
    } else if (ALERT_TYPE === 'below' && currentPrice < TARGET_PRICE) {
      shouldAlert = true;
    }
    
    // Send alert if condition is met and no alert was sent before
    if (shouldAlert && !alertSent) {
      const message = 
        `🚨 PRICE ALERT 🚨\n\n` +
        `${response.data.name} (${response.data.symbol})\n` +
        `Current Price: $${currentPrice}\n` +
        `Target: ${ALERT_TYPE} $${TARGET_PRICE}\n\n` +
        `Condition met! 🎯`;
      
      bot.sendMessage(TELEGRAM_CHAT_ID, message);
      alertSent = true;
      console.log('Alert sent!');
    }
    
    // Reset alert flag if price goes back on the other side of the threshold
    if ((ALERT_TYPE === 'above' && currentPrice < TARGET_PRICE) || 
        (ALERT_TYPE === 'below' && currentPrice > TARGET_PRICE)) {
      alertSent = false;
    }
    
  } catch (error) {
    console.error('Error checking price:', error.message);
    
    // Send error notification if API fails
    if (error.response) {
      bot.sendMessage(TELEGRAM_CHAT_ID, 
        `⚠️ Error checking price: ${error.response.status} - ${error.response.statusText}`);
    } else {
      bot.sendMessage(TELEGRAM_CHAT_ID, 
        `⚠️ Error checking price: ${error.message}`);
    }
  }
}

// Run the price check immediately
checkPrice();

// Then set up interval to check regularly
setInterval(checkPrice, CHECK_INTERVAL);

console.log(`Bot is running. Checking ${CRYPTO_TOKEN_ID} every ${CHECK_INTERVAL / 60000} minute(s).`);

Step 6: Finding the Right Token Address

Need to track a different token? Use DexPaprika API to find its address:

  1. List available networks:
curl -X GET "https://api.dexpaprika.com/networks" | jq
  1. Search for your token:
curl -X GET "https://api.dexpaprika.com/search?query=YOUR_TOKEN_NAME" | jq

Step 7: Running the Bot

  1. Start the bot:
node index.js
  1. You’ll receive a confirmation message on Telegram.
  2. The bot will check prices at your specified interval
  3. When your price condition is met, you’ll get an alert

Running as a Background Service

On Linux/Mac:

npm install -g pm2
pm2 start index.js --name crypto-alert
pm2 save

On Windows:

npm install -g forever
forever start index.js

How It Works

  1. The application connects to DexPaprika API to retrieve real-time token prices
  2. It compares the current price against your target threshold
  3. When the condition is met, it sends an immediate alert via the Telegram Bot API
  4. The process repeats based on your configured check interval

Troubleshooting

  • Not receiving messages? Double-check your bot token and chat ID
  • Ensure your network/token combination is valid in DexPaprika
  • Check console output for any error messages

Next Steps

Add Multiple Alerts

Extend the code to monitor multiple tokens or set different thresholds.

Create a Web Dashboard

Build a visual interface to manage your alerts and view price history.

Share Your Extensions! Built something cool by extending this tutorial? We’d love to see it! Share your work on our Discord - your tutorial might be featured on our website. Ideas to try: smart trend alerts, multi-token tracking, or historical data analysis.


Get Support