Skip to content

Instantly share code, notes, and snippets.

@chrismatthieu
Forked from polluterofminds/upload.js
Created October 31, 2021 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismatthieu/a065cfe67e43678d58e0dd6c07f5e915 to your computer and use it in GitHub Desktop.
Save chrismatthieu/a065cfe67e43678d58e0dd6c07f5e915 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