|
| 1 | +# API Reference |
| 2 | + |
| 3 | +Complete API documentation for interacting with XDC Network. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +XDC Network provides multiple APIs for developers to interact with the blockchain: |
| 8 | + |
| 9 | +| API Type | Use Case | Documentation | |
| 10 | +|----------|----------|---------------| |
| 11 | +| **JSON-RPC** | Standard blockchain queries | [JSON-RPC Reference](json-rpc.md) | |
| 12 | +| **WebSocket** | Real-time subscriptions | [WebSocket API](websocket.md) | |
| 13 | +| **GraphQL** | Complex queries (via indexers) | Coming Soon | |
| 14 | + |
| 15 | +## Quick Start |
| 16 | + |
| 17 | +### Connect to XDC Network |
| 18 | + |
| 19 | +=== "JavaScript (Web3.js)" |
| 20 | + ```javascript |
| 21 | + const Web3 = require('web3'); |
| 22 | + |
| 23 | + // Mainnet |
| 24 | + const web3 = new Web3('https://rpc.xinfin.network'); |
| 25 | + |
| 26 | + // Testnet (Apothem) |
| 27 | + const web3Testnet = new Web3('https://rpc.apothem.network'); |
| 28 | + ``` |
| 29 | + |
| 30 | +=== "JavaScript (Ethers.js)" |
| 31 | + ```javascript |
| 32 | + const { ethers } = require('ethers'); |
| 33 | + |
| 34 | + // Mainnet |
| 35 | + const provider = new ethers.JsonRpcProvider('https://rpc.xinfin.network'); |
| 36 | + |
| 37 | + // Testnet |
| 38 | + const testnetProvider = new ethers.JsonRpcProvider('https://rpc.apothem.network'); |
| 39 | + ``` |
| 40 | + |
| 41 | +=== "Python" |
| 42 | + ```python |
| 43 | + from web3 import Web3 |
| 44 | + |
| 45 | + # Mainnet |
| 46 | + w3 = Web3(Web3.HTTPProvider('https://rpc.xinfin.network')) |
| 47 | + |
| 48 | + # Testnet |
| 49 | + w3_testnet = Web3(Web3.HTTPProvider('https://rpc.apothem.network')) |
| 50 | + ``` |
| 51 | + |
| 52 | +=== "cURL" |
| 53 | + ```bash |
| 54 | + curl -X POST https://rpc.xinfin.network \ |
| 55 | + -H "Content-Type: application/json" \ |
| 56 | + -d '{"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1}' |
| 57 | + ``` |
| 58 | + |
| 59 | +## RPC Endpoints |
| 60 | + |
| 61 | +### Mainnet |
| 62 | + |
| 63 | +| Provider | Endpoint | Rate Limit | |
| 64 | +|----------|----------|------------| |
| 65 | +| XDC Official | `https://rpc.xinfin.network` | Public | |
| 66 | +| XDC Official | `https://rpc1.xinfin.network` | Public | |
| 67 | +| Ankr | `https://rpc.ankr.com/xdc` | Tiered | |
| 68 | +| BlocksScan | `https://rpc.xdc.blocksscan.io` | Public | |
| 69 | + |
| 70 | +### Apothem Testnet |
| 71 | + |
| 72 | +| Provider | Endpoint | Rate Limit | |
| 73 | +|----------|----------|------------| |
| 74 | +| XDC Official | `https://rpc.apothem.network` | Public | |
| 75 | +| BlocksScan | `https://rpc.apothem.blocksscan.io` | Public | |
| 76 | + |
| 77 | +### WebSocket Endpoints |
| 78 | + |
| 79 | +| Network | Endpoint | |
| 80 | +|---------|----------| |
| 81 | +| Mainnet | `wss://ws.xinfin.network` | |
| 82 | +| Apothem | `wss://ws.apothem.network` | |
| 83 | + |
| 84 | +## Common Queries |
| 85 | + |
| 86 | +### Get Block Number |
| 87 | + |
| 88 | +```javascript |
| 89 | +const blockNumber = await web3.eth.getBlockNumber(); |
| 90 | +console.log('Current block:', blockNumber); |
| 91 | +``` |
| 92 | + |
| 93 | +### Get Balance |
| 94 | + |
| 95 | +```javascript |
| 96 | +const balance = await web3.eth.getBalance('xdc71C765...'); |
| 97 | +console.log('Balance:', web3.utils.fromWei(balance, 'ether'), 'XDC'); |
| 98 | +``` |
| 99 | + |
| 100 | +### Get Transaction |
| 101 | + |
| 102 | +```javascript |
| 103 | +const tx = await web3.eth.getTransaction('0x123...'); |
| 104 | +console.log('Transaction:', tx); |
| 105 | +``` |
| 106 | + |
| 107 | +### Send Transaction |
| 108 | + |
| 109 | +```javascript |
| 110 | +const tx = await web3.eth.sendTransaction({ |
| 111 | + from: '0x...', |
| 112 | + to: '0x...', |
| 113 | + value: web3.utils.toWei('1', 'ether'), |
| 114 | + gas: 21000, |
| 115 | + gasPrice: web3.utils.toWei('0.25', 'gwei') |
| 116 | +}); |
| 117 | +``` |
| 118 | + |
| 119 | +## Address Format |
| 120 | + |
| 121 | +XDC Network supports two address formats: |
| 122 | + |
| 123 | +| Format | Example | Use Case | |
| 124 | +|--------|---------|----------| |
| 125 | +| **0x format** | `0x71C7656EC7ab88b098defB751B7401B5f6d8976F` | Standard EVM format | |
| 126 | +| **xdc format** | `xdc71C7656EC7ab88b098defB751B7401B5f6d8976F` | XDC-specific display | |
| 127 | + |
| 128 | +Both formats represent the same address. Convert between them: |
| 129 | + |
| 130 | +```javascript |
| 131 | +// xdc to 0x |
| 132 | +const ethFormat = xdcAddress.replace('xdc', '0x'); |
| 133 | + |
| 134 | +// 0x to xdc |
| 135 | +const xdcFormat = ethAddress.replace('0x', 'xdc'); |
| 136 | +``` |
| 137 | + |
| 138 | +## Error Handling |
| 139 | + |
| 140 | +Common error codes and their meanings: |
| 141 | + |
| 142 | +| Code | Message | Description | |
| 143 | +|------|---------|-------------| |
| 144 | +| -32700 | Parse error | Invalid JSON | |
| 145 | +| -32600 | Invalid request | Malformed request | |
| 146 | +| -32601 | Method not found | Unknown method | |
| 147 | +| -32602 | Invalid params | Invalid parameters | |
| 148 | +| -32603 | Internal error | Server error | |
| 149 | +| -32000 | Server error | Generic server error | |
| 150 | + |
| 151 | +See [Error Codes](error-codes.md) for complete reference. |
| 152 | + |
| 153 | +## Rate Limiting |
| 154 | + |
| 155 | +Public RPC endpoints have rate limits to ensure fair usage: |
| 156 | + |
| 157 | +| Endpoint Type | Limit | |
| 158 | +|--------------|-------| |
| 159 | +| Public | ~100 requests/second | |
| 160 | +| Paid/Premium | Custom | |
| 161 | + |
| 162 | +For high-volume applications: |
| 163 | +- Run your own node |
| 164 | +- Use premium RPC providers (Ankr, etc.) |
| 165 | +- Implement request batching |
| 166 | + |
| 167 | +## SDKs & Libraries |
| 168 | + |
| 169 | +| Language | Library | Install | |
| 170 | +|----------|---------|---------| |
| 171 | +| JavaScript | xdc3.js | `npm install xdc3` | |
| 172 | +| JavaScript | web3.js | `npm install web3` | |
| 173 | +| JavaScript | ethers.js | `npm install ethers` | |
| 174 | +| Python | web3.py | `pip install web3` | |
| 175 | +| Go | go-ethereum | Native support | |
| 176 | +| Rust | ethers-rs | `cargo add ethers` | |
| 177 | + |
| 178 | +## Next Steps |
| 179 | + |
| 180 | +- [JSON-RPC Reference](json-rpc.md) - Complete method documentation |
| 181 | +- [WebSocket API](websocket.md) - Real-time subscriptions |
| 182 | +- [Error Codes](error-codes.md) - Error handling guide |
0 commit comments