Purchase NFT

This article will guide you through the process of purchasing an NFT using the Rarible SDK.

Prerequisite

Follow our Getting started guide to install needed libraries and instantiate SDK.

Buying an NFT

To buy an NFT, follow these steps

  1. Connect Wallet: Ensure the user's wallet is connected. This can be done using wallet connect protocols.
  2. Find an NFT which you want to purchase
  3. Execute purchase transaction using SDK
// Fetch NFT information from the API
// Replace X with contract address and Y with tokenId
const item = await sdk.apis.item.getItemById({ itemId: toItemId("ETHEREUM:X:Y") })

// This will buy 1 NFT using best (cheapest) order for the specified NFT
const tx = await sdk.order.buy({
  orderId: toOrderId(item.bestSellOrder.id),
  amount: 1, //amount of NFTs to purchase (in case it's multiple NFT, e.g. ERC-1155)
})

// Later on you can watch for transaction status using this tx instance
  1. Confirm transaction using your wallet
  2. After transaction is confirmed user who sent the transaction should be owner of the NFT

Conclusion

Purchasing an NFT using the Rarible SDK is a straightforward process. By following the steps outlined above, developers can easily integrate NFT purchase functionality into their applications. Remember to handle exceptions and errors appropriately to ensure a smooth user experience.

For more detailed information and advanced functionalities, refer to the Rarible SDK documentation.