Skip to content

Commit 4ef0cb8

Browse files
Merge pull request #5 from alchemyplatform/dc/update-to-use-sdk
Update to use the new SDK
2 parents 1ec136b + 6f4a395 commit 4ef0cb8

5 files changed

Lines changed: 1073 additions & 231 deletions

File tree

.DS_Store

6 KB
Binary file not shown.

alchemy-sdk-script.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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("===");

alchemy-web3-script.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// This script demonstrates access to the NFT API via the AlchemyWeb3.js package.
12
// alchemy-nft-api/alchemy-web3-script.js
23
import { createAlchemyWeb3 } from "@alch/alchemy-web3";
34

@@ -6,14 +7,14 @@ const apiKey = "demo";
67

78
// Initialize an alchemy-web3 instance:
89
const web3 = createAlchemyWeb3(
9-
`https://eth-mainnet.alchemyapi.io/v2/${apiKey}`,
10+
`https://eth-mainnet.alchemyapi.io/v2/${apiKey}`
1011
);
1112

1213
// The wallet address we want to query for NFTs:
1314
const ownerAddr = "0xC33881b8FD07d71098b440fA8A3797886D831061";
1415
const nfts = await web3.alchemy.getNfts({
15-
owner: ownerAddr
16-
})
16+
owner: ownerAddr,
17+
});
1718

1819
// Print owner's wallet address:
1920
console.log("fetching NFTs for address:", ownerAddr);
@@ -35,8 +36,8 @@ console.log("===");
3536
console.log("fetching metadata for a crypto coven NFT...");
3637
const response = await web3.alchemy.getNftMetadata({
3738
contractAddress: "0x5180db8F5c931aaE63c74266b211F580155ecac8",
38-
tokenId: "1590"
39-
})
39+
tokenId: "1590",
40+
});
4041

4142
// Uncomment this line to see the full api response:
4243
// console.log(metadata);

0 commit comments

Comments
 (0)