List and buy NFTs in the marketplace

Buy and Sell NFTs in your marketplace with Shyf

Trading is an important aspect of any marketplace, be it a conventional one, or a specialized marketplace such as an NFT Marketplace. In this sample project tutorial, we will see, how we can, list, buy, and trade NFTs from the marketplace, which we created in the previous tutorial.

If you have missed our previous tutorial, you can find it here, make sure you set up a marketplace using SHYFT APIs before you continue on this tutorial.

If you have already gone through this tutorial, you can find the next tutorial for this series here.

Pre-requisites for using SHYFT APIs

Authentication: Getting your Shyft API key

x-api-key is an authentication parameter, which gives you access to SHYFT APIs. You can get your own API Key from the SHYFT website. Just signup with your email id here and you can get it for free.

List (sell) an NFT in the marketplace

SHYFT provides an entire set of marketplace APIs for performing all possible operations on an NFT Marketplace.

The API endpoint for listing an NFT on the marketplace

POST https://api.shyft.to/sol/v1/marketplace/list

The Request Header

{
    "Content_Type": "application/json",
    "x-api-key": "YOUR SHYFT API KEY"
}

The Request Parameters (body)

{
  "network": "devnet",
  "marketplace_address": "5p4Bua5tSsSoasdRJ95I1w5DiMSPfWcvMvnMVjPpZ6sJUb",
  "nft_address": "GpLzvmQYcQM3USHasdahoriEzmJLJ8AfKVdLFZRoVBsz",
  "price": 10,
  "seller_wallet": "GE4kh5FsCDWeJfasdasdkqKpCuYQxh8FYBiTJe"
}

Details of the parameters used:

Header

  1. x-api-key : The x-api-key is a parameter that is required to use SHYFT APIs. It is used by SHYFT for authorization purposes. You can get your own x-api-key for free from the SHYFT Website.

Body (All are required parameters)

  1. network: Selects the Solana blockchain network instance, can be devnet,testnet or mainnet-beta.

  2. marketplace_address: Address of the marketplace in which the NFT will be listed. In our case, this will be the marketplace_address of the marketplace we created in the previous tutorial.

  3. nft_address: Token address of the NFT to be listed on the marketplace.

  4. price: Price of the NFT at which it will be listed on the marketplace. The currency will be the marketplace currency.

  5. seller_wallet: Wallet address of the wallet from which the NFT will be listed, i.e. wallet address of the wallet.

We can directly execute this API call using JavaScript or any programming language, or we can use SHYFT's readAll API to fetch a list of all NFTs of our own wallet and use the NFT address from there to list to the marketplace. Wondering how to list all NFTs from your wallet using SHYFTs readAll API, read it here. Once this API request is successfully executed, we will have a response that should look somewhat like this.

{
  "success": true,
  "message": "Listing transaction created successfully",
  "result": {
    "network": "devnet",
    "marketplace_address": "5p4Bua5tSsSoasdRJ95I1w5DiMSPfWcvMvnMVjPpZ6sJUb",
    "seller_address": "GE4kh5FsCDWeJfasdasdkqKpCuYQxh8FYBiTJe",
    "price": 10,
    "nft_address": "GpLzvmQYcQM3USHasdahoriEzmJLJ8AfKVdLFZRoVBsz",
    "list_state": "G7accWXzKEvzqtA2vG5EZr1JTLpsEMvo6Kpt9sBhT8xq",
    "currency_symbol": "SFC",
    "encoded_transaction": "AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAkPA+Z8BRyNDL30G+lsh0lwSNNhbnN6HAnoyUtyAiN7Pg8ZTa3UjJFWTbVDHC886jP1qM+vXRbrQyLDeoBN7DPFTuCOy9VpQ/1bFLqVafsssQa0CmDXlG0szCd/vm3xYAjy61NPZoqzotVq1zh3FtGM1AobPdXo4vpihZLb/KSMAlH+aXsh63u97m9Zk1yHYYCbZADBoeziI3rn4oalp/iO+Q2/5soX1jASgm/taWpC+DJTiveCM+DXxaFA4Q7w0ETdAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA8R+yeVfMiObnMrg/7ZjH5/Gyx9ewfekqQYjXSofYb/svZPPwQLBQYfJpa5SKTa6RNOiUJY0/kX2he+jGM+hoMCmWThjy6RhVk6uQTc3IVRusBUckwgna71K0qHDpCEHv0JF6XdZMSX7FZQbbAqSYK9EOLzRA1UpKkCwXfh/XVIP2RX2EYRmx3j1oYfbFSMX0y9sE2+mXakJ/DVFgXv4yXBqfVFxh70WY12tQEVf3CwMEkxo8hVnWl27rLXwgAAAAGp9UXGSxcUSGMyUw9SvF/WNruCJuh/UTj29mKAAAAAAbd9uHXZaGT2cvhRs7reawctIXtX1s3kTqM9YV+/wCpY4c4ACg7P/CAYkG8uUdYoIF0PlYmgYiO9+OaxjLkXrICCQwABQsIBwMCBA4GCg0bM+aFpAF/g63+/f8A0O2QLgAAAAEAAAAAAAAACQUBAAYNDAnPayygS97DG/8="
  }
}

If successfully executed, the response should contain the encoded_transaction which we will have to sign in order to complete this transaction successfully. This encoded_transaction can be signed using the user wallet, or the user's private key. Find out how. Shyft provides a special package that provides the functionality to sign transactions in two ways, using the wallet via the frontend, or using the private key from the backend. Read our blog on How to sign transactions on Solana here.

Once successfully signed, The NFT will be listed for sale on the Marketplace. Once listed, we can also unlist the NFT from the marketplace so that it is no longer available for sale in the marketplace if needed. SHYFT also provides an API for this purpose.

Unlist NFT from the marketplace (Cancel sale)

API for unlisting an already listed NFT from the marketplace.

POST https://api.shyft.to/sol/v1/marketplace/unlist

Parameters :

{
  "network": "devnet",
  "marketplace_address": "5p4Bua5tSsSoasdRJ95I1w5DiMSPfWcvMvnMVjPpZ6sJUb",
  "list_state": "3S8nfVMFhewv8jdy54xqxMt2GekpCDFVF3zkWAF2EThf",
  "seller_wallet": "GE4kh5FsCDWeJfasdasdkqKpCuYQxh8FYBiTJe"
}

The parameters are similar to that of the list API, seller_wallet and seller_address are the same parameters. This API also returns anencoded_transaction, similar to the list API, which needs to be signed using the user wallet. Find out how?

Now, let's see how we can view all the listings of a particular marketplace.

Get all listings of a marketplace

One of the important functionalities of an NFT Marketplace is showcasing all the listed NFTs so that users have a visual input of the token before they proceed to buy. Also, there is no convenient way to understand, if an NFT has been successfully listed or not unless we are able to view it in active_listings. This is where getting all listings finds its application. Almost all marketplace UIs have a view that displays a list of all the NFTs available for purchase.

Let’s see how we can do that using SHYFT APIs. The API endpoint for getting all the listed NFTs in a marketplace

GET https://api.shyft.to/sol/v1/marketplace/active_listings?network=devnet&marketplace_address=5p4Bua5tSsSoasdRJ95I1w5DiMSPfWcvMvnMVjPpZ6sJUb

The Request Header

{
    "Content_Type": "application/json",
    "x-api-key": "YOUR SHYFT API KEY"
}

The Request Parameters

{
  "network": "devnet",
  "marketplace_address": "5p4Bua5tSsSoasdRJ95I1w5DiMSPfWcvMvnMVjPpZ6sJUb"
}

Here, network is the Solana blockchain network instance, and marketplace_address is the address of the marketplace whose active NFTs we will list. On successful execution, the response should return a list of all NFTs currently active on the marketplace. The term "currently active" or "active_listings" refers to the NFTs on the marketplace which have been listed and not yet been sold. On successful execution, the response received should look something like this.

{
  "success": true,
  "message": "Active listing fetched successfully",
  "result": [
    {
      "network": "devnet",
      "marketplace_address": "5p4Bua5tSsSoasdRJ95I1w5DiMSPfWcvMvnMVjPpZ6sJUb",
      "seller_address": "GE4kh5FsCDWeJfasdasdkqKpCuYQxh8FYBiTJe",
      "price": 10,
      "currency_symbol": "SD",
      "nft_address": "GpLzvmQYcQM3USHasdahoriEzmJLJ8AfKVdLFZRoVBsz",
			"nft": {
        "name": "Planet",
        "symbol": "PLA",
        "royalty": 5,
        "image_uri": "<https://nftstorage.link/ipfs/bafybeifa73zza4pff25fyhrjqgkatovwy25ppcqj76rkl7gp2ej74qpupe>",
        "cached_image_uri": "<https://cdn.shyft.to/img/https%253A%252F%252Fnftstorage.link%252Fipfs%252Fbafybeifa73zza4pff25fyhrjqgkatovwy25ppcqj76rkl7gp2ej74qpupe>",
        "metadata_uri": "<https://nftstorage.link/ipfs/bafkreidd7yzhlhokfpdjcujomkc4ntoxcoerdpqz34s3bs763odwux6tpi>",
        "description": "This is a planet NFT.",
        "mint": "GpLzvmQYcQM3USHa133123iEzmJLJ8AfKVdLFZRoVBsz",
        "owner": "5hbm5nSmtNFd12313G9XsC5rWXAREfPMG3Wj8Wxm6MD4",
        "creators": [
          {
            "address": "5hbm5nS4mtswyhWyG9XsC5rWXAREfPMG3Wj8Wxm6MD4",
            "share": 100,
            "verified": 1
          }
        ],
        "attributes": {
          "awesomeness": "100"
        },
        "attributes_array": [
          {
            "trait_type": "awesomeness",
            "value": "100"
          }
        ],
        "files": [],
        "update_authority": "5hbm5nSmtNFdwyhWyG9XsC5rWXAREfPMG3Wj8Wxm6MD4"
      },
      "list_state": "G7accWXzKEvzqtA2vG5EZr1JTLpsEMvo6Kpt9sBhT8xq",
      "created_at": "2022-08-22T17:16:06.000Z",
      "receipt": "FwEG6zTfM4mT9SaCMS61nswuJcfNDLEi2xn7T7gs4qRs"
    },
    {
      "network": "devnet",
      "marketplace_address": "5p4Bua5tSsSoasdRJ95I1w5DiMSPfWcvMvnMVjPpZ6sJUb",
      "seller_address": "GE4kh5FsCDWeJfqLsKx7zC9ijkqKpCuYQxh8FYBiTJe",
      "price": 300,
      "currency_symbol": "SD",
      "nft_address": "5r2rJ37qUGYCqqHzvjBTjMBh4Pu2VD9wSiUnsky8UzYS",
			"nft": {
        "name": "SperCoins",
        "symbol": "Super",
        "royalty": 5,
        "image_uri": "<https://nftstorage.link/ipfs/assdasd3zasdasdkatovwy25ppcqj76rkl7gp2ej74qpupe>",
        "cached_image_uri": "<https://cdn.shyft.to/img/https%253A%252F%252Fnftstorage.link%252Fipfs%252Fasd73zza4pff25fyhrjqgasdads25ppcqj76rkl7gp2ej74qpupe>",
        "metadata_uri": "<https://nftstorage.link/ipfs/bafasdawdsd7yzhlhokfpdjcujomkc4ntoxcoerdpqz34s3bs763odwux6tpi>",
        "description": "This is a planet NFT.",
        "mint": "GpLzvmQYcQM3USHa133123iEzmJLJ8Sgw12KVdLFZRoVBsz",
        "owner": "6yhsamtNFd12313G9XsC5rWXAREfPMG3Wj8Wxm6MD4",
        "creators": [
          {
            "address": "5hbm5nS4mtswyhWyG9XsC5rWXAREfPMG3Wj8Wxm6MD4",
            "share": 100,
            "verified": 1
          }
        ],
        "attributes": {
          "awesomeness": "100"
        },
        "attributes_array": [
          {
            "trait_type": "awesomeness",
            "value": "100"
          }
        ],
        "files": [],
        "update_authority": "5hbm5nSmtNFdwyhWyG9XsC5rWXAREfPMG3Wj8Wxm6MD4"
      },
      "list_state": "8WM1Etk7fWraMshaAgYc6jBBKVAGsPwwRnNWyke9o5yN",
      "created_at": "2022-08-22T17:20:17.000Z",
      "receipt": "D9qHwezut8c7rmLkaGE1h1Yo3fVTp9EKYnLRDFjupyA3"
    }
  ]
}

The result field in the response is an array that contains a list of all the currently listed NFTs in the marketplace.

Buying NFTs from a marketplace

The API endpoint for buying an NFT from the marketplace

POST https://api.shyft.to/sol/v1/marketplace/buy

The Request Header

{
    "Content_Type": "application/json",
    "x-api-key": "YOUR SHYFT API KEY"
}

The Request Parameters (body)

{
  "network": "devnet",
  "buyer_wallet": "AaYFExyZuMHbJHzjimK1232H1yfA9sKTxSzBc6Nr5X4s",
  "seller_address": "GE4kh5FsCDWeJfasdasdkqKpCuYQxh8FYBiTJe",
  "marketplace_address": "5p4Bua5tSsSoasdRJ95I1w5DiMSPfWcvMvnMVjPpZ6sJUb",
  "price": 10,
  "nft_address": "GpLzvmQYcQM3USHasdahoriEzmJLJ8AfKVdLFZRoVBsz"
}

Details of the parameters used:

Header

  1. x-api-key : The x-api-key is a parameter that is required to use SHYFT APIs. It is used by SHYFT for authorization purposes. You can get your own x-api-key for free from the SHYFT Website.

Body (All are required parameters)

  1. network : Selects the Solana blockchain network instance, which can be devnet,testnet or mainnet-beta.

  2. buyer_wallet : Wallet address of the wallet from which the NFT will be bought from the marketplace. The price of the NFT and the gas fees for the buy transaction will be deducted from this wallet. Please note that this cannot be the same as the seller_address. Also, the marketplace creator cannot buy NFTs from the marketplace.

  3. seller_address : Wallet address of the wallet from which the NFT will be bought. Once the buy is successful, the NFT will be removed from this wallet and the price at which the NFT was listed will be credited here, minus the marketplace transaction fees.

  4. marketplace_address : Address of the marketplace from which the NFT will be bought from. In our case, this will be the marketplace_address of the marketplace we created in the previous tutorial.

  5. price : Price of the NFT at which the NFT was listed on the marketplace. The currency is the marketplace currency.

  6. nft_address : Token address of the NFT that we are attempting to buy from the marketplace.

If all the parameters are correct and the request is successfully executed, the response should contain an encoded_transaction, which needs to be signed using the user wallet or using user's private key. Read more about signing transactions on Solana here

So, that's pretty much all about this tutorial about listing and buying items from the NFT marketplace using SHYFT APIs. So far, we have seen how we can set up an NFT marketplace with ease, Trade NFTs, and list all active items to a marketplace using SHYFT APIs.

In the next and final episode of this series, we will be fetching marketplace stats and treasury balance.

Resources

SHYFT API Documentation

Shyft Website

Get API Key

GitHub

Tool for Signing Transaction on Solana

Join our Discord

If you liked this, you can also read our tutorials on How to get your user's token balances? or How to sign transactions on Solana.

Last updated