This guide will walk you through the process of transferring NFTs from your wallet using the Crossmint transactions API.
Prerequisites
Ensure you have a wallet created. You can follow the Quickstart for server
wallets to prepare one. You will need:
API Key : Ensure you have an API key with the scope: wallets:transactions.create
Wallet Address : The locator of the wallet you want to transfer the NFT from.
NFT Details : The contract address and token ID of the NFT you want to transfer.
Sending the Transaction
We will use the Create Transaction API.
First, we need to prepare the transaction data that will encode the transferFrom
function call for the NFT contract:
prepareTransaction.ts
prepareTransaction.py
import { encodeFunctionData } from 'viem' ;
const fromAddress = '0x...' ;
const toAddress = '0x...' ;
const tokenId = '1' ;
const data = encodeFunctionData ( {
abi: [ {
name: 'transferFrom' ,
type: 'function' ,
inputs: [
{ name: 'from' , type: 'address' } ,
{ name: 'to' , type: 'address' } ,
{ name: 'tokenId' , type: 'uint256' }
] ,
outputs: [ ] ,
stateMutability: 'nonpayable'
} ] ,
args: [ fromAddress, toAddress, tokenId]
} ) ;
console . log ( data) ;
Now we can create the transaction using the encoded data:
sendTransaction.ts
sendTransaction.py
const walletLocator = '0x...' ;
const apiKey = 'sk_staging...' ;
const signerAddress = '0x...' ;
async function sendTransaction ( ) {
const response = await fetch (
` https://api.crossmint.com/2022-06-09/wallets/ ${ walletLocator} /transactions ` ,
{
method: 'POST' ,
headers: {
'Content-Type' : 'application/json' ,
'X-API-KEY' : apiKey,
} ,
body: JSON . stringify ( {
params: {
calls: [ {
to: nftContractAddress,
value: '0' ,
data: data
} ] ,
chain: 'polygon-amoy' ,
} ,
} ) ,
}
) ;
return await response. json ( ) ;
}
First, we need to prepare the transaction data that will encode the transferFrom
function call for the NFT contract:
prepareTransaction.ts
prepareTransaction.py
import { encodeFunctionData } from 'viem' ;
const fromAddress = '0x...' ;
const toAddress = '0x...' ;
const tokenId = '1' ;
const data = encodeFunctionData ( {
abi: [ {
name: 'transferFrom' ,
type: 'function' ,
inputs: [
{ name: 'from' , type: 'address' } ,
{ name: 'to' , type: 'address' } ,
{ name: 'tokenId' , type: 'uint256' }
] ,
outputs: [ ] ,
stateMutability: 'nonpayable'
} ] ,
args: [ fromAddress, toAddress, tokenId]
} ) ;
console . log ( data) ;
Now we can create the transaction using the encoded data:
sendTransaction.ts
sendTransaction.py
const walletLocator = '0x...' ;
const apiKey = 'sk_staging...' ;
const signerAddress = '0x...' ;
async function sendTransaction ( ) {
const response = await fetch (
` https://api.crossmint.com/2022-06-09/wallets/ ${ walletLocator} /transactions ` ,
{
method: 'POST' ,
headers: {
'Content-Type' : 'application/json' ,
'X-API-KEY' : apiKey,
} ,
body: JSON . stringify ( {
params: {
calls: [ {
to: nftContractAddress,
value: '0' ,
data: data
} ] ,
chain: 'polygon-amoy' ,
} ,
} ) ,
}
) ;
return await response. json ( ) ;
}
First, we need to prepare the transaction data for transferring the NFT. We will be transferring a compressed NFT . As the inforormation is stored off-chain, we will need to connect to the Solana blockchain using an RPC provider that supports the Digital Asset Standard . We suggest using Helius as it has a free tier. Sign up and get the Devnet RPC URL.
import { mplBubblegum, getAssetWithProof, transfer } from "@metaplex-foundation/mpl-bubblegum" ;
import { createUmi } from "@metaplex-foundation/umi-bundle-defaults" ;
import { publicKey } from "@metaplex-foundation/umi" ;
import { dasApi } from "@metaplex-foundation/digital-asset-standard-api" ;
import bs58 from "bs58" ;
const assetId = "your_compressed_nft_mint" ;
const senderAddress = "your_wallet_address" ;
const recipientAddress = "recipient_address" ;
const umi = createUmi ( "helius_devnet_rpc_url" )
. use ( mplBubblegum ( ) )
. use ( dasApi ( ) ) ;
const assetWithProof = await getAssetWithProof ( umi, publicKey ( assetId) , { truncateCanopy: true } ) ;
const transferBuilder = transfer ( umi, {
... assetWithProof,
leafOwner: publicKey ( senderAddress) ,
newLeafOwner: publicKey ( recipientAddress) ,
} ) ;
const transaction = umi. transactions. create ( {
version: 0 ,
instructions: transferBuilder. getInstructions ( ) ,
blockhash: "11111111111111111111111111111111" ,
payer: publicKey ( "11111111111111111111111111111112" ) ,
} ) ;
const serializedTransaction = umi. transactions. serialize ( transaction) ;
const base58Transaction = bs58. encode ( serializedTransaction) ;
console . log ( base58Transaction) ;
Now we can send the transaction:
sendTransaction.ts
Response
const walletLocator = 'EFeH...' ;
const apiKey = 'sk_staging...' ;
async function sendTransaction ( ) {
const serializedTransaction = await prepareTransaction ( ) ;
const response = await fetch (
` https://api.crossmint.com/2022-06-09/wallets/ ${ walletLocator} /transactions ` ,
{
method: 'POST' ,
headers: {
'Content-Type' : 'application/json' ,
'X-API-KEY' : apiKey,
} ,
body: JSON . stringify ( {
params: {
transaction: serializedTransaction
}
} ) ,
}
) ;
return await response. json ( ) ;
}