The Token Details API demo will walk you through how users can seamlessly retrieve and display token price changes from the past week.
Prerequisites
- Node.js and npm installed
- Basic knowledge of JavaScript, React, and Express.js
Set up environment variables
To securely manage your API key, store it as an environment variable. For macOS:
- Open the Terminal.
- Add
API_KEYvariable to your shell configuration file. Replaceyour_api_keywith your DevPortal API key:Bash1export API_KEY=your_api_key - Save and reload your configuration file:Bash1
source ~/.zshrc # For zsh (default for macOS)
Step 1: Initialization
Create a new directory for the project:
Bash1mkdir token-details && cd token-detailsInitialize a new Node.js project:
Bash1npm init -yInstall Express, CORS, and Axios:
Bash1npm install express cors axiosCreate a new file
api.jsand set up a basic Express server by pasting into it the following:JavaScript123456789101112131415161718192021222324const express = require('express'); const axios = require('axios'); const cors = require('cors'); const path = require('path'); const app = express(); const PORT = 5001; app.use(cors()); // To handle CORS issues when making requests to the front end // Serve static files from the React app app.use(express.static(path.join(__dirname, 'client/build'))); // Add endpoint handler here // We will route all other requests to the client build app.get('*', (req, res) => { res.sendFile(path.join(__dirname, 'client/build', 'index.html')); }); app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });Add an endpoint to fetch token prices:
JavaScript1234567891011121314151617181920212223const BASE_URL = 'https://api.1inch.com/token-details/v1.0/charts/interval'; const CHAIN_ID = 1; // Eth app.get('/api/:tokeAddress/prices/:interval', async (req, res) => { const { tokeAddress, interval } = req.params; try { const constructedUrl = `${BASE_URL}/${CHAIN_ID}/${tokeAddress}?interval=${interval}`; const response = await axios.get(constructedUrl, { headers: { Authorization: `Bearer ${process.env.API_KEY}`, }, }); // Send the data from the API back to the client res.json(response.data.d); } catch (error) { console.error('Axios Error: ', error.response); res.status(500).json({ error: 'Failed to fetch token price by interval' }); } });
Step 2: Setting up React frontend
Create a new React application:
Bash1npx create-react-app clientNavigate to the React application directory and install React Charts:
Bash12cd client npm i react-chartsCreate a component
TokenPrice.jsinside thesrcdirectory with the following content:JavaScript1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071import React, { useState, useEffect } from 'react'; import { Chart } from 'react-charts'; const TokenPrice = ({ address, interval }) => { const [tokenPrices, setTokenPrices] = useState([]); const [isLoading, setLoading] = useState(false); const primaryAxis = React.useMemo( () => ({ getValue: (datum) => datum.date }), [], ); const secondaryAxes = React.useMemo( () => [{ getValue: (datum) => datum.price }], [], ); useEffect(() => { const fetchData = async () => { setLoading(true); const response = await fetch( `http://localhost:5001/api/${address}/prices/${interval}`, ); if (!response.ok) { console.log('Fetch token prices error', response); return; } const prices = await response.json(); setTokenPrices(prices); setLoading(false); }; if (isLoading) { return; } fetchData(); }, [address, interval]); if (isLoading) { return <div>Loading...</div>; } if (tokenPrices.length === 0) { return <div>No data available</div>; } return ( <Chart options={{ data: [ { data: tokenPrices.map((d) => ({ date: new Date(d.t * 1000), price: d.v, })), }, ], primaryAxis, secondaryAxes, }} /> ); }; export default TokenPrice;Import and use
TokenPriceinsrc/App.js:JavaScript1234567891011121314151617181920212223import './App.css'; import TokenPrice from './TokenPrice'; function App() { return ( <div className="App" style={{ width: 800, height: 800, }} > <h1>Token Price</h1> <TokenPrice address="0x111111111117dC0aa78b770fA6A738034120C302" interval="7d" /> </div> ); } export default App;
Step 3: Running the project
- Start the Express server:Bash1
node api.js - In a new terminal, navigate to the client directory and start the React app:Now, you can view your 1inch token price over last week at http://localhost:3000.Bash12
cd client npm start
Described above is a basic setup and you can expand upon this by adding more features, error handling, and styling to get to production.