The Files SDK is an all-in-one tool for everything you might need, from uploading content, using Gateways, even user or group management!
Looking for our IPFS SDK? You can find it here!
1. Installation and Setup
Install with your package manager of choice
Import and initialize the SDK in your codebase with the following variables
import { PinataSDK } from "pinata";
const pinata = new PinataSDK({
pinataJwt: "PINATA_JWT",
pinataGateway: "example-gateway.mypinata.cloud",
});
The PINATA_JWT
is a secret key, be sure to initialize the SDK in a secure environment and practice basic variable security practices. If you need to upload from a client environment, consider using signed JWTs
2. Upload a File
import { PinataSDK } from "pinata";
const pinata = new PinataSDK({
pinataJwt: process.env.PINATA_JWT!,
pinataGateway: "example-gateway.mypinata.cloud",
});
async function main() {
try {
const file = new File(["hello"], "Testing.txt", { type: "text/plain" });
const upload = await pinata.upload.file(file);
console.log(upload);
} catch (error) {
console.log(error);
}
}
await main();
This will return an object like the following
{
id: "41bc6820-43d3-46f8-a9e0-e53e8adea2e8",
name: "hello.txt",
cid: "bafkreid7qoywk77r7rj3slobqfekdvs57qwuwh5d2z3sqsw52iabe3mqne",
size: 12,
number_of_files: 1,
mime_type: "text/plain;charset=utf-8",
user_id: "cb7c7bd8-7deb-479d-8750-34a20f2adb3b",
group_id: null,
}
3. Retrieve a File
Use the cid
of a file to fetch it through a Gateway, or create a signed URL.
import { PinataSDK } from "pinata";
const pinata = new PinataSDK({
pinataJwt: process.env.PINATA_JWT!,
pinataGateway: "example-gateway.mypinata.cloud",
});
async function main() {
try {
const data = await pinata.gateways.get("bafkreibm6jg3ux5qumhcn2b3flc3tyu6dmlb4xa7u5bf44yegnrjhc4yeq");
console.log(data)
const url = await pinata.gateways.createSignedURL({
cid: "bafkreib4pqtikzdjlj4zigobmd63lig7u6oxlug24snlr6atjlmlza45dq",
expires: 1800,
})
console.log(url)
} catch (error) {
console.log(error);
}
}
main();