POST https://api.pinata.cloud/pinning/pinFileToIPFS 400 while uploading image

76 views Asked by At

I am trying to upload an image to pinata ipfs using nextjs but am unable to do, It always shows error 400

Here is my code, I checked lot of sites or applied the same code still getting the same error

export const pinFileToIPFS = async (file, pinataMetaData) => {
  const url = `https://api.pinata.cloud/pinning/pinFileToIPFS`;

  const data = new FormData();
  console.log("in pinata js");
  console.log(file.name, pinataMetaData);

  data.append("file", file, `${file.name}.jpg`);
  data.append("pinataMetadata", JSON.stringify(pinataMetaData));

  console.log("data", data);
  //axios calling

  await axios
    .post(url, data, {
      maxBodyLength: "Infinity",
      headers: {
        "Content-Type": `multipart/form-data; boundary=${data._boundary}`,
        Authorization: `Beared ${process.env.PINATA_JWT}`,
        pinata_api_key: `${key}`,
        pinata_secret_api_key: `${secret}`,
      },
    })
    .then(function (response) {
      return response.data.IpfsHash;
    })
    .catch(function (error) {
      console.log(error);
    });
};

I tried using a new API key, changed the max use limit, and tried the same code from various docs and youtube

1

There are 1 answers

0
Robin Thomas On

Since you are already using pinata_api_key and pinata_secret_api_key headers, you don't have to use Authorization header.

So change your code to:

await axios
    .post(url, data, {
        headers: {
            "Content-Type": "multipart/form-data",
            pinata_api_key: key,
            pinata_secret_api_key: secret,
        },
    })
    .then(function (response) {
        return response.data.IpfsHash;
    })
    .catch(function (error) {
        console.log(error);
    });

You also don't need the maxBodyLength or boundary. Axios will set that automatically as required.

Also verify that pinataMetadata conforms to the standard:

{
    "name": "aCustomNameForYourUpload",
    "keyvalues": {
        "customKey": "customValue",
        "customKey2": "customValue2"
    }
}

Please note that while both “name” and “keyvalues” are optional, if you include the “pinataMetadata” object in your API request, you need to include at least ONE of the two pieces of metadata inside the “pinataMetadata” object*.

Please also note that if you do not provide a custom “name” for your file, the “name” value will automatically be set to the original name of the file being uploaded.

The custom “keyvalue” entries can be any of the following types:

  • A string of alphanumeric characters
  • An number (in the form of an integer or decimal)
  • An ISO_8601 date (this is still a string, but we can actually query on dates in this format)

https://medium.com/pinata/how-to-pin-to-ipfs-effortlessly-ba3437b33885