Upload a single file to Pinata

HTML uploads are currently only available on paid plans with granted access. If you are on a paid plan and wish to upload HTML please send a request through our support chat or send an email to [email protected]

Usage

import { PinataSDK } from "pinata-web3";

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-web3")
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 PinResponse = {
  IpfsHash: string;
  PinSize: number;
  Timestamp: string;
  isDuplicate?: boolean;
};

Parameters

file

  • Type: File object

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)

addMetadata (Optional)

Add optional metadata to file

const upload = await pinata.upload
  .file(file)
  .addMetadata({
    name: "pinnie.png",
    keyValues: {
      whimsey: 100
    }
  })

group (Optional)

  • Type: string

Upload to a specific group by passing in the groupId

const upload = await pinata.upload
  .file(file)
  .group("b07da1ff-efa4-49af-bdea-9d95d8881103")

key (Optional)

  • Type: string

Upload a file using a secondary API key generated through keys.create()

const upload = await pinata.upload
  .file(file)
  .key("GENERATED_API_JWT")

cidVersion (Optional)

  • Type: 0 | 1

Specificy CID version for upload

const upload = await pinata.upload
  .file(file)
  .cidVersion(0)