List an NFT
Prerequisites
You should have already completed Part 1: Setup where you created all necessary keys and cloned our sample repo that uses the OpenSea SDK.
Choose your NFT to list
First, you'll need to actually own an NFT before you can list it. If you don't own one, go to OpenSea, sign in, and find one to buy. Once you've bought one, note the contractAddress
and the tokenId
of the NFT. If you look at this Bored Ape Yacht Club NFT, you can find the contractAddress
and the tokenId
in the URL.
Update the code
Open up the [createListing.ts](https://github.com/ProjectOpenSea/buy-sell-opensea-sdk-demo/blob/main/src/createListing.ts)
file under the /src
directory. Here's the code:
import { WALLET_ADDRESS, sdk } from './utils/constants';
const createListing = async () => {
// TODO: Fill in the token address and token ID of the NFT you want to sell, as well as the price
let tokenAddress: string = "";
let tokenId: string = "";
let listingAmount: string = "";
const listing = {
accountAddress: WALLET_ADDRESS,
startAmount: listingAmount,
asset: {
tokenAddress: tokenAddress,
tokenId: tokenId,
},
};
try {
const response = await sdk.createListing(listing);
console.log("Successfully created a listing with orderHash:", response.orderHash);
} catch (error) {
console.error("Error in createListing:", error);
}
}
// Check if the module is the main entry point
if (require.main === module) {
// If yes, run the createOffer function
createListing().catch((error) => {
console.error("Error in createListing:", error);
});
}
export default createListing;
Before running the script, you need to update Lines 6-8 (add values for tokenAddress
, tokenId
, and listingAmount
) to values that represent the NFT you want to list, and the price.
Create the listing
After setting the above variables, you'll need to first make sure the code compiles:
npm run build
Next, run the createListing
script with this command:
npm run createListing
If the offer was successfully created, you'll see a similar this output from the script:
Successfully created an listing with orderHash: <order_hash>
Validate the listing
Navigate back to the NFTs page on OpenSea. If you've successfully created a new listing, you'll see it there. This is the listing that was created in the test run of this tutorial:
Updated 11 months ago