Skip to main content

Don't have an API key yet?

Sign-up to Moralis to get your free API key and start building today.

Get Your Free API Key
Version: 2.0

How to get the spender allowance of an ERC20 token

Prerequisites​

Before getting started, make sure you have the following ready:

  • Node v.14+ or Python
  • NPM/Yarn or Pip

Step 1: Setup Moralis​

First register your Moralis account and get your Moralis API Key.

Once you have your Moralis API Key, install the Moralis SDK in your project.

npm install moralis @moralisweb3/common-evm-utils

Step 2: Get the spender allowance of an ERC20 token​

In order to get the spender allowance, Moralis provides you with an getTokenAllowance endpoint.

Here you'll need four parameters: address , chain, ownerAddress , spenderAddress.

  • address: The address of the token contract.
  • chain: The chain to query.
  • ownerAddress: The address of the token owner.
  • spenderAddress: The address of the token spender.

Once you have obtained the parameters, you can copy the following code:

const Moralis = require("moralis").default;
const { EvmChain } = require("@moralisweb3/common-evm-utils");

const runApp = async () => {
await Moralis.start({
apiKey: "YOUR_API_KEY",
// ...and any other configuration
});

const chain = EvmChain.ETHEREUM;

const address = "0x514910771AF9Ca656af840dff83E8264EcF986CA";

const ownerAddress = "0x7c470D1633711E4b77c8397EBd1dF4095A9e9E02";

const spenderAddress = "0xed33259a056f4fb449ffb7b7e2ecb43a9b5685bf";

const response = await Moralis.EvmApi.token.getTokenAllowance({
address,
chain,
ownerAddress,
spenderAddress,
});

console.log(response.toJSON());
};

runApp();

Step 3: Run the script​

To run the script, enter the following command:

node index.js

In your terminal, you should see the following JSON response:

{
"allowance": "0"
}

Congratulations πŸ₯³ You just got the spender allowance with just a few lines of code using the Moralis Token API!

API Reference​

If you want to know more details on the endpoint and optional parameters, check out:

Support​

If you face any trouble following the tutorial, feel free to reach out to our community engineers in our Discord or Forum to get 24/7 developer support.