Skip to content

Instantly share code, notes, and snippets.

@stevedylandev
Last active November 17, 2022 17:52
Show Gist options
  • Save stevedylandev/2f23f75dd53a061d8f39c886c880f1cf to your computer and use it in GitHub Desktop.
Save stevedylandev/2f23f75dd53a061d8f39c886c880f1cf to your computer and use it in GitHub Desktop.
A function that will upload to Pinata through a buffer
const axios = require("axios");
const axiosRetry = require("axios-retry");
const FormData = require("form-data");
const jwt = `Bearer PASTE_YOUR_JWT`
const uploadToPinata = async (sourceUrl) => {
const axiosInstance = axios.create();
axiosRetry(axiosInstance, { retries: 5 });
const data = new FormData();
const response = await axiosInstance(sourceUrl, {
method: "GET",
responseType: "stream",
});
data.append(`file`, response.data);
try {
const res = await axios.post("https://api.pinata.cloud/pinning/pinFileToIPFS", data, {
maxBodyLength: "Infinity",
headers: {
'Content-Type': `multipart/form-data; boundary=${data._boundary}`,
'Authorization': jwt
}
});
console.log(res.data);
} catch (error) {
console.log(error)
}
};
uploadToPinata("https://example.com/1.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment