If you need to get pump fun token prices in real time using javascript this is your post.
This guide will let you collect token prices (one or more) for using for your trading bot, telegram bot, or wherever you need.
We’ll walk you through:
1. Where to get a Pump Fun API and how to set up your API key.
2. How to select the specific token you want to track in the code.
3. Using an RPC from GS Node to get on-chain Solana data. Is 100% Discord allocated, just ask to our bot for a free RPC to test.
4. A new code snippet that fetches data for multiple tokens simultaneously.
Let’s dive in.
You may need real time prices either for your bots or for a dashboard for your website:
• Automation: Bots and alerts can act on your behalf, especially when you’re away.
• Visualization: Fuel real-time charts or dashboards for your community.
To retrieve real-time price data, you need an API. For Pump Fun, consider these these steps:
Example of an API Endpoint
1 2 3 4 5 6 7 8 9 10 11 |
GET https://api.pumpfun.io/v1/price Headers: Authorization: Bearer <YOUR_API_KEY> Response: { "token": "PumpFun", "price_usd": "0.0000123", "price_change_24h": "2.37%", "timestamp": "1675647381" } |
An API (like the Pump Fun API) gives you near real-time market data, including price, percentage change, and possibly order book data.
You’ll also want to query the blockchain itself for:
• Token balances
• Account states
• Recent transactions
For truly real-time streaming data, consider:
• WebSockets that push data without constant polling.
• gRPC solutions (premium or specialized), which provide minimal overhead and continuous streaming.
1. Install Node.js (latest LTS recommended).
2. Create a project folder (e.g., pump-fun-tracker) and open it in VS Code.
3. Initialize with NPM:
1 |
npm init -y |
4. Install Dependencies:
1 |
npm install @solana/web3.js axios dotenv |
We added dotenv for secure handling of your API key.
Below is an updated code snippet showing how to fetch the price for one specific token (we will use a fictional token called “Pump Fun”) from the Pump Fun API and how to query the Solana network via GS Node’s RPC.
Note: Ensure you have your API key (e.g., PUMP_FUN_API_KEY) stored in a .env file for security.
File: .env
1 |
PUMP_FUN_API_KEY=YOUR_ACTUAL_API_KEY |
File: index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
require('dotenv').config(); // Loads env variables from .env const { Connection } = require('@solana/web3.js'); const axios = require('axios'); // ------------------------------ // 1. Define the endpoints // ------------------------------ const GS_NODE_RPC = 'https://rpc.gsnode.io'; // Replace with real GS Node RPC const PUMP_FUN_API_URL = 'https://api.pumpfun.io/v1/price'; // Hypothetical endpoint // ------------------------------ // 2. Create a Solana connection // ------------------------------ const connection = new Connection(GS_NODE_RPC); // ------------------------------ // 3. Fetch single token price // ------------------------------ async function getPumpFunPrice(tokenSymbol = 'PumpFun') { try { const response = await axios.get(PUMP_FUN_API_URL, { headers: { Authorization: `Bearer ${process.env.PUMP_FUN_API_KEY}` }, params: { token: tokenSymbol } }); if (response.data) { console.log(`Token: ${response.data.token}`); console.log(`Current Price (USD): ${response.data.price_usd}`); console.log(`24h Change: ${response.data.price_change_24h}`); console.log(`Timestamp: ${response.data.timestamp}`); } else { console.error('No valid data returned by the API.'); } } catch (error) { console.error('Error fetching token price:', error.message); } } // ------------------------------ // 4. Check on-chain data // ------------------------------ async function checkOnChainData() { try { const slot = await connection.getSlot(); console.log('Current Solana slot:', slot); const epochInfo = await connection.getEpochInfo(); console.log('Epoch Info:', epochInfo); // Additional queries: token account, supply, etc. // ... } catch (error) { console.error('Error querying Solana RPC:', error.message); } } // ------------------------------ // 5. Main function // ------------------------------ async function main() { // Pass your desired token symbol here if needed await getPumpFunPrice('PumpFun'); await checkOnChainData(); } // Execute main(); |
How it works:
What if you want to track a group of tokens in one go? You can loop through an array of token symbols or IDs, send concurrent requests, and handle them together.
File: multipleTokens.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
require('dotenv').config(); const axios = require('axios'); // ------------------------------ // 1. Endpoints & tokens list // ------------------------------ const PUMP_FUN_API_URL = 'https://api.pumpfun.io/v1/price'; const tokensToTrack = ['PumpFun', 'HyperMoon', 'StarDoge']; // Replace these with real token symbols as per your API's documentation // ------------------------------ // 2. Fetch prices for multiple tokens // ------------------------------ async function getMultipleTokenPrices(tokens = []) { try { const requests = tokens.map(token => { return axios.get(PUMP_FUN_API_URL, { headers: { Authorization: `Bearer ${process.env.PUMP_FUN_API_KEY}` }, params: { token: token } }); }); // Execute all requests concurrently const responses = await Promise.all(requests); // Handle responses responses.forEach((res, index) => { if (res.data) { console.log(`\n--- ${tokens[index]} ---`); console.log(`Price (USD): ${res.data.price_usd}`); console.log(`24h Change: ${res.data.price_change_24h}`); console.log(`Timestamp: ${res.data.timestamp}`); } else { console.log(`No valid data for token: ${tokens[index]}`); } }); } catch (error) { console.error('Error fetching multiple token prices:', error.message); } } // Run the function getMultipleTokenPrices(tokensToTrack); |
How it works:
1. Manage Rate Limits: If you’re calling the API too frequently, you might get throttled. Consider caching or using WebSockets/gRPC if high frequency is needed.
2. Use WebSockets or gRPC: For truly live data, you’ll want a push-based approach rather than repeated polling.
3. Set Up Alerts: With Telegram, Discord, or Slack integrations, you’ll receive instant notifications when a token hits your target price.
4. Choose a Fast RPC: Low-latency services like GS Node ensure your on-chain queries don’t lag, which is crucial for rapid trading.
Go to bitquery (example URL) and sign up for a developer account. Generate an API key in your dashboard.
Use a .env file with libraries like dotenv in Node.js. This avoids committing sensitive data to a public repository.
Some APIs allow batching requests by passing multiple token symbols in a single call. If not, you can send parallel requests as shown in the multipleTokens.js example.
Polling APIs may not be enough if you need sub-second updates. Consider a WebSocket or gRPC solution for streaming data.
For basic or hobby projects, yes. For high-frequency or large-scale trading, you’ll need professional-grade infrastructure, robust error handling, and advanced features like gRPC or dedicated RPC nodes.
Engineer. CEO of GS Node. Marketing Manager at Smithii.