|
| 1 | +import { createMetadata } from "@doc"; |
| 2 | + |
| 3 | +export const metadata = createMetadata({ |
| 4 | + title: "thirdweb Insight For Agents & LLMs", |
| 5 | + description: |
| 6 | + "thirdweb Insight query documentation formatted for use with LLMs and agents", |
| 7 | + image: { |
| 8 | + title: "Insight", |
| 9 | + icon: "insight", |
| 10 | + }, |
| 11 | +}); |
| 12 | + |
| 13 | +# Thirdweb Insight |
| 14 | +Insight is a powerful tool that lets you retrieve blockchain data from any EVM chain, enrich it with metadata, and transform it using custom logic. Whether you're building a gaming inventory system, tracking DeFi metrics, or analyzing NFT collections, Insight makes it easy to get the data you need with simple API calls. |
| 15 | + |
| 16 | +## Things to Keep in Mind |
| 17 | + |
| 18 | +- **Rate Limiting**: The API has rate limits based on your authentication tier. Monitor your usage and implement appropriate retry logic. |
| 19 | + |
| 20 | +- **Pagination**: When retrieving large datasets, use pagination parameters (`page` and `limit`) to avoid timeouts and manage response sizes effectively. |
| 21 | + |
| 22 | +- **Chain Selection**: Always verify you're querying the correct chain ID. Using an incorrect chain ID will return a 404 error. |
| 23 | + |
| 24 | +- **Data Freshness**: There may be a slight delay between on-chain events and their availability in the API due to block confirmation times. |
| 25 | + |
| 26 | +- **Error Handling**: Implement proper error handling for both HTTP errors (400/500) and network issues. Consider implementing retries for transient failures. |
| 27 | + |
| 28 | +- **Query Optimization**: |
| 29 | + - Use specific filters to reduce response size and improve performance |
| 30 | + - Avoid overly broad date ranges when possible |
| 31 | + - Consider using aggregations for large datasets |
| 32 | + |
| 33 | +- **Authentication**: Keep your authentication credentials secure and don't expose them in client-side code. |
| 34 | + |
| 35 | +- **Response Processing**: Some numeric values are returned as strings to maintain precision. Convert them appropriately in your application. |
| 36 | + |
| 37 | + |
| 38 | +## API URL |
| 39 | + |
| 40 | +```typescript |
| 41 | +const baseUrl = `https://${chainId}.insight.thirdweb.com`; |
| 42 | +``` |
| 43 | + |
| 44 | +## Authentication |
| 45 | + |
| 46 | +The API supports three authentication methods: |
| 47 | + |
| 48 | +```typescript |
| 49 | +// 1. Header Authentication |
| 50 | +const headers = { |
| 51 | + "x-client-id": "YOUR_CLIENT_ID", // thirdweb Client ID |
| 52 | +}; |
| 53 | + |
| 54 | +// 2. Query Parameter |
| 55 | +const url = `https://${chainId}.insight.thirdweb.com/v1/events?clientId=YOUR_CLIENT_ID`; |
| 56 | + |
| 57 | +// 3. Bearer Token |
| 58 | +const headers = { |
| 59 | + Authorization: "Bearer YOUR_JWT_TOKEN", |
| 60 | +}; |
| 61 | + |
| 62 | +// Example using fetch with header auth |
| 63 | +async function getEvents() { |
| 64 | + const response = await fetch(`https://${chainId}.insight.thirdweb.com/v1/events`, { |
| 65 | + headers: { |
| 66 | + "x-client-id": "YOUR_CLIENT_ID", |
| 67 | + }, |
| 68 | + }); |
| 69 | + return await response.json(); |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## Core Concepts |
| 74 | + |
| 75 | +### Chain IDs |
| 76 | + |
| 77 | +The API supports the following chain IDs (with 1 as default): |
| 78 | + |
| 79 | +```typescript |
| 80 | +type ChainId = string; // Chain ID of the network you want to query |
| 81 | + |
| 82 | +// Example: Using a specific chain |
| 83 | +const chainId = "<chainId>"; // Replace with your desired chain ID |
| 84 | +const baseUrl = `https://${chainId}.insight.thirdweb.com`; |
| 85 | +``` |
| 86 | + |
| 87 | +### Base Response Structure |
| 88 | + |
| 89 | +```typescript |
| 90 | +interface BaseResponse<T> { |
| 91 | + data: T[]; |
| 92 | + meta: { |
| 93 | + chain_id: number; // Required |
| 94 | + page: number; // Required |
| 95 | + limit: number; // Required |
| 96 | + total_items: number; // Required |
| 97 | + total_pages: number; // Required |
| 98 | + address?: string; // Optional |
| 99 | + signature?: string; // Optional |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// Example response from getting events |
| 104 | +{ |
| 105 | + "data": [ |
| 106 | + { |
| 107 | + "chain_id": 1, |
| 108 | + "block_number": "17859301", |
| 109 | + "transaction_hash": "0x123...", |
| 110 | + "address": "0x456...", |
| 111 | + "data": "0x789...", |
| 112 | + "topics": ["0xabc..."] |
| 113 | + } |
| 114 | + ], |
| 115 | + "meta": { |
| 116 | + "chain_id": 1, |
| 117 | + "page": 0, |
| 118 | + "limit": 20, |
| 119 | + "total_items": 150, |
| 120 | + "total_pages": 8 |
| 121 | + } |
| 122 | +} |
| 123 | +``` |
| 124 | + |
| 125 | +## API Examples |
| 126 | + |
| 127 | +### Events API |
| 128 | + |
| 129 | +```typescript |
| 130 | +// 1. Get All Events |
| 131 | +async function getAllEvents() { |
| 132 | + const response = await fetch(`https://${chainId}.insight.thirdweb.com/v1/events`, { |
| 133 | + headers: { "x-client-id": "YOUR_CLIENT_ID" }, |
| 134 | + }); |
| 135 | + return await response.json(); |
| 136 | +} |
| 137 | + |
| 138 | +// 2. Get Contract Events with Filtering |
| 139 | +async function getContractEvents(contractAddress: string) { |
| 140 | + const params = new URLSearchParams({ |
| 141 | + filter_block_number_gte: "17000000", |
| 142 | + sort_by: "block_timestamp", |
| 143 | + sort_order: "desc", |
| 144 | + limit: "50", |
| 145 | + }); |
| 146 | + |
| 147 | + const url = `https://${chainId}.insight.thirdweb.com/v1/events/${contractAddress}?${params}`; |
| 148 | + const response = await fetch(url, { |
| 149 | + headers: { "x-client-id": "YOUR_CLIENT_ID" }, |
| 150 | + }); |
| 151 | + return await response.json(); |
| 152 | +} |
| 153 | +``` |
| 154 | + |
| 155 | +### Token Balance API |
| 156 | + |
| 157 | +```typescript |
| 158 | +// 1. Get ERC20 Balances |
| 159 | +async function getERC20Balances(ownerAddress: string) { |
| 160 | + const response = await fetch( |
| 161 | + `https://${chainId}.insight.thirdweb.com/v1/tokens/erc20/${ownerAddress}`, |
| 162 | + { headers: { "x-client-id": "YOUR_CLIENT_ID" } }, |
| 163 | + ); |
| 164 | + const data = await response.json(); |
| 165 | + // Example response: |
| 166 | + // { |
| 167 | + // "data": [ |
| 168 | + // { |
| 169 | + // "tokenAddress": "0x123...", |
| 170 | + // "balance": "1000000000000000000" |
| 171 | + // } |
| 172 | + // ] |
| 173 | + // } |
| 174 | + return data; |
| 175 | +} |
| 176 | + |
| 177 | +// 2. Get NFT Balances |
| 178 | +async function getNFTBalances(ownerAddress: string) { |
| 179 | + const response = await fetch( |
| 180 | + `https://${chainId}.insight.thirdweb.com/v1/tokens/erc721/${ownerAddress}`, |
| 181 | + { headers: { "x-client-id": "YOUR_CLIENT_ID" } }, |
| 182 | + ); |
| 183 | + const data = await response.json(); |
| 184 | + // Example response: |
| 185 | + // { |
| 186 | + // "data": [ |
| 187 | + // { |
| 188 | + // "collectionAddress": "0x456...", |
| 189 | + // "tokenId": "1", |
| 190 | + // "balance": "1" |
| 191 | + // } |
| 192 | + // ] |
| 193 | + // } |
| 194 | + return data; |
| 195 | +} |
| 196 | +``` |
| 197 | + |
| 198 | +### Using Filters |
| 199 | + |
| 200 | +```typescript |
| 201 | +// Example: Get events with complex filtering |
| 202 | +async function getFilteredEvents() { |
| 203 | + const params = new URLSearchParams({ |
| 204 | + // Block filters |
| 205 | + filter_block_number_gte: "17000000", |
| 206 | + filter_block_number_lte: "17859301", |
| 207 | + |
| 208 | + // Time filters |
| 209 | + filter_block_timestamp_gte: "1715222400", |
| 210 | + |
| 211 | + // Transaction filters |
| 212 | + filter_from_address: "0x123...", |
| 213 | + filter_value_gte: "1000000000000000000", // 1 ETH |
| 214 | + |
| 215 | + // Pagination |
| 216 | + page: "0", |
| 217 | + limit: "20", |
| 218 | + |
| 219 | + // Sorting |
| 220 | + sort_by: "block_timestamp", |
| 221 | + sort_order: "desc", |
| 222 | + }); |
| 223 | + |
| 224 | + const response = await fetch( |
| 225 | + `https://${chainId}.insight.thirdweb.com/v1/events?${params}`, |
| 226 | + { headers: { "x-client-id": "YOUR_CLIENT_ID" } }, |
| 227 | + ); |
| 228 | + return await response.json(); |
| 229 | +} |
| 230 | +``` |
| 231 | + |
| 232 | +### Error Handling |
| 233 | + |
| 234 | +```typescript |
| 235 | +async function safeApiCall() { |
| 236 | + try { |
| 237 | + const response = await fetch(`https://${chainId}.insight.thirdweb.com/v1/events`, { |
| 238 | + headers: { "x-client-id": "YOUR_CLIENT_ID" }, |
| 239 | + }); |
| 240 | + |
| 241 | + if (!response.ok) { |
| 242 | + const errorData = await response.json(); |
| 243 | + // Example error response: |
| 244 | + // { "error": "Invalid client ID" } |
| 245 | + throw new Error(errorData.error); |
| 246 | + } |
| 247 | + |
| 248 | + return await response.json(); |
| 249 | + } catch (error) { |
| 250 | + console.error("API Error:", error.message); |
| 251 | + throw error; |
| 252 | + } |
| 253 | +} |
| 254 | +``` |
| 255 | + |
| 256 | +## API Reference |
| 257 | + |
| 258 | +### Events API |
| 259 | + |
| 260 | +1. **Get All Events** |
| 261 | + |
| 262 | +```typescript |
| 263 | +GET /v1/events |
| 264 | +``` |
| 265 | + |
| 266 | +2. **Get Contract Events** |
| 267 | + |
| 268 | +```typescript |
| 269 | +GET /v1/events/:contractAddress |
| 270 | +``` |
| 271 | + |
| 272 | +3. **Get Specific Event Type** |
| 273 | + |
| 274 | +```typescript |
| 275 | +GET /v1/events/:contractAddress/:signature |
| 276 | +``` |
| 277 | + |
| 278 | +### Transactions API |
| 279 | + |
| 280 | +1. **Get All Transactions** |
| 281 | + |
| 282 | +```typescript |
| 283 | +GET /v1/transactions |
| 284 | +GET /v1/transactions; |
| 285 | +``` |
| 286 | + |
| 287 | +2. **Get Contract Transactions** |
| 288 | + |
| 289 | +```typescript |
| 290 | +GET /v1/transactions/:contractAddress |
| 291 | +``` |
| 292 | + |
| 293 | +3. **Get Specific Transaction Type** |
| 294 | + |
| 295 | +```typescript |
| 296 | +GET /v1/transactions/:contractAddress/:signature |
| 297 | +``` |
| 298 | + |
| 299 | +### Token Balance API |
| 300 | + |
| 301 | +1. **ERC20 Balances** |
| 302 | + |
| 303 | +```typescript |
| 304 | +GET /v1/tokens/erc20/:ownerAddress |
| 305 | + |
| 306 | +interface ERC20Response { |
| 307 | + data: { |
| 308 | + tokenAddress: string; // Required |
| 309 | + balance: string; // Required |
| 310 | + }[]; |
| 311 | +} |
| 312 | +``` |
| 313 | + |
| 314 | +2. **ERC721 & ERC1155 Balances** |
| 315 | + |
| 316 | +```typescript |
| 317 | +GET /v1/tokens/erc721/:ownerAddress |
| 318 | +GET /v1/tokens/erc1155/:ownerAddress |
| 319 | + |
| 320 | +interface TokenBalance { |
| 321 | + data: { |
| 322 | + collectionAddress: string; // Required |
| 323 | + tokenId: string; // Required |
| 324 | + balance: string; // Required |
| 325 | + }[]; |
| 326 | +} |
| 327 | +``` |
| 328 | + |
| 329 | +## Query Parameters |
| 330 | + |
| 331 | +### Common Parameters |
| 332 | + |
| 333 | +```typescript |
| 334 | +interface CommonQueryParams { |
| 335 | + page?: number; // Default: 0 |
| 336 | + limit?: number; // Default: 20, must be > 0 |
| 337 | + sort_by?: "block_number" | "block_timestamp" | "transaction_index"; |
| 338 | + sort_order?: "asc" | "desc"; |
| 339 | + group_by?: string; |
| 340 | + aggregate?: string[]; |
| 341 | +} |
| 342 | +``` |
| 343 | + |
| 344 | +### Filter Types |
| 345 | + |
| 346 | +1. **Block Filters** |
| 347 | + |
| 348 | +```typescript |
| 349 | +interface BlockFilters { |
| 350 | + filter_block_number?: number; // Example: 1000000 |
| 351 | + filter_block_number_gte?: number; // Example: 1000000 |
| 352 | + filter_block_number_gt?: number; // Example: 1000000 |
| 353 | + filter_block_number_lte?: number; // Example: 1000000 |
| 354 | + filter_block_number_lt?: number; // Example: 1000000 |
| 355 | + filter_block_hash?: string; // Example: "0x3a1fba5..." |
| 356 | +} |
| 357 | +``` |
| 358 | + |
| 359 | +2. **Time Filters** |
| 360 | + |
| 361 | +```typescript |
| 362 | +interface TimeFilters { |
| 363 | + filter_block_timestamp?: number; // Example: 1715222400 |
| 364 | + filter_block_timestamp_gte?: number; // Example: 1715222400 |
| 365 | + filter_block_timestamp_gt?: number; // Example: 1715222400 |
| 366 | + filter_block_timestamp_lte?: number; // Example: 1715222400 |
| 367 | + filter_block_timestamp_lt?: number; // Example: 1715222400 |
| 368 | +} |
| 369 | +``` |
| 370 | + |
| 371 | +3. **Transaction Filters** |
| 372 | + |
| 373 | +```typescript |
| 374 | +interface TransactionFilters { |
| 375 | + filter_transaction_index?: number; |
| 376 | + filter_transaction_hash?: string; |
| 377 | + filter_from_address?: string; |
| 378 | + filter_value?: number; |
| 379 | + filter_gas_price?: number; |
| 380 | + filter_gas?: number; |
| 381 | + // Additional gte, gt, lte, lt variants for numeric fields |
| 382 | +} |
| 383 | +``` |
| 384 | + |
| 385 | +## Error Handling |
| 386 | + |
| 387 | +All endpoints return standard error responses for 400 and 500 status codes: |
| 388 | + |
| 389 | +```typescript |
| 390 | +// 400 Bad Request |
| 391 | +// 500 Internal Server Error |
| 392 | +interface ErrorResponse { |
| 393 | + error: string; // Required |
| 394 | +} |
| 395 | +``` |
0 commit comments