-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathalchemy-sdk-script.js
More file actions
55 lines (47 loc) · 1.64 KB
/
alchemy-sdk-script.js
File metadata and controls
55 lines (47 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// This script demonstrates access to the NFT API via the Alchemy SDK.
import {
Network,
initializeAlchemy,
getNftsForOwner,
getNftMetadata,
BaseNft,
NftTokenType,
} from "@alch/alchemy-sdk";
// Optional Config object, but defaults to demo api-key and eth-mainnet.
const settings = {
apiKey: "demo", // Replace with your Alchemy API Key.
network: Network.ETH_MAINNET, // Replace with your network.
maxRetries: 10,
};
const alchemy = initializeAlchemy(settings);
// Print owner's wallet address:
const ownerAddr = "vitalik.eth";
console.log("fetching NFTs for address:", ownerAddr);
console.log("...");
// Print total NFT count returned in the response:
const nftsForOwner = await getNftsForOwner(alchemy, "vitalik.eth");
console.log("number of NFTs found:", nftsForOwner.totalCount);
console.log("...");
// Print contract address and tokenId for each NFT:
for (const nft of nftsForOwner.ownedNfts) {
console.log("===");
console.log("contract address:", nft.contract.address);
console.log("token ID:", nft.tokenId);
}
console.log("===");
// Fetch metadata for a particular NFT:
console.log("fetching metadata for a Crypto Coven NFT...");
const response = await getNftMetadata(
alchemy,
"0x5180db8F5c931aaE63c74266b211F580155ecac8",
"1590"
);
// Uncomment this line to see the full api response:
// console.log(response);
// Print some commonly used fields:
console.log("NFT name: ", response.title);
console.log("token type: ", response.tokenType);
console.log("tokenUri: ", response.tokenUri.gateway);
console.log("image url: ", response.rawMetadata.image);
console.log("time last updated: ", response.timeLastUpdated);
console.log("===");