How to Send ERC20 Token with Web3.js?
Last Updated :
26 Apr, 2025
Sending ERC20 tokens is a common task in the world of Ethereum and decentralized applications. ERC20 tokens are a type of digital asset that lives on the Ethereum blockchain and follows a specific set of rules. The article focuses on discussing how to send ERC20 tokens using Web3.js, a popular JavaScript library for interacting with the Ethereum blockchain along with an example code and its explanation.
Prerequisites
- Understanding of Ethereum and ERC20 tokens.
- Web3.js enabled environment: This can be a local development environment or a web-based Ethereum wallet such as MetaMask.
- ERC20 token contract address and ABI (Application Binary Interface): The ABI is a JSON file that contains information about the functions and parameters of the ERC20 contract.
Anatomy of Ethereum Transaction
In Ethereum, a transaction is a message that is sent from one account to another. It contains several pieces of information, including:
- The address of the sender and recipient.
- The amount of Ether or ERC20 tokens to be transferred.
- The gas limit and gas price determine how much the transaction will cost in terms of Ether.
- The data field can be used to encode additional information such as the function call and its parameters.
Approach
Follow the steps below to send ERC2o tokens with Web3.js:
- Set up the web3.js environment and load the ERC20 contract ABI.
- Retrieve the ERC20 contract instance using the contract address.
- Estimate the gas limit and gas price required for the transaction.
- Sign the transaction using the private key of the sender's account.
- Submit the signed transaction to the Ethereum network.
Implementation
To send ERC20 tokens with Web3.js, you can follow the steps below:
1. Import the required modules from Web3.js, 'web3' and 'ERC20'.
const Web3 = require('web3');
const ERC20 = require('web3-eth-erc20');
Here,Â
- const Web3 = require('web3'): This statement is used to import the Web3 library into your JavaScript code. This library provides an interface for interacting with the Ethereum blockchain, and it exports an object that you can use to access various methods and properties of the Web3 library.
- const ERC20 = require('web3-eth-erc20'): This statement is used to import the ERC20 library, which is a specific module within the Web3 library that provides an interface for interacting with ERC-20 tokens on the Ethereum blockchain. This library exports an object that you can use to access various methods and properties related to ERC-20 tokens.
2. Set up the connection to the Ethereum network using the Web3.js instance. You can use the Infura API to connect to the mainnet or testnet of your choice.
const web3 = new Web3('https://mainnet.infura.io/v3/your-api-key');
Here,
const web3 = new Web3('https://mainnet.infura.io/v3/your-api-key'): This statement is used to create a new instance of the Web3 class, which provides an interface for interacting with the Ethereum blockchain. The new Web3() constructor takes a URL as an argument, which specifies the location of an Ethereum node that the Web3 instance will connect to. In this case, the URL provided is for the Infura Ethereum node, which is a publicly accessible Ethereum node that you can use to interact with the Ethereum mainnet (i.e., the main Ethereum network).
3. Once this statement is executed, the web3 variable will contain an instance of the Web3 class that is connected to the Infura Ethereum node. You can then use this instance to perform various operations on the Ethereum blockchain, such as reading and writing data and interacting with smart contracts.
4. Instantiate the ERC20 contract object using the contract ABI and the address of the ERC20 token. You can get the ABI and address from the token's official website or from a third-party contract explorer like Etherscan.
const contract = new ERC20(web3, '0x1234567890abcdefghijklmnopqrstuvwxyz', {
 from: '0x1234567890abcdefghijklmnopqrstuvwxyz',
 gas: 6721975,
});
Here,
const contract = new ERC20(web3, '0x1234567890abcdefghijklmnopqrstuvwxyz', { from: '0x1234567890abcdefghijklmnopqrstuvwxyz', gas: 6721975,}): This statement is used to create a new instance of the ERC20 class, which provides an interface for interacting with ERC-20 tokens on the Ethereum blockchain. The new ERC20() constructor takes two arguments:
- The web3 instance that you created in the previous step, provides the connection to the Ethereum node.
- The address of the ERC-20 token contract on the Ethereum blockchain.
The new ERC20() constructor also takes an optional third argument, which is an object containing various options for configuring the ERC20 instance. In this case, the options object includes the from the property, which specifies the Ethereum address that will be used as the sender of transactions, and the gas property, which specifies the maximum amount of gas (i.e., the fee paid to the Ethereum network to process transactions) that will be used for transactions.
5. Once this statement is executed, the contract variable will contain an instance of the ERC20 class that is connected to the specified ERC-20 token contract on the Ethereum blockchain. One can then use this instance to perform various operations on the ERC-20 token, such as checking the token balance of an Ethereum address or transferring tokens to another address.
6. Check the balance of the ERC20 token by calling the 'balanceOf()' method. This method takes the address of the wallet as input and returns the balance of the token in the wallet.
const balance = await contract.balanceOf('0x1234567890abcdefghijklmnopqrstuvwxyz');
console.log(balance);
The output of this code would be the balance of the Ethereum contract associated with the address '0x1234567890abcdefghijklmnopqrstuvwxyz', which is retrieved by calling the balanceOf() method on the contract object. This value would be printed to the console using the console.log() method.
7. To send the ERC20 token, call the 'transfer()' method and pass the recipient's address and the amount of tokens to be transferred as input parameters.
await contract.transfer('0x1234567890abcdefghijklmnopqrstuvwxyz', 10);
The output of this code would be the result of calling the transfer() method on the contract object, with the arguments '0x1234567890abcdefghijklmnopqrstuvwxyz' and 10. This method would likely transfer 10 units of some token or asset associated with the contract to the address '0x1234567890abcdefghijklmnopqrstuvwxyz'.
This code example shows how to send ERC20 tokens using Web3.js. The code imports the required modules, set up the connection to the Ethereum network, instantiates the ERC20 contract object, checks the balance of the token, and sends the token to the recipient. You can customize the code to your specific needs, such as adding error handling or using different parameters for the contract methods.
Below is the complete code for the above approach:
Â
JavaScript
async function main() {
const Web3 = require('web3');
const ERC20 = require('web3-eth-erc20');
const web3 = new Web3('https://mainnet.infura.io/v3/your-api-key');
const contract = new ERC20(web3, '0x1234567890abcdefghijklmnopqrstuvwxyz', {
from: '0x1234567890abcdefghijklmnopqrstuvwxyz',
gas: 6721975,
});
const balance = await contract.balanceOf('0x1234567890abcdefghijklmnopqrstuvwxyz');
console.log(balance);
await contract.transfer('0x1234567890abcdefghijklmnopqrstuvwxyz', 10);
}
main();
Explanation:Â
- This code imports the Web3 library, the ERC20 library, and creates an instance of Web3 that connects to the Ethereum mainnet via Infura.Â
- It then creates an instance of an ERC20 contract by providing the contract's address and the account address to use as the form field when calling contract methods.
- The code then calls the balanceOf method on the contract to get the balance of the specified Ethereum address and logs the result to the console.Â
- It then calls the transfer method on the contract to transfer 10 units of the ERC20 token to the specified Ethereum address.
Note that this code will only work if it is run in an environment that has the necessary libraries installed and if it is able to connect to the Ethereum network.
Similar Reads
How to use JSON web tokens with Node.js ?
JSON Web Token (JWT) is an Internet Standard that is used for exchanging data between two parties in a secure manner. It can't be easily hacked as it creates a digital signature with the secret key along with the HMAC algorithm). JWT Structure: JSON Web Tokens consist of three parts separated by do
4 min read
How to Get Started with WebGL?
WebGL (Web Graphics Library) is a powerful JavaScript API that allows developers to render 2D and 3D graphics directly in the browser without plugins. It is based on OpenGL ES, making it a great tool for creating interactive visuals and games. In this article, we will walk you through the basics of
4 min read
How to use SSL/TLS with Node.js ?
TLS/SSL is used for establishing secure connections over the internet. Today, most websites use HTTPS to communicate with clients. HTTPS is basically HTTP running over TLS/SSL. Web clients like browsers alert users about websites that do not use HTTPS since such websites are vulnerable to cyber-atta
5 min read
How to Build a Simple Web Server with Node.js ?
Node.js is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. You need to remember that NodeJS is not a framework, and itâs not a programming language. Node.js is mostly used in server-side programming. In this article, we will discuss how to make
3 min read
How to Get Session Token in AWS?
A session token is a popular concept that is used in AWS for giving access to some user or person for a limited amount of time, in this the user gets to access the AWS resources but only for a limited amount of time only.The purpose of the session token is to have more security in the AWS system so
6 min read
How to use TypeScript to build Node.js API with Express ?
TypeScript is a powerful version of JavaScript that incorporates static typing and other features, making it easy to build and maintain large applications. Combined with Node.js and Express, TypeScript can enhance your development experience by providing better type safety and tools. This guide will
4 min read
Flask API Authentication with JSON Web Tokens
Authentication is the process of verifying the identity of the user. It checks whether the user is real or not. It is used to provide access to resources only to valid users. There are two types of Authentication: Single Factor Authentication: In this only one piece of information is needed to verif
7 min read
How to Create and Verify JWTs with Node?
In this article, we will see how to create JWT tokens in Node.js. We will implement secure authentication in Node.js by creating and verifying JSON Web Tokens (JWTs) using libraries like `jsonwebtoken`.Prerequisites:Good knowledge of JavaScript.Basic knowledge about Express JS.Basic knowledge about
5 min read
How to Send WebSocket Requests with Postman ?
This article will show how to send WebSocket requests in Postman. Postman is a popular collaborative platform for API development. It offers different tools for designing, debugging, and testing an API, making it more efficient. WebSocket is an advanced technology used for real-time bidirectional co
3 min read
How to send dynamic key value pair to PHP with jQuery ?
The purpose of this article is to send dynamic key-value pairs to the PHP back-end using jQuery AJAX in an HTML document.Create two input fields i.e one for a key and the second one for value, and a button (to send key-value pair) in an HTML document. Assign a unique id to both the fields and to the
3 min read