This repository was archived by the owner on Feb 19, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 6 files changed +7390
-1
lines changed Expand file tree Collapse file tree 6 files changed +7390
-1
lines changed Original file line number Diff line number Diff line change 1
- # token-api-javascript-scripts
1
+ # Alchemy Token API Javascript Scripts
2
+
3
+ Clone the repo, install dependencies, and try the API out!
4
+
5
+ 1 . Clone
6
+
7
+ ```
8
+ git clone https://github.com/alchemyplatform/nft-api-javascript-scripts.git
9
+ ```
10
+
11
+ 2 . Install
12
+
13
+ ```
14
+ npm install
15
+ ```
16
+
17
+ 3 . Use ` alchemy-web3 ` javascript sdk
18
+
19
+ ```
20
+ node alchemy-web3-script.js
21
+ ```
22
+
23
+ 4 . Use ` fetch ` javascript module
24
+
25
+ ```
26
+ node fetch-script.js
27
+ ```
28
+
29
+ 5 . Use ` axios ` javascript module
30
+
31
+ ```
32
+ node axios-script.js
33
+ ```
34
+
35
+ 6 . Read docs for more info, specific API methods and more:
Original file line number Diff line number Diff line change
1
+ // alchemy-token-api/alchemy-web3-script.js
2
+ import { createAlchemyWeb3 } from "@alch/alchemy-web3" ;
3
+
4
+ // Replace with your Alchemy api key:
5
+ const apiKey = "demo" ;
6
+
7
+ // Initialize an alchemy-web3 instance:
8
+ const web3 = createAlchemyWeb3 (
9
+ `https://eth-mainnet.g.alchemy.com/v2/${ apiKey } ` ,
10
+ ) ;
11
+
12
+ // The wallet address / token we want to query for:
13
+ const ownerAddr = "0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be" ;
14
+ const balances = await web3 . alchemy . getTokenBalances ( ownerAddr , [ "0x607f4c5bb672230e8672085532f7e901544a7375" ] )
15
+
16
+ // The token address we want to query for metadata:
17
+ const metadata = await web3 . alchemy . getTokenMetadata ( "0x607f4c5bb672230e8672085532f7e901544a7375" )
18
+
19
+ console . log ( "BALANCES->" ) ;
20
+ console . log ( balances ) ;
21
+ console . log ( "TOKEN METADATA->" ) ;
22
+ console . log ( metadata ) ;
Original file line number Diff line number Diff line change
1
+ // alchemy-token-api/axios-script.js
2
+ import axios from 'axios' ;
3
+
4
+ // Replace with your Alchemy API key:
5
+ const apiKey = "demo" ;
6
+ const baseURL = `https://eth-mainnet.g.alchemy.com/v2/${ apiKey } ` ;
7
+ // Replace with the wallet address you want to query:
8
+ const ownerAddr = "0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be" ;
9
+ // Replace with the token contract address you want to query:
10
+ const tokenAddr = "0x607f4c5bb672230e8672085532f7e901544a7375" ;
11
+
12
+ var data = JSON . stringify ( {
13
+ "jsonrpc" : "2.0" ,
14
+ "method" : "alchemy_getTokenBalances" ,
15
+ "params" : [
16
+ `${ ownerAddr } ` ,
17
+ [
18
+ `${ tokenAddr } `
19
+ ]
20
+ ] ,
21
+ "id" : 42
22
+ } ) ;
23
+
24
+ var config = {
25
+ method : 'post' ,
26
+ url : baseURL ,
27
+ headers : {
28
+ 'Content-Type' : 'application/json'
29
+ } ,
30
+ data : data
31
+ } ;
32
+
33
+ axios ( config )
34
+ . then ( function ( response ) {
35
+ console . log ( JSON . stringify ( response . data , null , 2 ) )
36
+ } )
37
+ . catch ( function ( error ) {
38
+ console . log ( error ) ;
39
+ } ) ;
Original file line number Diff line number Diff line change
1
+ // alchemy-token-api/fetch-script.js
2
+ import fetch from 'node-fetch' ;
3
+
4
+ // Replace with your Alchemy API key:
5
+ const apiKey = "demo" ;
6
+ const fetchURL = `https://eth-mainnet.g.alchemy.com/v2/${ apiKey } ` ;
7
+
8
+ // Replace with the wallet address you want to query:
9
+ const ownerAddr = "0x3f5ce5fbfe3e9af3971dd833d26ba9b5c936f0be" ;
10
+ // Replace with the token contract address you want to query:
11
+ const tokenAddr = "0x607f4c5bb672230e8672085532f7e901544a7375" ;
12
+
13
+ var 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
+
28
+ var requestOptions = {
29
+ method : 'POST' ,
30
+ body : raw ,
31
+ redirect : 'follow'
32
+ } ;
33
+
34
+ // Make the request and print the formatted response:
35
+ fetch ( 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 ) ) ;
You can’t perform that action at this time.
0 commit comments