Buy and Sell NFTs

Buy and sell NFTs programmatically using the OpenSea SDK (@opensea/sdk).

The OpenSea SDK (@opensea/sdk) is a TypeScript SDK for creating listings, making offers, fulfilling orders, and querying NFT data programmatically via the Seaport protocol.

Supports both ethers.js and viem as wallet/provider libraries.

Quick Start

npm install @opensea/sdk

With ethers.js

import { ethers } from "ethers";
import { OpenSeaSDK, Chain } from "@opensea/sdk";

const provider = new ethers.JsonRpcProvider("https://your-rpc-url");

const sdk = new OpenSeaSDK(provider, {
  chain: Chain.Mainnet,
  apiKey: "YOUR_API_KEY",
});

With viem

import { createPublicClient, createWalletClient, http } from "viem";
import { mainnet } from "viem/chains";
import { OpenSeaSDK, Chain } from "@opensea/sdk/viem";

const publicClient = createPublicClient({ chain: mainnet, transport: http("https://your-rpc-url") });
const walletClient = createWalletClient({ chain: mainnet, transport: http("https://your-rpc-url"), account: "0x..." });

const sdk = new OpenSeaSDK(
  { publicClient, walletClient },
  { chain: Chain.Mainnet, apiKey: "YOUR_API_KEY" },
);

Usage (same for both)

// Get a collection
const collection = await sdk.api.getCollection("boredapeyachtclub");

// Make an offer
const offer = await sdk.createOffer({
  asset: { tokenAddress: "0x...", tokenId: "1" },
  accountAddress: "0x...",
  amount: 0.5,
});

// Create a listing
const listing = await sdk.createListing({
  asset: { tokenAddress: "0x...", tokenId: "1" },
  accountAddress: "0x...",
  amount: 3,
});

// Fulfill an order (buy or accept offer)
await sdk.fulfillOrder({ order, accountAddress: "0x..." });

Requirements

  • Node.js >= 20.0.0
  • OpenSea API key (get one here)
  • ethers.js (v6+) or viem (v2+) as your wallet/provider library

Security Warning: Do not use the SDK in client-side code — your API key would be exposed. Use it on a backend server and return transaction data to your frontend.

Documentation

Full documentation including a getting started guide, API reference, advanced use cases (bulk orders, cancellation, event listeners), and FAQ is maintained in the GitHub repository:

github.com/ProjectOpenSea/opensea-js

Links