{"id":16250,"library":"tronweb","title":"TronWeb JavaScript SDK","description":"TronWeb is the official JavaScript SDK for interacting with the TRON blockchain, providing a comprehensive encapsulation of the TRON HTTP API. As of version 6.2.2, it continues to be actively maintained with frequent minor and patch releases, incorporating new TRON features and API quality-of-life improvements. Unlike a direct fork of Ethereum's Web3.js, TronWeb is specifically tailored to unlock TRON's unique feature set and offers distinct tools for DApp integration across browsers, Node.js environments (v16+), and IoT devices. It ships with TypeScript types, enhancing developer experience for strongly typed projects. Key features include deterministic contract address computation (CREATE2), robust transaction deserialization, and methods for retrieving real-time witness lists and transaction building parameters.","status":"active","version":"6.2.2","language":"javascript","source_language":"en","source_url":"https://github.com/tronprotocol/tronweb","tags":["javascript","TRON","tronweb","typescript"],"install":[{"cmd":"npm install tronweb","lang":"bash","label":"npm"},{"cmd":"yarn add tronweb","lang":"bash","label":"yarn"},{"cmd":"pnpm add tronweb","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"TronWeb is typically imported as a default export in ESM. The `TronWeb` class is then instantiated.","wrong":"import { TronWeb } from 'tronweb';","symbol":"TronWeb","correct":"import TronWeb from 'tronweb';"},{"note":"For Node.js environments still using CommonJS modules.","symbol":"TronWeb (CommonJS)","correct":"const TronWeb = require('tronweb');"},{"note":"Utility functions like `getCreate2Address` are accessed via the `tronWeb` instance, not directly imported from sub-paths.","wrong":"import { getCreate2Address } from 'tronweb/utils';","symbol":"Utilities (instance method)","correct":"const create2Address = tronWeb.utils.address.getCreate2Address(creatorAddress, salt, bytecodeHash);"}],"quickstart":{"code":"import TronWeb from 'tronweb';\n\n// Replace with your actual TRON Grid API key and a test private key for Shasta\n// NEVER use production private keys directly in code. Use environment variables for security.\nconst TRON_GRID_API_KEY = process.env.TRON_GRID_API_KEY ?? 'YOUR_TRONGRID_API_KEY';\nconst TEST_PRIVATE_KEY = process.env.TEST_PRIVATE_KEY ?? 'YOUR_SHASTA_PRIVATE_KEY_HERE'; // e.g., for a test account on Shasta\n\nif (TRON_GRID_API_KEY === 'YOUR_TRONGRID_API_KEY' || TEST_PRIVATE_KEY === 'YOUR_SHASTA_PRIVATE_KEY_HERE') {\n  console.warn('Please provide a valid TRON_GRID_API_KEY and TEST_PRIVATE_KEY in your environment variables or replace placeholders.');\n  // In a real application, you might throw an error or exit.\n}\n\nconst tronWeb = new TronWeb({\n  fullHost: 'https://api.shasta.trongrid.io', // Official Tron testnet endpoint\n  headers: { \"TRON-PRO-API-KEY\": TRON_GRID_API_KEY },\n  privateKey: TEST_PRIVATE_KEY\n});\n\nasync function getAccountInfo() {\n  try {\n    const address = tronWeb.defaultAddress.base58;\n    if (!address) {\n      console.error(\"No default address set. Ensure your private key is valid and connected.\");\n      return;\n    }\n    console.log(`Connected to Shasta Testnet with address: ${address}`);\n\n    const balanceSun = await tronWeb.trx.getBalance(address);\n    const balanceTrx = tronWeb.trx.fromSun(balanceSun);\n\n    console.log(`Account Balance: ${balanceTrx} TRX`);\n\n    // Example of using a new utility method (v6.2.2+)\n    const creatorAddress = 'TXwU1rY5Gj89P4g45v3C6M5427K8K1YJ8M'; // Example creator address\n    const salt = '0x1234567890abcdef1234567890abcdef'; // Example salt\n    const bytecodeHash = '0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'; // Example bytecode hash\n    const create2Address = tronWeb.utils.address.getCreate2Address(creatorAddress, salt, bytecodeHash);\n    console.log(`CREATE2 Address for example: ${create2Address}`);\n\n  } catch (error) {\n    console.error(\"Error fetching account info or using utilities:\", error);\n  }\n}\n\ngetAccountInfo();","lang":"typescript","description":"Initializes TronWeb, connects to the Shasta testnet using environment variables for sensitive keys, fetches the default account's balance, and demonstrates the `getCreate2Address` utility function."},"warnings":[{"fix":"Update your `contract.new()` calls to handle the returned instance instead of relying on mutation. Ensure your ABI definitions use `as const` for TypeScript type inference.","message":"The behavior of `contract.new()` method was changed in v6.0.4. Previously, it mutated the current TronWeb instance; now, it returns a new contract instance and infers method signatures from the provided ABI. ABIs should be defined using `as const` or passed directly to `tronWeb.contract()` for accurate type inference.","severity":"breaking","affected_versions":">=6.0.4"},{"fix":"Ensure your development and deployment environments use Node.js v16 or a newer LTS version.","message":"TronWeb requires Node.js v16 or above. Older Node.js versions are not officially supported and may lead to unexpected behavior or security vulnerabilities.","severity":"gotcha","affected_versions":"all"},{"fix":"Upgrade to TronWeb v6.2.2 or higher to mitigate prototype pollution risks. Regularly update dependencies to address security patches.","message":"Versions prior to v6.2.2 had potential prototype pollution vulnerabilities in `decodeParams` and `encodeArgs`. This was fixed by replacing `{}` with `Object.create(null)` to prevent malicious property injection.","severity":"gotcha","affected_versions":"<6.2.2"},{"fix":"Ensure your connected TRON full node (e.g., TronGrid) is running JAVA-TRON v4.8.1 or higher if you plan to use `trx.getNowWitnessList`.","message":"The `trx.getNowWitnessList` method, introduced in v6.2.0, requires the underlying JAVA-TRON full node to be running version v4.8.1 or above. Using it with older node versions will result in errors.","severity":"gotcha","affected_versions":">=6.2.0"},{"fix":"If your application had an indirect dependency on `jsonwebtoken` via TronWeb, ensure you manage that dependency directly if it's still required for other parts of your application. Always audit your dependency tree.","message":"The `jsonwebtoken` dependency was removed in v6.1.1 due to audit issues. While this improves the package's security posture, applications directly relying on `jsonwebtoken` through TronWeb might need to adjust.","severity":"deprecated","affected_versions":">=6.1.1"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"For ESM, use `import TronWeb from 'tronweb';`. For CommonJS, use `const TronWeb = require('tronweb');`. Ensure your project's module system is correctly configured.","cause":"Attempting to instantiate TronWeb using incorrect import syntax (e.g., CommonJS `require` for an ESM module, or `import { TronWeb } from 'tronweb'` instead of `import TronWeb from 'tronweb'`).","error":"TypeError: TronWeb is not a constructor"},{"fix":"Double-check your private key for typos, ensure it's a valid hexadecimal string, and confirm it's associated with the correct network (e.g., Shasta testnet key for Shasta).","cause":"The private key passed to the TronWeb constructor is not in a valid format or is incorrect for the network being connected to.","error":"Error: Invalid private key provided"},{"fix":"Verify your `fullHost` URL is correct and accessible. Ensure `TRON-PRO-API-KEY` is provided and valid if connecting to TronGrid. Check your internet connection and any firewall settings.","cause":"TronWeb instance failed to establish a connection to the specified full node. This can be due to network issues, an invalid `fullHost` URL, or missing/incorrect `TRON-PRO-API-KEY` headers for services like TronGrid.","error":"Error: Failed to connect to fullHost: https://api.trongrid.io"},{"fix":"Ensure the TRON full node you are connected to (e.g., your local quickstart instance or a public gateway) is running JAVA-TRON v4.8.1 or a newer compatible version.","cause":"Attempting to use a TronWeb method (e.g., `trx.getNowWitnessList`) that relies on a specific TRON full node feature only available in newer JAVA-TRON versions, while connected to an older node.","error":"Error: This feature requires JAVA-TRON v4.8.1 or above"}],"ecosystem":"npm"}