Skip to content

Latest commit

 

History

History
183 lines (136 loc) · 7.21 KB

File metadata and controls

183 lines (136 loc) · 7.21 KB

Version npm Test CI Coverage Status License Docs Discussions

@opensea/sdk

This is the TypeScript SDK for OpenSea, the largest marketplace for NFTs and tokens.

It allows developers to access the official orderbook, filter it, create listings and offers, complete trades programmatically, and swap tokens across chains.

Get started by getting an API key and instantiating your own OpenSea SDK instance. Then you can create orders off-chain or fulfill orders onchain, and listen to events in the process.

Get an API key

For quick experimentation — request a free-tier key in code, no signup needed. The returned key is valid for 30 days and the endpoint is rate-limited to 3 keys per hour per IP:

import { OpenSeaSDK } from "@opensea/sdk";

const { api_key } = await OpenSeaSDK.requestInstantApiKey();
const sdk = new OpenSeaSDK(provider, { chain: Chain.Mainnet, apiKey: api_key });

Or from the shell:

curl -s -X POST https://api.opensea.io/api/v2/auth/keys | jq -r '.api_key'

For production — create a permanent key at opensea.io/settings/developer. These keys don't expire, get higher rate limits, and can be rotated from your account. See the API key docs for details.

Happy seafaring!

Quick Start

With ethers.js

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

const provider = new ethers.JsonRpcProvider("https://eth-mainnet.g.alchemy.com/v2/YOUR_ALCHEMY_API_KEY");
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() })
const sdk = new OpenSeaSDK({ publicClient }, { chain: Chain.Mainnet, apiKey: 'YOUR_API_KEY' })

Wallet-authenticated API helpers

For a server-side EVM signer, OpenSeaAuth signs in with SIWE, creates a scoped personal access token (PAT), and exchanges it for the short-lived JWT used by REST and MCP.

import { Wallet } from "ethers"
import { OpenSeaAPI, OpenSeaAuth } from "@opensea/sdk"

const signer = new Wallet(process.env.OPENSEA_PRIVATE_KEY!)
const auth = new OpenSeaAuth()
let token = await auth.authenticate(signer, { scopes: ["read:favorites"] })

try {
  token = await auth.getValidToken()
  const api = new OpenSeaAPI({
    apiKey: process.env.OPENSEA_API_KEY,
    authToken: token.accessToken,
  })
  await api.walletAuth.getFavorites(await signer.getAddress(), { limit: 10 })
  await api.walletAuth.markWalletAsAgent(await signer.getAddress())
  await api.walletAuth.removeWalletAgentDesignation(await signer.getAddress())
} finally {
  await auth.revoke(token.accessToken)
}

Keep the same OpenSeaAuth instance through revocation because it holds the SIWE session. revoke() accepts the current JWT as a guard, revokes its backing PAT, and clears the in-memory auth state. walletAuth provides typed helpers for every deployed scoped operation; request only the scopes the task needs. See the wallet-auth guide.

Cross-chain drop minting

Build the ordered transactions for paying on one chain and minting a drop on another through the typed API client:

const mint = await sdk.api.buildCrossChainDropMintTransactions("pyro-on-ape", {
  payer: "0x1111111111111111111111111111111111111111",
  minter: "0x1111111111111111111111111111111111111111",
  quantity: 1,
  payment: {
    chain: "base",
    tokenAddress: "0x0000000000000000000000000000000000000000",
  },
})

// Submit mint.transactions in order, then poll the exact returned request.
const receipt = await sdk.api.getTransactionReceipt(mint.receiptRequest)

Token activity stats

Read materialized trade count and USD volume for a token without aggregating raw events:

const activity = await sdk.api.getTokenActivityStats(
  Chain.Base,
  "0x4200000000000000000000000000000000000006",
  { windows: ["1h", "24h"] },
)

console.log(activity.windows["24h"]?.trades)
console.log(activity.windows["24h"]?.volumeUsd)

The SDK exposes camel-cased fields. A requested window is absent when the token has no swaps in that period.

Documentation

Security Warning

Do not use this SDK directly in client-side/frontend applications.

The OpenSea SDK requires an API key for initialization. If you embed your API key in frontend code (e.g., browser applications, mobile apps), it will be publicly exposed and could be extracted by anyone, leading to potential abuse and rate limit issues.

Recommended Architecture

For frontend applications that need to interact with OpenSea functionality:

  1. Create a backend API wrapper: Set up your own backend server that securely stores your OpenSea API key
  2. Call OpenSea SDK server-side: Use @opensea/sdk on your backend to interact with OpenSea's APIs
  3. Return data to your frontend: Send the necessary data (like transaction parameters) back to your frontend
  4. Execute transactions in the browser: Have users sign transactions with their own wallets (e.g., MetaMask) in the browser

Changelog

The changelog for recent versions can be found at: