Skip to main content

Quickstart

Prerequisites

  • Ensure you have the latest version of NodeJS (packaged with NPM) installed.

Initialize a new project

mkdir new_project
cd new_project
npm install -D typescript ts-node
npx tsc --init

Install the libraries

Install the Infura NFT API:

npm install -S @infura/sdk

Install the dotenv package to load environment variables from a .env file to store configuration in the environment separate from the code.

npm i dotenv

Create your .env file

Create a .env file at the project root and add the following data:

INFURA_API_KEY=<API-KEY>
INFURA_API_KEY_SECRET=<API-KEY-SECRET>
WALLET_PUBLIC_ADDRESS=<WALLET-PUBLIC-ADDRESS>
WALLET_PRIVATE_KEY=<WALLET-PRIVATE-KEY>

Replace the Infura project credential placeholders with your own.

Add a wallet address and its private key for testing on the supported network the application will run on.

danger

Never disclose your private keys or secret recovery phrases. Do not commit the .env file to your repository if it contains sensitive data. You can create a .gitignore file to prevent accidentally committing the file.

Create your script

touch index.ts

Copy the following code into your script:

info

If using a network other than Ethereum Mainnet, then update the chainId value in the code to an alternate supported network.

// Import the libraries and load the environment variables.
import { config as loadEnv } from "dotenv";
import { SDK, Auth, TEMPLATES, Metadata } from "@infura/sdk";
loadEnv();

// Create Auth object
const auth = new Auth({
projectId: process.env.INFURA_API_KEY,
secretId: process.env.INFURA_API_KEY_SECRET,
privateKey: process.env.WALLET_PRIVATE_KEY,
chainId: 1,
});

// Instantiate SDK
const sdk = new SDK(auth);
const getCollectionsByWallet = async (walletAddress: string) => {
const result = await sdk.api.getCollectionsByWallet({
walletAddress: walletAddress,
});
console.log("collections:", result);
};

(async () => {
try {
await getCollectionsByWallet("0xacdaEEb57ff6886fC8e203B9Dd4C2b241DF89b7a");
} catch (error) {
console.log(error);
}
})();

Run the script

npx ts-node index.ts

The result should look similar to:

collections: {
total: 373,
pageNumber: 1,
pageSize: 100,
network: 'ETHEREUM',
cursor: 'eyJhbGciOiJIUzI1NiJ9.ZXlKaGJHY2lPaUpJVXpJMU5pSXNJblI1Y0NJNklrcFhWQ0o5LmV5SmpkWE4wYjIxUVlYSmhiWE1pT25zaWQyRnNiR1YwUVdSa2NtVnpjeUk2SWpCNFlXTmtZV1ZsWWpVM1ptWTJPRGcyWm1NNFpUSXdNMkk1WkdRMFl6SmlNalF4WkdZNE9XSTNZU0o5TENKclpYbHpJanBiSWpFMk5UYzFPVFF6TXprdU1qazRJbDBzSW5kb1pYSmxJanA3SW05M2JtVnlYMjltSWpvaU1IaGhZMlJoWldWaU5UZG1aalk0T0RabVl6aGxNakF6WWpsa1pEUmpNbUl5TkRGa1pqZzVZamRoSW4wc0lteHBiV2wwSWpveE1EQXNJbTltWm5ObGRDSTZNQ3dpYjNKa1pYSWlPbHRkTENKa2FYTmhZbXhsWDNSdmRHRnNJanBtWVd4elpTd2lkRzkwWVd3aU9qTTNNeXdpY0dGblpTSTZNU3dpZEdGcGJFOW1abk5sZENJNk1Td2lhV0YwSWpveE5qYzRPRFV6TlRreGZRLlRoSklVNU9ETzNiUmpnUGpOcXYzU3pTaXFrZ1dpZEdYMHN2bHhSajdxbkE.AVJ6hM7qlFKjMyKcMwn5zREnyfBCnj_iS6IVbQGg_D4',
account: '0xacdaEEb57ff6886fC8e203B9Dd4C2b241DF89b7a',
collections: [
{
contract: '0xffbfd7d27e3fb59de43054c234b464dfe0f95ae8',
tokenType: 'ERC1155',
name: ' Witcher Art Mint Pass Genesis',
symbol: ' Witcher Art Mint Pass Genesis'
},
...
}