node.jscloudipfspinata

Facing error in creating a group in Pinata IPFS cloud


Hello I'm trying to create a groups in pinata IPFS cloud, my script is running but it's files that are uploaded are not getting added into the group

This is my function to create group on pinata

const createGroupOnPinata = async (groupName) => {
  try {
    const { data } = await axios.post(
      "https://api.pinata.cloud/v3/groups/public",
      { name: groupName },
      {
        headers: {
          "Content-Type": "application/json",
          Authorization: `Bearer ${PINATA_JWT}`,
        },
      }
    );
    return data.data?.id ? data.data : null;
  } catch (error) {
    console.error("Error creating group on Pinata:", error.message);
    return null;
  }
};

Note: Group is getting created and I'm getting the ID

This is function to upload files to IPFS

const uploadFileToPinata = async (filePath, fileName, groupID) => {
  try {
    const url = "https://api.pinata.cloud/pinning/pinFileToIPFS";
    const data = new FormData();

    data.append("file", fs.createReadStream(filePath), { filename: fileName });
    data.append("pinataMetadata", JSON.stringify({ name: fileName }));
    data.append("network", "public");
    data.append("group", groupID);

    const response = await axios.post(url, data, {
      headers: {
        ...data.getHeaders(),
        Authorization: `Bearer ${PINATA_JWT}`,
      },
    });

    return response.data.IpfsHash;
  } catch (error) {
    console.error(`Error uploading file ${fileName} to Pinata:`, error.message);
    return null;
  }
};

Even this is working, but when I'm to fetch data with the group id [text]https://api.pinata.cloud/v3/files/public?group=${groupID} on postman I'm getting response as

{
    "data": {
        "files": [],
        "next_page_token": null
    }
}

I can't understand why is this happening can some help me please I tried transferring the file from pinata GUI and fetch from postman that's working and i'm getting files, but it's not working with script I happen to figure it out why


Solution

  • Based on Pinata's API documentation, the group id should be part of pinataOptions key in form data. So, you will need to do something like:

    const uploadFileToPinata = async (filePath, fileName, groupID) => {
      try {
        const url = "https://api.pinata.cloud/pinning/pinFileToIPFS";
        const data = new FormData();
    
        data.append("file", fs.createReadStream(filePath), {
          filename: fileName
        });
        
        data.append("pinataMetadata", JSON.stringify({
          name: fileName
        }));
        
        data.append("pinataOptions", JSON.stringify({
          groupId: groupID
        }));
    
        const response = await axios.post(url, data, {
          headers: {
            ...data.getHeaders(),
            Authorization: `Bearer ${PINATA_JWT}`,
          },
        });
    
        return response.data.IpfsHash;
      } catch (error) {
        console.error(`Error uploading file ${fileName} to Pinata:`, error.message);
        return null;
      }
    };

    Also, there's no mention of the network parameter, so might wanna remove that as well.