├── .gitignore ├── README.md ├── alchemy-sdk-script.js ├── axios-script.js ├── fetch-script.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Alchemy NFT API Javascript Scripts 2 | 3 | Clone the repo, install dependencies, and try the API out! 4 | 5 | 1. Clone 6 | 7 | ``` 8 | git clone git@github.com:alchemyplatform/nft-api-javascript-scripts.git 9 | ``` 10 | 11 | 2. Install 12 | 13 | ``` 14 | npm install 15 | ``` 16 | 17 | 3. Use `alchemy-sdk` javascript sdk 18 | 19 | ``` 20 | node alchemy-sdk-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 36 | 37 | https://docs.alchemy.com/alchemy/enhanced-apis/nft-api 38 | -------------------------------------------------------------------------------- /alchemy-sdk-script.js: -------------------------------------------------------------------------------- 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 = "vitalik.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, "vitalik.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("==="); 56 | -------------------------------------------------------------------------------- /axios-script.js: -------------------------------------------------------------------------------- 1 | // alchemy-nft-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.alchemyapi.io/v2/${apiKey}/getNFTs/`; 7 | // Replace with the wallet address you want to query for NFTs: 8 | const ownerAddr = "0xF5FFF32CF83A1A614e15F25Ce55B0c0A6b5F8F2c"; 9 | 10 | // Construct the axios request: 11 | var config = { 12 | method: 'get', 13 | url: `${baseURL}?owner=${ownerAddr}` 14 | }; 15 | 16 | // Make the request and print the formatted response: 17 | axios(config) 18 | .then(response => console.log(JSON.stringify(response.data, null, 2))) 19 | .catch(error => console.log(error)); 20 | -------------------------------------------------------------------------------- /fetch-script.js: -------------------------------------------------------------------------------- 1 | // alchemy-nft-api/fetch-script.js 2 | import fetch from 'node-fetch'; 3 | 4 | // Setup request options: 5 | var requestOptions = { 6 | method: 'GET', 7 | redirect: 'follow' 8 | }; 9 | 10 | // Replace with your Alchemy API key: 11 | const apiKey = "demo"; 12 | const baseURL = `https://eth-mainnet.alchemyapi.io/v2/${apiKey}/getNFTs/`; 13 | // Replace with the wallet address you want to query: 14 | const ownerAddr = "0xF5FFF32CF83A1A614e15F25Ce55B0c0A6b5F8F2c"; 15 | const fetchURL = `${baseURL}?owner=${ownerAddr}`; 16 | 17 | // Make the request and print the formatted response: 18 | fetch(fetchURL, requestOptions) 19 | .then(response => response.json()) 20 | .then(response => JSON.stringify(response, null, 2)) 21 | .then(result => console.log(result)) 22 | .catch(error => console.log('error', error)); 23 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "alchemy-nft-api", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "keywords": [], 10 | "author": "", 11 | "license": "ISC", 12 | "type": "module", 13 | "dependencies": { 14 | "@alch/alchemy-sdk": "^1.0.4", 15 | "@alch/alchemy-web3": "^1.1.12", 16 | "axios": "^0.25.0", 17 | "node-fetch": "^3.2.0" 18 | } 19 | } 20 | --------------------------------------------------------------------------------