快速入门
获取NFT
Last updated March 12, 2025
从Solana区块链获取NFT数据。
获取NFT或收藏集
在以下部分,您可以看到完整的代码示例以及需要更改的参数。有关获取NFT和收藏集的更多详情,请参阅Core文档。
1import { fetchAsset, fetchCollection, mplCore } from '@metaplex-foundation/mpl-core';
2import { publicKey } from '@metaplex-foundation/umi';
3import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
4
5// Initialize UMI
6const umi = createUmi('https://api.devnet.solana.com')
7 .use(mplCore())
8
9// Fetch a Core Asset
10const assetAddress = publicKey('AssetAddressHere...')
11const asset = await fetchAsset(umi, assetAddress)
12
13// Fetch a Core Collection
14const collectionAddress = publicKey('CollectionAddressHere...')
15const collection = await fetchCollection(umi, collectionAddress)
16
17console.log('Asset fetched:', asset)
18console.log('Name:', asset.name)
19console.log('Owner:', asset.owner)
20console.log('URI:', asset.uri)
21
22console.log('\nCollection fetched:', collection)
23console.log('Name:', collection.name)
24console.log('URI:', collection.uri)
1# Fetch an NFT using the Metaplex CLI
2
3# Fetch asset by ID
4mplx core fetch asset <assetId>
5
6# Download all files to a directory
7mplx core fetch asset <assetId> --download --output ./assets
8
9# Download only the image
10mplx core fetch asset <assetId> --download --image
11
12# Download only the metadata
13mplx core fetch asset <assetId> --download --metadata
1// npm install @metaplex-foundation/umi @metaplex-foundation/umi-bundle-defaults @metaplex-foundation/digital-asset-standard-api
2import { publicKey } from '@metaplex-foundation/umi'
3import { createUmi } from '@metaplex-foundation/umi-bundle-defaults'
4import { dasApi } from '@metaplex-foundation/digital-asset-standard-api'
5
6// Initialize Umi with a DAS-enabled RPC endpoint
7const umi = createUmi('https://api.devnet.solana.com').use(dasApi())
8
9// The address of the NFT you want to fetch
10const assetAddress = publicKey('YOUR_NFT_ADDRESS')
11
12// Fetch the asset using DAS API
13const asset = await umi.rpc.getAsset(assetAddress)
14
15console.log('Asset ID:', asset.id)
16console.log('Name:', asset.content.metadata?.name)
17console.log('Description:', asset.content.metadata?.description)
18console.log('Image:', asset.content.links?.image)
19console.log('Owner:', asset.ownership.owner)
20console.log('Interface:', asset.interface)
1# Fetch an NFT using the DAS API
2curl -X POST \
3 -H "Content-Type: application/json" \
4 -d '{
5 "jsonrpc": "2.0",
6 "id": 1,
7 "method": "getAsset",
8 "params": {
9 "id": "<NFT Mint Address>"
10 }
11 }' \
12 https://api.devnet.solana.com
参数
根据您的获取需求自定义以下参数:
| 参数 | 描述 |
|---|---|
assetAddress | NFT资产的公钥 |
collectionAddress | 收藏集的公钥(可选) |
工作原理
获取过程涉及以下步骤:
- 获取地址 - 您需要想要获取的NFT资产或收藏集的公钥
- 获取资产数据 - 使用
fetchAsset检索包括名称、URI、所有者和插件在内的NFT信息 - 获取收藏集数据 - 使用
fetchCollection检索收藏集信息(可选)
NFT和收藏集数据
获取资产时,将返回所有数据:
- Name - NFT的名称
- URI - 元数据JSON的链接
- Owner - 拥有NFT的钱包
- Update Authority - 可以修改NFT的人
- Plugins - 附加的插件,如版税和属性
获取收藏集时,将返回:
- Name - 收藏集的名称
- URI - 收藏集元数据JSON的链接
- Update Authority - 可以修改收藏集的人
- Num Minted - 收藏集中的资产数量
- Plugins - 附加的插件,如版税和属性
