Upload a single file to Pinata
Usage
import { PinataSDK } from "pinata";
const pinata = new PinataSDK({
pinataJwt: process.env.PINATA_JWT!,
pinataGateway: "example-gateway.mypinata.cloud",
});
const file = new File(["hello world!"], "hello.txt", { type: "text/plain" })
const upload = await pinata.upload.file(file)
Local Files
If you need to upload files from a local file source you can use fs
to feed a file into a blob
, then turn that blob
into a File
.
const { PinataSDK } = require("pinata")
const fs = require("fs")
const { Blob } = require("buffer")
const pinata = new PinataSDK({
pinataJwt: process.env.PINATA_JWT!,
pinataGateway: "example-gateway.mypinata.cloud"
})
const blob = new Blob([fs.readFileSync("./hello-world.txt")]);
const file = new File([blob], "hello-world.txt", { type: "text/plain"})
const upload = await pinata.upload.file(file);
Returns
type UploadResponse = {
id: string;
name: string;
cid: string;
size: number;
number_of_files: number;
mime_type: string;
user_id: string;
};
Parameters
file
Accepts a File object in accordance with the W3C File API.
const blob = new Blob(["hello world!"], { type: "text/plain" })
const file = new File([blob], "hello.txt", { type: "text/plain" })
const upload = await pinata.upload.file(file)
In most environments you can also pass a Blob here as well.
const blob = new Blob(["hello world!"], { type: "text/plain" })
const upload = await pinata.upload.file(blob)
group (Optional)
Upload to a specific group by passing in the groupId
const upload = await pinata.upload
.file(file)
.group("b07da1ff-efa4-49af-bdea-9d95d8881103")
Add optional metadata to file
const upload = await pinata.upload
.file(file)
.addMetadata({
name: "hello.txt",
keyvalues: {
env: "prod"
}
})
key (Optional)
Upload a file using a secondary API key generated through keys.create()
const upload = await pinata.upload
.file(file)
.key("GENERATED_API_JWT")