Skip to content

Instantly share code, notes, and snippets.

@polluterofminds
Created August 25, 2021 14:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save polluterofminds/d4dec1f8e99eec06c651c505804d3b1d to your computer and use it in GitHub Desktop.
Save polluterofminds/d4dec1f8e99eec06c651c505804d3b1d to your computer and use it in GitHub Desktop.
Pinata Upload JavaScript
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