Usually you can pass a Blob directly into the request but to help guarantee success we recommend passing it into a File object.
Pin Blob
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()
URL
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.
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.
import fs from "fs"import FormData from "form-data"import rfs from "recursive-fs"import basePathConverter from "base-path-converter"import got from 'got'const pinDirectoryToPinata = async () => { const url = `https://api.pinata.cloud/pinning/pinFileToIPFS`; const src = "PATH_TO_FOLDER"; var status = 0; try { const { dirs, files } = await rfs.read(src); let data = new FormData(); for (const file of files) { data.append(`file`, fs.createReadStream(file), { filepath: basePathConverter(src, file), }); } const response = await got(url, { method: 'POST', headers: { "Authorization": "Bearer PINATA_API_JWT" }, body: data }) .on('uploadProgress', progress => { console.log(progress); }); console.log(JSON.parse(response.body)); } catch (error) { console.log(error); }};pinDirectoryToPinata()