├── LICENSE ├── README.md ├── package.json ├── server.js └── vercel.json /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 ikhsan0x 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pancakeswap-api 2 | PancakeSwap GraphQL token data price API on BSC 3 | 4 | Page source 5 | 6 | https://nodereal.io/api-marketplace/pancakeswap-graphql 7 | 8 | # USECASE 9 | 10 | ### GET Method 11 | ```URL 12 | https://pancakeswap-token-api.vercel.app/token-price?token=0xbA2aE424d960c26247Dd6c32edC70B295c744C43&apikey=YOUR_APIKEY 13 | ``` 14 | 15 | ### POST Method 16 | ```URL 17 | https://pancakeswap-token-api.vercel.app/token-price 18 | ``` 19 | 20 | ```JSON 21 | { 22 | "token": "0xbA2aE424d960c26247Dd6c32edC70B295c744C43", 23 | "apikey": "YOUR_APIKEY" 24 | } 25 | ``` 26 | 27 | ### Response 28 | ```JSON 29 | { 30 | "id": "0xba2ae424d960c26247dd6c32edc70b295c744c43-19859", 31 | "date": 1715817600, 32 | "dailyVolumeToken": "234947.86821497", 33 | "dailyVolumeBNB": "62.30027505589203895992151332573449", 34 | "dailyVolumeUSD": "36389.0788977555014831540787666163", 35 | "dailyTxns": "283", 36 | "totalLiquidityToken": "5506128.35321168", 37 | "totalLiquidityBNB": "1461.867620792842557232993158764851", 38 | "totalLiquidityUSD": "851381.724247936961858068829352331", 39 | "priceUSD": "0.1546243875247355810244826672969983" 40 | } 41 | ``` 42 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "dependencies": { 3 | "axios": "^1.6.8", 4 | "body-parser": "^1.20.2", 5 | "express": "^4.19.2", 6 | "memory-cache": "^0.2.0" 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /server.js: -------------------------------------------------------------------------------- 1 | const express = require("express"); 2 | const bodyParser = require("body-parser"); 3 | const axios = require("axios"); 4 | const cache = require('memory-cache'); 5 | 6 | const HTTP_PORT = 3000; 7 | const app = express(); 8 | app.disable('x-powered-by'); 9 | app.use(bodyParser.urlencoded({ 10 | extended: false 11 | })); 12 | app.use(bodyParser.json()); 13 | app.use(function(req, res, next) { 14 | res.setHeader('Access-Control-Allow-Origin', '*'); 15 | res.setHeader('Content-Type', 'application/json'); 16 | res.set('Cache-Control', 'public, max-age=3600'); 17 | next(); 18 | }); 19 | 20 | const cacheMiddleware = (duration) => { 21 | return (req, res, next) => { 22 | const key = '__express__' + req.originalUrl || req.url; 23 | const cachedBody = cache.get(key); 24 | if (cachedBody) { 25 | res.send(cachedBody); 26 | return; 27 | } else { 28 | res.sendResponse = res.send; 29 | res.send = (body) => { 30 | cache.put(key, body, duration * 1000); 31 | res.sendResponse(body); 32 | }; 33 | next(); 34 | } 35 | }; 36 | }; 37 | 38 | async function fetchPriceUSD(apikey, tokenAddress) { 39 | const GraphqlUrl = `https://open-platform.nodereal.io/${apikey}/pancakeswap-free/graphql`; 40 | const query = ` 41 | { 42 | tokenDayDatas( 43 | first: 1, 44 | orderBy: date, 45 | orderDirection: desc, 46 | where: { 47 | token: "${tokenAddress}" 48 | } 49 | ) { 50 | id 51 | date 52 | dailyVolumeToken 53 | dailyVolumeBNB 54 | dailyVolumeUSD 55 | dailyTxns 56 | totalLiquidityToken 57 | totalLiquidityBNB 58 | totalLiquidityUSD 59 | priceUSD 60 | } 61 | } 62 | `; 63 | try { 64 | const response = await axios.post(GraphqlUrl, { 65 | query 66 | }); 67 | return response.data.data.tokenDayDatas[0]; 68 | } catch (error) { 69 | return error; 70 | } 71 | } 72 | 73 | app.get("/token-price", cacheMiddleware(60), async (req, res) => { 74 | try { 75 | let token = req.query.token; 76 | let apikey = req.query.apikey; 77 | let getData = await fetchPriceUSD(apikey, token); 78 | res.json(getData); 79 | } catch (error) { 80 | res.status(500).json({ 81 | error: error.message 82 | }); 83 | } 84 | }); 85 | 86 | app.post("/token-price", cacheMiddleware(60), async (req, res) => { 87 | try { 88 | let token = req.body.token; 89 | let apikey = req.body.apikey; 90 | let getData = await fetchPriceUSD(apikey, token); 91 | res.json(getData); 92 | } catch (error) { 93 | res.status(500).json({ 94 | error: error.message 95 | }); 96 | } 97 | }); 98 | 99 | app.get("/", (req, res, next) => { 100 | res.json({ 101 | "status": "API is running" 102 | }) 103 | }); 104 | 105 | app.listen(HTTP_PORT, () => { 106 | console.log("Server running on port %PORT%".replace("%PORT%", HTTP_PORT)); 107 | }); 108 | -------------------------------------------------------------------------------- /vercel.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 2, 3 | "builds": [ 4 | { 5 | "src": "server.js", 6 | "use": "@vercel/node" 7 | } 8 | ], 9 | "routes": [ 10 | { 11 | "src": "/(.*)", 12 | "dest": "/server.js" 13 | } 14 | ] 15 | } 16 | --------------------------------------------------------------------------------