I’m having trouble with my NFT marketplace project. The image upload to Pinata works fine, but I keep getting a 400 Bad Request error when trying to create an NFT. Here’s what I’ve done so far:
- Set up a function to upload images to IPFS using Pinata
- Created a function to mint NFTs with metadata
- Implemented a createSale function to handle the token creation
My API key and secret key are correct, and I can upload images without issues. The problem occurs when I call the createNFT function. It seems like the metadata isn’t being processed correctly.
Here’s a simplified version of my createNFT function:
async function mintNFT(name, description, price, imageUrl) {
const metadata = { name, description, image: imageUrl };
try {
const response = await axios.post('https://api.pinata.cloud/pinning/pinJSONToIPFS', metadata, {
headers: {
'Content-Type': 'application/json',
'pinata_api_key': MY_API_KEY,
'pinata_secret_api_key': MY_SECRET_KEY
}
});
const tokenURI = `ipfs://${response.data.IpfsHash}`;
await createSale(tokenURI, price);
} catch (error) {
console.error('Error creating NFT:', error);
}
}
Any ideas on what might be causing this 400 Bad Request error? I’ve double-checked my API credentials and the image upload works, so I’m stumped.
I encountered a similar issue while working on an NFT project. Have you verified that your metadata object is properly structured? Ensure all required fields are present and correctly formatted. Additionally, consider implementing rate limiting in your requests to Pinata’s API. They have usage limits that, when exceeded, can result in 400 errors.
Another potential cause could be the size of your metadata. If it’s too large, it might be rejected. Try to keep it concise. Also, double-check that your ‘imageUrl’ is a valid IPFS URI.
If the problem persists, you might want to explore using Pinata’s SDK instead of direct API calls. It can sometimes handle edge cases more gracefully. Lastly, don’t forget to review Pinata’s documentation for any recent changes or known issues that might affect your implementation.
hey there elias87! i’ve run into similar issues before with pinata and nft creation. have you tried logging the full error response from the api? sometimes there’s more detailed info in there that can help pinpoint the problem.
one thing that jumps out at me - are you sure the imageurl you’re passing into the metadata is in the correct format? pinata usually wants the full ipfs:// url for the image, not just the hash.
also, just a thought - maybe try adding a ‘pinataOptions’ object to your axios request? something like:
pinataOptions: {
cidVersion: 1
}
i’ve found that can sometimes help with compatibility issues.
what does your createsale function look like? could there be something funky going on there?
oh, and totally random, but have you checked your network connection? sometimes flaky internet can cause weird api errors that look like other problems.
let me know if any of that helps! always happy to brainstorm more if you’re still stuck 
bro, i feel ur pain. had the same headache last week. double check ur metadata format - make sure all fields r there n spelled right. also, try console.logging the response before createSale. might give u more clues. if nothin else works, maybe hit up pinata support? they can be pretty helpful sometimes.