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