Skip to content

How to fetch a Smart Account's NFTs

We provide several Enhanced APIs, which are especially useful for querying information about the smart accounts you create using Account Kit, such as the account's owned NFTs using the NFT API.

For the purposes of our example, we will use the NFT API to query our smart account's data by extending the Alchemy Smart Account Client with Enhanced APIs.

1. Install the alchemy-sdk

We have developed a Typescript SDK to make development with the Enhanced APIs simple. The SDK includes ways to leverage Alchemy's Simulation API, Token API, Transact API, NFT API, Webhooks and Websockets, and more across our supported chains. Take a look at the code here.

We will use the Alchemy SDK Client to extend our Alchemy Smart Account Client using the client's alchemyEnhancedApiActions method. That way, our client will have direct access to the Enhanced APIs.

To use the Alchemy SDK in our project directory, we will need to install the required package:

bash
npm i alchemy-sdk
bash
yarn add alchemy-sdk

2. Extend the Alchemy Smart Account Client with Enhanced APIs

Then, all we need to do is create an alchemy client from the Alchemy SDK, create an AlchemySmartAccountClient from Account Kit, and then extend the client with functionality from the SDK client using alchemyEnhancedApiActions. We can get the smart account's address from the AlchemySmartAccountClient in order to fetch the smart account's NFT in just 1 line of code!

ts
import {
  alchemyEnhancedApiActions,
  createModularAccountAlchemyClient,
} from "@alchemy/aa-alchemy";
import { LocalAccountSigner, sepolia } from "@alchemy/aa-core";
import { Alchemy, Network } from "alchemy-sdk";

const alchemy = new Alchemy({
  network: Network.ETH_SEPOLIA,
  apiKey: "YOUR_API_KEY",
});

const client = (
  await createModularAccountAlchemyClient({
    chain: sepolia,
    apiKey: "YOUR_API_KEY",
    signer: LocalAccountSigner.mnemonicToAccountSigner("OWNER_MNEMONIC"),
  })
).extend(alchemyEnhancedApiActions(alchemy));

const address = client.getAddress();

// get all NFTs owned by the smart account
export const nfts = client.nft.getNftsForOwner(address);

Note

Note that we must configure the Alchemy SDK client to have the same API Key and blockchain network as Alchemy Smart Account Client it is extending via alchemyEnhancedApiActions. This method explicitly checks this requirement and will throw an error at runtime if not satisfied.

Additionally, since the Alchemy SDK client does not yet support JWT authentication, an AlchemySmartAccountClient initialized with JWTs cannot use this method. We must be initialize the client with an API key or RPC URL.

That's it! There are so many more Enhanced APIs the Alchemy SDK exposes, and can be useful for development with Account Abstraction. Try it out here, and check out How to fetch a Smart Account's ERC-20 Tokens for another example.