How to fetch NFT collection using JavaScript and OpenSea API

A simple guide on how to use OpenSea's API to fetch your assets

Monday, May 16, 2022

open sea

TLDR

Check the code here

Check my list of NFTs here

Introduction

Non-fungible tokens (NFT)


OpenSea is the world's first and largest NFT marketplace. It can be used to discover, collect, and sell extraordinary NFTs.


In this article, I will show how I fetch all my collections using OpenSea's API and then display them on my website.

Prerequisite

For us to be able to use OpenSea's API, we need to request an OpenSea API key here


Create a reusable options object for each API call

This will contain the API key provided by OpenSea

.js
12345678const options = { method: 'GET', headers: { Accept: 'application/json', // Do not expose your API key in the browser 'X-API-KEY': process.env.OPENSEA_API, },};

To avoid exposing your API key, refer to this blog.


(OPTIONAL) Create a variable for the wallet's address

This is the address of our wallet. We can use this address to fetch our collections and assets.

.js
1const WALLET_ADDRESS = '0x704CD00cbB8BF91038dFCF8bC008D065DDF1D8F8';

Fetch all collections owned by the specified address

.js
1234const collectionResponse = await fetch( `https://api.opensea.io/api/v1/collections?asset_owner=${WALLET_ADDRESS}`, options,).then(response => response.json());

Map the response to the preferred structure

Here is the type definition of how my website used the response data

.ts
1234567891011type NftCollection = { name: string; slug: string; contractAddress: string; details: string; owned: { id: number; img: string; name: string; }[];};

Here is how the mapping of the response to the above type definition

.js
1234567const collection = collectionResponse.map((item) => ({ details: item.description, slug: item.slug, name: item.name, contractAddress: item.primary_asset_contracts[0].address, owned: [],}));

For each collection, fetch all assets owned by the defined address

.js
1234567891011121314for (const iterator of collection) { const assetsResponse = await fetch( `https://api.opensea.io/api/v1/assets?owner=${WALLET_ADDRESS}&asset_contract_address=${iterator.contractAddress}&include_orders=false`, options, ).then(response => response.json()); iterator.owned = assetsResponse.assets .map((item) => ({ name: item.name, img: item.image_url, id: item.token_id, })) .filter((item: any) => item.name && item.img);}

Putting it all together

.js
1234567891011121314151617181920212223242526272829303132333435363738394041const fetchOwnCollection = async () => { const WALLET_ADDRESS = '0x704CD00cbB8BF91038dFCF8bC008D065DDF1D8F8'; const options = { method: 'GET', headers: { Accept: 'application/json', 'X-API-KEY': process.env.OPENSEA_API as string, }, }; const collectionResponse = await fetch( `https://api.opensea.io/api/v1/collections?asset_owner=${WALLET_ADDRESS}`, options, ).then(response => response.json()); const collection = collectionResponse.map(item => ({ details: item.description, slug: item.slug, name: item.name, contractAddress: item.primary_asset_contracts[0].address, owned: [], })); for (const iterator of collection) { const assetsResponse = await fetch( `https://api.opensea.io/api/v1/assets?owner=${WALLET_ADDRESS}&asset_contract_address=${iterator.contractAddress}&include_orders=false`, options, ).then(response => response.json()); iterator.owned = assetsResponse.assets .map(item => ({ name: item.name, img: item.image_url, id: item.token_id, })) .filter(item => item.name && item.img); } return collection;};

Rendering the collection

It will depend on the frontend framework of choice. Here is how I did it using NextJs.

NOTE: The overall API requests might be slow when fetched from the browser, make sure to have an appropriate content loader in place. Caching the response might help as well.

Demo

An image of a blog post

What's next?

Add functionalities for visitors to make offers and buy my NFT collection.