Pin File to IPFS
Upload a file to Pinata to be pinned to IPFS
Bearer authentication header of the form Bearer <token>
, where <token>
is your auth token.
Optional stringified object
Optional stringified object
Guides
Usually you can pass a Blob directly into the request but to help guarantee success we recommend passing it into a File
object.
const JWT = 'YOUR_PINATA_JWT';
async function pinFileToIPFS() {
try {
const text = "Hello World!";
const blob = new Blob([text], { type: "text/plain" });
const file = new File([blob], "hello-world.txt")
const data = new FormData();
data.append("file", file);
const res = await fetch("https://api.pinata.cloud/pinning/pinFileToIPFS", {
method: "POST",
headers: {
Authorization: `Bearer ${JWT}`,
},
body: data,
});
const resData = await res.json();
console.log(resData);
} catch (error) {
console.log(error);
}
};
await pinFileToIPFS()
To upload a file from an external URL you can stream the contents into an arrayBuffer
, which then gets passed into a new Blob
that can then be uploaded to Pinata.
const JWT = "YOUR_PINATA_JWT";
async function uploadByURL(url) {
try {
const urlStream = await fetch(url);
const arrayBuffer = await urlStream.arrayBuffer();
const blob = new Blob([arrayBuffer]);
const file = new File([blob], "file")
const data = new FormData();
data.append("file", file);
const upload = await fetch(
"https://api.pinata.cloud/pinning/pinFileToIPFS",
{
method: "POST",
headers: {
Authorization: `Bearer ${JWT}`,
},
body: data,
}
);
const uploadRes = await upload.json();
console.log(uploadRes);
} catch (error) {
console.log(error);
}
}
await uploadByURL("https://pocketcast.cloud/og.png");
To upload a file in base64 simply turn the contents into a buffer
that is passed into a Blob
.
const JWT = "YOUR_PINATA_JWT";
async function uploadBase64(base64String) {
try {
const buffer = Buffer.from(base64String, "base64");
const blob = new Blob([buffer]);
const file = new File([blob], "file");
const data = new FormData();
data.append("file", file);
const upload = await fetch(
"https://api.pinata.cloud/pinning/pinFileToIPFS",
{
method: "POST",
headers: {
Authorization: `Bearer ${JWT}`,
},
body: data,
},
);
const uploadRes = await upload.json();
console.log(uploadRes);
} catch (error) {
console.log(error);
}
}
await uploadBase64("SGVsbG8gZnJvbSBQaW5hdGEhIDop");
Folders can also be uploaded via the API by creating an array of files and mapping over them to add them to the form data. This is different then having a single file
entry and having multiple files for that one entry, which does not work.
Authorizations
Bearer authentication header of the form Bearer <token>
, where <token>
is your auth token.