|
| 1 | +--- |
| 2 | +title: Token API |
| 3 | +image: /img/socialCards/token-api.jpg |
| 4 | +--- |
| 5 | + |
| 6 | +The Token API provides comprehensive programmatic access to token data on the Linea network. |
| 7 | +This API is designed for developers, builders and analysts who need detailed information about |
| 8 | +ERC-20 tokens and associated activity on Linea. |
| 9 | + |
| 10 | +:::warning[Alpha Version Disclaimer] |
| 11 | +Linea's Token API is an alpha version and is subject to breaking changes. |
| 12 | +We recommend using it for testing and development purposes only. |
| 13 | +We are looking for your feedback on this new service—please reach out to us on [Discord](https://discord.com/invite/linea) |
| 14 | +in the [#developer-chat](https://discord.com/channels/1141419161893998702/1141419163223593024) |
| 15 | +channel. |
| 16 | +::: |
| 17 | + |
| 18 | +The Token API's key uses cases include: |
| 19 | + |
| 20 | +- Building automated trading bots |
| 21 | +- Creating token monitoring dashboards |
| 22 | +- Performing onchain data analysis |
| 23 | +- DeFi application integration |
| 24 | +- Wallet and transaction tracking |
| 25 | + |
| 26 | +## Data sources |
| 27 | + |
| 28 | +### Data collection |
| 29 | + |
| 30 | +Data is collected from multiple sources: |
| 31 | + |
| 32 | +1. **Onchain data** |
| 33 | + - Smart contract state (name, symbol, decimals) |
| 34 | + |
| 35 | +2. **External sources** |
| 36 | + - [CoinGecko](https://www.coingecko.com/en/api) |
| 37 | + - [MetaMask Token API](https://docs.cx.metamask.io/docs/token-v2/) |
| 38 | + - [MetaMask Price API](https://docs.cx.metamask.io/docs/price/) |
| 39 | + - [Pond.fun](https://pond.fun/) |
| 40 | + - [Dune Analytics](https://dune.com/) |
| 41 | + |
| 42 | +### Data updates |
| 43 | + |
| 44 | +The API data updates at various intervals, depending on the data type: |
| 45 | + |
| 46 | +- Token detection and metadata: every two hours |
| 47 | +- Historical prices: every hour |
| 48 | +- Current prices: every five minutes |
| 49 | + |
| 50 | +## API endpoints |
| 51 | + |
| 52 | +### Tokens |
| 53 | + |
| 54 | +These endpoints provide information about tokens on Linea when available, including their metadata, |
| 55 | +prices, and trading activity. |
| 56 | + |
| 57 | +#### Get `/api/tokens` |
| 58 | + |
| 59 | +Retrieve a list of all available tokens on Linea. |
| 60 | + |
| 61 | +<details> |
| 62 | + <summary>**Response**</summary> |
| 63 | + |
| 64 | + ```json |
| 65 | + [ |
| 66 | + { |
| 67 | + "name": "Wrapped Ether", |
| 68 | + "symbol": "WETH", |
| 69 | + "decimals": 18, |
| 70 | + "logo": "https://s2.coinmarketcap.com/static/img/coins/64x64/2396.png", |
| 71 | + "contractAddress": "0xe5d7c2a44ffddf6b295a15c148167daaaf5cf34f", |
| 72 | + "currentPrice": "3321.74000000000000", |
| 73 | + "priceUpdatedAt": "2025-01-09T09:36:02.194Z", |
| 74 | + "info": { |
| 75 | + "prices": [ |
| 76 | + { |
| 77 | + "price": "3321.74000000000000", |
| 78 | + "date": "2025-01-09T09:00:00.000Z" |
| 79 | + } |
| 80 | + ], |
| 81 | + "sells": 3279, |
| 82 | + "buys": 3722, |
| 83 | + "graduatedAt": null |
| 84 | + } |
| 85 | + } |
| 86 | + ] |
| 87 | + ``` |
| 88 | + |
| 89 | +</details> |
| 90 | + |
| 91 | +#### Get `/api/tokens/{contractAddress}` |
| 92 | + |
| 93 | +Retrieve detailed information for a specific token. |
| 94 | + |
| 95 | +**Parameters:** |
| 96 | + |
| 97 | +<table> |
| 98 | + <thead> |
| 99 | + <tr> |
| 100 | + <th>Parameter</th> |
| 101 | + <th>Type</th> |
| 102 | + <th>Required</th> |
| 103 | + <th>Description</th> |
| 104 | + </tr> |
| 105 | + </thead> |
| 106 | + <tbody> |
| 107 | + <tr> |
| 108 | + <td>`contractAddress`</td> |
| 109 | + <td>string</td> |
| 110 | + <td>Yes</td> |
| 111 | + <td>Token address</td> |
| 112 | + </tr> |
| 113 | + </tbody> |
| 114 | +</table> |
| 115 | + |
| 116 | + <details> |
| 117 | + <summary>**Response**</summary> |
| 118 | + |
| 119 | + ```json |
| 120 | + { |
| 121 | + "name": "Wrapped Ether", |
| 122 | + "symbol": "WETH", |
| 123 | + "decimals": 18, |
| 124 | + "logo": "https://s2.coinmarketcap.com/static/img/coins/64x64/2396.png", |
| 125 | + "contractAddress": "0xe5d7c2a44ffddf6b295a15c148167daaaf5cf34f", |
| 126 | + "currentPrice": "3321.74000000000000", |
| 127 | + "priceUpdatedAt": "2025-01-09T09:36:02.194Z", |
| 128 | + "info": { |
| 129 | + "prices": [ |
| 130 | + { |
| 131 | + "price": "3321.74000000000000", |
| 132 | + "date": "2025-01-09T09:00:00.000Z" |
| 133 | + } |
| 134 | + ], |
| 135 | + "sells": 3279, |
| 136 | + "buys": 3722, |
| 137 | + "graduatedAt": null |
| 138 | + } |
| 139 | + } |
| 140 | + ``` |
| 141 | + |
| 142 | + </details> |
| 143 | + |
| 144 | +#### Get `/api/tokens/bonded` |
| 145 | + |
| 146 | +Retrieve recently bonded tokens. |
| 147 | + |
| 148 | +<details> |
| 149 | + <summary>**Response**</summary> |
| 150 | + |
| 151 | + ```json |
| 152 | + [ |
| 153 | + { |
| 154 | + "name": "CatWifCap", |
| 155 | + "symbol": "CWC", |
| 156 | + "decimals": 18, |
| 157 | + "logo": "https://d12kvghf2eznx1.cloudfront.net/tokens/images/d0a931cd-7982-47e4-86c7-74be11ab0b09", |
| 158 | + "contractAddress": "0x092b9e25a7d143c83d44c27194f5cee7c1150f22", |
| 159 | + "currentPrice": "0.00013412000000", |
| 160 | + "priceUpdatedAt": "2025-01-09T09:45:04.799Z", |
| 161 | + "info": { |
| 162 | + "prices": [ |
| 163 | + { |
| 164 | + "price": "0.00013412000000", |
| 165 | + "date": "2025-01-09T09:00:00.000Z" |
| 166 | + } |
| 167 | + ], |
| 168 | + "sells": 0, |
| 169 | + "buys": 0, |
| 170 | + "graduatedAt": "2024-12-01T18:19:42.095Z" |
| 171 | + } |
| 172 | + } |
| 173 | + ] |
| 174 | + ``` |
| 175 | + |
| 176 | +</details> |
| 177 | + |
| 178 | +#### Get `/api/tokens/most-swapped` |
| 179 | + |
| 180 | +Retrieve the most swapped tokens over the last 24 hours (from the [corresponding Dune query](https://dune.com/queries/4396527)). |
| 181 | + |
| 182 | + <details> |
| 183 | + <summary>**Response**</summary> |
| 184 | + |
| 185 | + ```json |
| 186 | + [ |
| 187 | + { |
| 188 | + "name": "Foxy", |
| 189 | + "symbol": "FOXY", |
| 190 | + "decimals": 18, |
| 191 | + "logo": "https://i.ibb.co/MSKkFbf/logo.png", |
| 192 | + "contractAddress": "0x5fbdf89403270a1846f5ae7d113a989f850d1566", |
| 193 | + "currentPrice": "0.01080016000000", |
| 194 | + "priceUpdatedAt": "2025-01-09T09:50:05.106Z", |
| 195 | + "info": { |
| 196 | + "prices": [ |
| 197 | + { |
| 198 | + "price": "0.01078276000000", |
| 199 | + "date": "2025-01-09T09:00:00.000Z" |
| 200 | + } |
| 201 | + ], |
| 202 | + "sells": 219, |
| 203 | + "buys": 212, |
| 204 | + "graduatedAt": null |
| 205 | + } |
| 206 | + } |
| 207 | + ] |
| 208 | + ``` |
| 209 | + |
| 210 | +</details> |
| 211 | + |
| 212 | +#### Get `/api/tokens/top-winners` |
| 213 | + |
| 214 | +Retrieve tokens with the highest price increase over the last 24 hours. |
| 215 | + |
| 216 | +<details> |
| 217 | + <summary>**Response**</summary> |
| 218 | + |
| 219 | + ```json |
| 220 | + [ |
| 221 | + { |
| 222 | + "name": "Yellow Duckies", |
| 223 | + "symbol": "DUCKIES", |
| 224 | + "decimals": 8, |
| 225 | + "logo": "https://assets.coingecko.com/coins/images/27630/thumb/duckies_logo.png?1706528164", |
| 226 | + "contractAddress": "0x796000fad0d00b003b9dd8e531ba90cff39e01e0", |
| 227 | + "currentPrice": "0.00662527000000", |
| 228 | + "priceUpdatedAt": "2025-01-10T14:05:02.603Z", |
| 229 | + "last24hVariation": 48.75177091589188, |
| 230 | + "info": { |
| 231 | + "prices": [], |
| 232 | + "sells": 0, |
| 233 | + "buys": 0, |
| 234 | + "graduatedAt": null |
| 235 | + } |
| 236 | + } |
| 237 | + ] |
| 238 | + ``` |
| 239 | + |
| 240 | +</details> |
| 241 | + |
| 242 | +#### Get `/api/tokens/top-losers` |
| 243 | + |
| 244 | +Retrieve tokens with the highest price decrease over the last 24 hours. |
| 245 | + |
| 246 | +<details> |
| 247 | + <summary>**Response**</summary> |
| 248 | + |
| 249 | + ```json |
| 250 | + [ |
| 251 | + { |
| 252 | + "name": "CROAK", |
| 253 | + "symbol": "CROAK", |
| 254 | + "decimals": 18, |
| 255 | + "logo": "https://bafybeiempzjdoflyvrhs6uk3424pij753jus3hz5ztkcri4d3zkyjmyd34.ipfs.dweb.link/logo/coin.png", |
| 256 | + "contractAddress": "0xacb54d07ca167934f57f829bee2cc665e1a5ebef", |
| 257 | + "currentPrice": "0.00146891000000", |
| 258 | + "priceUpdatedAt": "2025-01-10T14:05:03.982Z", |
| 259 | + "last24hVariation": -8.218352482560526, |
| 260 | + "info": { |
| 261 | + "prices": [], |
| 262 | + "sells": 30, |
| 263 | + "buys": 34, |
| 264 | + "graduatedAt": null |
| 265 | + } |
| 266 | + } |
| 267 | + ] |
| 268 | + ``` |
| 269 | + |
| 270 | +</details> |
| 271 | + |
| 272 | +## Usage examples |
| 273 | + |
| 274 | +### Simple token price bot |
| 275 | + |
| 276 | +```typescript |
| 277 | +async function monitorPriceChange(contractAddress: string, threshold: number) { |
| 278 | + const BASE_URL = 'https://not.live.yet'; |
| 279 | + const initialPrice = await fetch(`${BASE_URL}/api/tokens/${contractAddress}`).then(r => r.json()); |
| 280 | + |
| 281 | + setInterval(async () => { |
| 282 | + const currentPrice = await fetch(`${BASE_URL}/api/tokens/${contractAddress}`).then(r => r.json()); |
| 283 | + const priceChange = (currentPrice.price.usd - initialPrice.price.usd) / initialPrice.price.usd; |
| 284 | + |
| 285 | + if (Math.abs(priceChange) > threshold) { |
| 286 | + // Execute trading strategy |
| 287 | + console.log(`Price changed by ${priceChange}% - Trading signal`); |
| 288 | + } |
| 289 | + }, 60000); // Check every minute |
| 290 | +} |
| 291 | +``` |
| 292 | + |
| 293 | +## Best practices |
| 294 | + |
| 295 | +1. **Rate limiting** |
| 296 | + - This is an alpha version with strict rate limits per calling IP: |
| 297 | + - 2 requests per second |
| 298 | + - 60 requests per minute |
| 299 | + - Cache static data |
| 300 | + - Implement backoff strategies when limits are reached |
| 301 | + |
| 302 | +2. **Error handling** |
| 303 | + - Always check HTTP status of responses |
| 304 | + - Implement retry with exponential backoff |
| 305 | + - Validate token addresses before requests |
| 306 | + |
| 307 | +3. **Performance** |
| 308 | + - (Not available yet) Use pagination for large lists |
| 309 | + - Implement local caching when appropriate |
| 310 | + |
| 311 | +## Security considerations |
| 312 | + |
| 313 | +- Validate all input parameters |
| 314 | +- Sanitize response data |
| 315 | + |
| 316 | +## Support and feedback |
| 317 | + |
| 318 | +For technical support or feature requests, |
| 319 | +reach out to us on [Discord](https://discord.com/invite/linea) in the [#developer-chat](https://discord.com/channels/1141419161893998702/1141419163223593024) channel. |
0 commit comments