Search code examples
nearprotocol

Getting 500 Internal Server Error when calling /mint_nft NEAR REST API


I have cloned this NEAR REST API SERVER repo into my local system and haven't made any changes to its original code. Only added console logs for debugging purpose. Using its API, I have successfully deployed a contract to testnet account and also called that contract.

But when comes to NFT part, its not working. I'm calling /mint_nft API and getting 500 Internal Server Error.

enter image description here

It's breaking here:

let create_token = await token.ViewNFT(tokenId, contract);

create_token is null.

enter image description here


If I do minting with NEAR CLI, its working fine and I can see that NFT in my testnet wallet..

near call $ID nft_mint '{"token_id": "02", "receiver_id": "'$ID'", "token_metadata": { "title": "Design #2", "description": "Art Design", "media": "https://ipfs.io/ipfs/Qme7ss3ARVgxv6rXqVPiikMJ8u2NLgmgszg13pYrDKEoiu", "copies": 1}}' --accountId $ID --deposit 0.1

enter image description here


near-api-server.config.json

{
  "server_host": "localhost",
  "server_port": 9000,
  "rpc_node": "https://rpc.testnet.near.org",
  "init_disabled": false,
  "master_account_id": "parimal9.testnet",
  "master_key": "ed25519:2bUSD2v88RiwznraL1ZYtduH2rsJqbrE8K6QT2asnsPnXHc171Qj4khvxFxZqb6AN6zyiZzo4j7f9amooFi7kJX1",
  "nft_contract": "nft.parimal9.testnet"
}


I have also tried this way but still not working.

enter image description here


server.route({
        method: 'POST',
        path: '/mint_nft',
        handler: async (request) => {
            console.log('\n\n\nMINT_NFT_PAYLOAD:', request.payload);
            let {min, max} = request.payload;

            if (!min || !max) min = max = 0;
            let response = [];

            request = processRequest(request);
            for (let i = min; i <= max; i++) {
                const tokenId = request.payload.token_id.replace('{inc}', i);
                console.log('tokenId:', tokenId);

                let {account_id, private_key, metadata, contract} = request.payload;

                const tx = await token.MintNFT(
                    tokenId,
                    metadata,
                    contract,
                    account_id,
                    private_key
                );

                if (tx) {
                    if (min === max) {
                        console.log('tokenId:', tokenId);
                        let create_token = await token.ViewNFT(tokenId, contract);
                        console.log('create_token:', create_token, '\n\n\n');
                        create_token.token_id = tokenId;
                        console.log('create_token.token_id:', tokenId, '\n\n\n');
                        response.push({token: create_token, tx: tx});
                    } else {
                        response.push({tx: tx});
                    }
                } else {
                    response.push({text: 'Error. Check backend logs.'});
                }
            }

            return response;
        },
    });


Solution

  • There are several issues in the existing repository for /mint_nft.

    The REST-API implemented doesn't follow the correct format when minting an NFT. I have created a PR to address the issue

    In short, the function call to mint an NFT is missing one argument, receiver_id, and the format and name of metadata is not correct. It should be token_metadata: {} // type is object, not string.

    If we change the postData to

    {
        "token_id": "foo",
        "token_metadata": {
            "media": "https://ipfs.io/ipfs/Qme7ss3ARVgxv6rXqVPiikMJ8u2NLgmgszg13pYrDKEoiu"
        },
        "account_id": "parimal9.testnet",
        "private_key": "yourPrivateKey",
        "contract": "parimal9.testnet"
    }
    

    and make a few changes to the server code, which you can see in the PR I made, we should be able to call /mint_nft from the server.