Token API Quickstart

A new developer’s guide to using the Token API and getting token information. Query Token data using alchemy-web3 (recommended) or fetch.

How to Query the Token API

Fetch

You can easily interact with Alchemy’s Token API using simple fetch requests. No additional dependencies required with Node.js 18+.

Usage

token-api-javascript-scripts/alchemy-web3-script.js at main · alchemyplatform/token-api-javascript-scripts

github.comgithub.com

token-api-javascript-scripts/alchemy-web3-script.js at main · alchemyplatform/token-api-javascript-scripts

shell
$touch token-api-script.js

And then paste the following code snippet into the file:

token-api-script.js
1// Replace with your Alchemy API Key
2const apiKey = "demo";
3const baseURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
4
5async function getTokenData() {
6 try {
7 // The wallet address / token we want to query for:
8 const ownerAddr = "0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be";
9 const tokenAddr = "0x607f4c5bb672230e8672085532f7e901544a7375";
10
11 // Get token balances
12 const balanceResponse = await fetch(baseURL, {
13 method: 'POST',
14 headers: {
15 'Content-Type': 'application/json',
16 },
17 body: JSON.stringify({
18 jsonrpc: "2.0",
19 method: "alchemy_getTokenBalances",
20 params: [ownerAddr, [tokenAddr]],
21 id: 1
22 })
23 });
24
25 const balanceData = await balanceResponse.json();
26
27 // Get token metadata
28 const metadataResponse = await fetch(baseURL, {
29 method: 'POST',
30 headers: {
31 'Content-Type': 'application/json',
32 },
33 body: JSON.stringify({
34 jsonrpc: "2.0",
35 method: "alchemy_getTokenMetadata",
36 params: [tokenAddr],
37 id: 2
38 })
39 });
40
41 const metadataData = await metadataResponse.json();
42
43 console.log("Token Balances:");
44 console.log(balanceData.result);
45 console.log("Token Metadata:");
46 console.log(metadataData.result);
47
48 } catch (error) {
49 console.error('Request failed:', error.message);
50 }
51}
52
53getTokenData();

From your command line, you can execute the script with:

shell
$node token-api-script.js

You should see output like this:

shell
$Token Balances:
>{
> address: '0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be',
> tokenBalances: [
> {
> contractAddress: '0x607f4c5bb672230e8672085532f7e901544a7375',
> tokenBalance: '0x0000000000000000000000000000000000000000000000000000000000000000',
> error: null
> }
> ]
>}
>Token Metadata:
>{
> decimals: 9,
> logo: 'https://static.alchemyapi.com/images/assets/1637.png',
> name: 'iExec RLC',
> symbol: 'RLC'
>}

Usage

token-api-javascript-scripts/fetch-script.js at main · alchemyplatform/token-api-javascript-scripts

github.comgithub.com

token-api-javascript-scripts/fetch-script.js at main · alchemyplatform/token-api-javascript-scripts

shell
$touch fetch-script.js

and then paste the following code snippet into the file to explore the getNFTs method:=

fetch-script.js
1// alchemy-token-api/fetch-script.js
2
3
4// Replace with your Alchemy API key:
5const apiKey = "demo";
6const fetchURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;
7
8// Replace with the wallet address you want to query:
9const ownerAddr = "0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be";
10// Replace with the token contract address you want to query:
11const tokenAddr = "0x607f4c5bb672230e8672085532f7e901544a7375";
12
13var raw = JSON.stringify({
14 "jsonrpc": "2.0",
15 "method": "alchemy_getTokenBalances",
16 "headers": {
17 "Content-Type": "application/json"
18 },
19 "params": [
20 `${ownerAddr}`,
21 [
22 `${tokenAddr}`,
23 ]
24 ],
25 "id": 42
26});
27
28var requestOptions = {
29 method: 'POST',
30 body: raw,
31 redirect: 'follow'
32};
33
34// Make the request and print the formatted response:
35fetch(fetchURL, requestOptions)
36 .then(response => response.json())
37 .then(response => JSON.stringify(response, null, 2))
38 .then(result => console.log(result))
39 .catch(error => console.log('error', error));

From your command line, you can execute the script with:

shell
$node fetch-script.js

Your output should look like the following:

json
1{
2 "jsonrpc": "2.0",
3 "id": 42,
4 "result": {
5 "address": "0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be",
6 "tokenBalances": [
7 {
8 "contractAddress": "0x607f4c5bb672230e8672085532f7e901544a7375",
9 "tokenBalance": "0x00000000000000000000000000000000000000000000000000003c005f81ab00",
10 "error": null
11 }
12 ]
13 }
14}