You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用JavaScript从OpenSea获取NFT的Traits属性?

用JavaScript从OpenSea获取NFT Traits属性的可行方案

核心方案:调用OpenSea官方API

OpenSea提供了资产查询API,可直接获取指定NFT的完整信息(包括Traits属性),以下是具体实现步骤:


1. 准备工作

  • 申请OpenSea API密钥:前往OpenSea开发者平台注册获取API密钥,无密钥的请求会受到严格频率限制,甚至被拒绝。
  • 获取目标NFT关键信息:需要知道NFT的合约地址和Token ID,这两个参数是定位具体NFT的核心。

2. JavaScript实现代码(Fetch版本)

// 替换为你的实际信息
const API_KEY = '你的OpenSea API密钥';
const CONTRACT_ADDRESS = '目标NFT的合约地址';
const TOKEN_ID = '目标NFT的Token ID';

async function fetchNFTTraits() {
  try {
    // 发送API请求
    const res = await fetch(
      `https://api.opensea.io/api/v1/assets?asset_contract_address=${CONTRACT_ADDRESS}&token_ids=${TOKEN_ID}`,
      {
        headers: {
          'X-API-KEY': API_KEY,
          'Accept': 'application/json'
        }
      }
    );
    
    const data = await res.json();
    
    // 提取并处理Traits
    if (data.assets?.length > 0) {
      const nft = data.assets[0];
      const traits = nft.traits;
      
      // 示例:打印Traits到控制台
      console.log('NFT属性列表:');
      traits.forEach(trait => {
        console.log(`${trait.trait_type}: ${trait.value}`);
      });
      
      // 可将traits返回用于页面渲染等业务逻辑
      return traits;
    } else {
      console.log('未找到对应NFT');
      return null;
    }
  } catch (err) {
    console.error('请求失败:', err);
    return null;
  }
}

// 执行函数
fetchNFTTraits();

3. 注意事项

  • 若使用测试网(如Rinkeby),只需将API端点替换为测试网对应地址即可。
  • 部分NFT可能未设置Traits属性,需在代码中加入空值判断,避免报错。
  • API请求有频率限制,请勿高频调用,建议对请求结果做缓存处理。

翻译后的Traits示例

"traits": [
    {
      "trait_type": "原始上衣",
      "value": "白领衬衫",
      "display_type": null,
      "max_value": null,
      "trait_count": 0,
      "order": null
    },
    {
      "trait_type": "原始背景",
      "value": "红色",
      "display_type": null,
      "max_value": null,
      "trait_count": 0,
      "order": null
    },
    {
      "trait_type": "原始类型",
      "value": "普通mfer",
      "display_type": null,
      "max_value": null,
      "trait_count": 0,
      "order": null
    },
    {
      "trait_type": "原始眼睛",
      "value": "眼罩",
      "display_type": null,
      "max_value": null,
      "trait_count": 0,
      "order": null
    },
    {
      "trait_type": "嘴巴",
      "value": "微笑",
      "display_type": null,
      "max_value": null,
      "trait_count": 0,
      "order": null
    },
    {
      "trait_type": "原始耳机",
      "value": "白色耳机",
      "display_type": null,
      "max_value": null,
      "trait_count": 0,
      "order": null
    },
    {
      "trait_type": "原始烟",
      "value": "黑色香烟",
      "display_type": null,
      "max_value": null,
      "trait_count": 0,
      "order": null
    },
    {
      "trait_type": "原始4:20手表",
      "value": "牡蛎银",
      "display_type": null,
      "max_value": null,
      "trait_count": 0,
      "order": null
    }
  ],

内容的提问来源于stack exchange,提问作者user18181493

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 14:06:24