import { PinataSDK } from "pinata";
const pinata = new PinataSDK({
pinataJwt: process.env.PINATA_JWT!,
pinataGateway: "example.mypinata.cloud", // Gateway has Hot Swaps installed
});
async function main() {
try {
// Upload the first file
const file = new File(["The original CID"], "cid.txt", {
type: "text/plain",
});
const { IpfsHash: CID1 } = await pinata.upload.public.file(file);
console.log("This is the original CID hash: ", CID1);
// Upload a second file
const file2 = new File(["The new CID"], "cid.txt", { type: "text/plain" });
const { IpfsHash: CID2 } = await pinata.upload.public.file(file2);
console.log("This is the new CID hash: ", CID2);
// Create the swap, so when we visit CID1 we will get the content of CID2
const swap = await pinata.files.public.addSwap({
cid: CID1,
swapCid: CID2,
});
console.log("Swap created: ", swap);
// Fetch CID1 through our gateway that has Hot Swaps installed, get the content of CID2
const data = await pinata.gateways.public.get(CID1);
console.log("Result of requestingt CID1 through the gateway: ", data);
} catch (error) {
console.log(error);
}
}
main();