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
Since you are already using
pinata_api_keyandpinata_secret_api_keyheaders, you don't have to useAuthorizationheader.So change your code to:
You also don't need the
maxBodyLengthor boundary. Axios will set that automatically as required.Also verify that
pinataMetadataconforms to the standard: