Search…
Fetching NFT Data
Learn how to fetch your NFT data
Since most NFTs are stored on-chain, retrieving metadata can be a complex process. There are two main options for retrieving data.
  1. 1.
    Directly from the Source Network
  2. 2.
    From Koii Nodes
  3. 3.
    Using Koii-X custom query hook

Directly from the Source Network

Reading data from the source network directly means that we can often only read one NFT at a time, and that the data will be missing contextual information. This can still be useful for certain applications and during testing.

Atomic NFTs (from Arweave)

Atomic NFTs are some of the easiest to read into an application, since Arweave has a number of supported REST APIs. The arweave.net gateway provides easy CORS-compatible services for pulling in contract data, but it will need to be reconstructed using the SmartWeave libraries client-side to get the latest state.
1
// Some code showing how to fetch an NFT via Smartweave's API
2
import Arweave from "arweave";
3
​
4
const arweave = new Arweave({
5
host: 'arweave.net',
6
port: 443,
7
protocol: 'https'
8
});
9
10
const id = "NFTid"
11
getDatafromArweave(id);
12
​
13
​
14
async function getDatafromArweave(id) {
15
let NFTdata = await arweave.transactions.get(id)
16
console.log(NFTdata)
17
}
Copied!

Ethereum NFTs (from Ethereum, Polygon, or Avalanche)

Ethereum NFTs are coming soon to Koii-X Templates.

From the Koii Nodes via the Mainnet API

If you are using Atomic NFTs, there is often a lot of additional information such as attention tracking counts, total KOII rewards, and bridging information. To fetch full NFT records, you can query the Koii Mainnet Gateway APIs as shown below:
1
// Some code showing how to query mainnet.koii.live for the latest NFT states
2
import * as kweb from "@_koi/sdk/web";
3
let ktools = new kweb.Web();
4
​
5
const id = "NFTid"
6
getDatafromArweave(id);
7
​
8
async function getDatafromArweave(id) {
9
let NFTdata = await ktools.getNftState(id);
10
console.log(NFTdata)
11
}
Copied!

Using Koii-X custom query hook

This is the easiest way to fetch the data of an NFT:
Import useNft query hook
1
import { useNft } from "api/hooks";
Copied!
Pass your NFT id to useNft
1
const nftId = 8ZsDVH9Iotsx4Y1U6QPdBibR3N1BkKt1CgEsYRyKUSE;
2
const { data, isLoading } = useNft({ id: nftId });
Copied!
That's it. You should have this NFT content in data