Created
August 25, 2021 14:17
-
-
Save polluterofminds/d4dec1f8e99eec06c651c505804d3b1d to your computer and use it in GitHub Desktop.
Pinata Upload JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getApiConfig = async () => { | |
const config = { | |
headers: { | |
Authorization: `Bearer ${PinataJWT}`, | |
} | |
}; | |
return config; | |
}; | |
export const handleUpload = async (selectedFiles, customName, wrapWithDirectory) => { | |
try { | |
const data = new FormData(); | |
if (customName && customName !== '') { | |
const metadata = JSON.stringify({ | |
name: customName | |
}); | |
data.append('pinataMetadata', metadata); | |
} | |
if (selectedFiles.length > 0) { | |
selectedFiles.forEach((file) => { | |
data.append(`file`, file); | |
}); | |
} else { | |
data.append('file', selectedFiles[0], selectedFiles[0].name); | |
} | |
if (wrapWithDirectory === true) { | |
const pinataOptions = JSON.stringify({ | |
wrapWithDirectory: true | |
}); | |
data.append('pinataOptions', pinataOptions); | |
} | |
const res = await axios.post(`https://api.pinata.cloud/pinning/pinfFileToIPFS`, data, getApiConfig()); | |
return res.data; | |
} catch (error) { | |
// Handle error | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment