guide March 26, 2025
Upload Files On-Chain
In this guide, we will learn how to upload files on-chain using Solana, Irys, and TypeScript.
- Solana: A fast and cheap decentralized blockchain for payments.
- Irys: A fast and cheap decentralized blockchain for storage.
System Requirements
To follow this guide, you will need to have the following installed.Install Bun (or Node.js)
curl -fsSL https://bun.sh/install | bashInstall Solana CLI
curl --proto '=https' --tlsv1.2 -sSfL https://solana-install.solana.workers.dev | bashSetup Project
Open your terminal and navigate to the directory where you want to create your project and initialize a new project.
mkdir my-project
cd my-project
bun initAdd Dependencies
Once initialized, add the following dependencies to your project.
bun add @solana/web3.js @irys/sdk @irys/upload-solanaUpdate Scripts
Open the
package.json that was created during initialization and add a scripts block with the following.{
"scripts": {
"upload": "bun run index.ts",
"fund": "bun run index.ts fund",
"keypair": "solana-keygen new --outfile ./.env.keypair",
}
}Generate Keypair
Above we added a script that will generate a new keypair for us and write it to the file./.env.keypair.
_Warning: Running the script again will overwrite any existing keypair._
bun keypairGet Devnet Solana Tokens
We need SOL in our wallet to get started. For development purposes, you can get free devnet SOL from 76 Dev Discord, Solana Devnet Faucet or you can try running the following command.solana airdrop 2 --keypair ./.env.keypairAdd Upload Logic
Create anindex.ts at the root of your project and copy/paste the following.
import { Uploader } from "@irys/upload";
import { Solana } from "@irys/upload-solana";
import fs from "fs";
const config = {
// Update this RPC url to a mainnet RPC when you're ready
// (https://app.extrnode.com/)
rpc: "https://api.devnet.solana.com",
keypair: "./.env.keypair",
uploadsDirectory: "./uploads",
outputDirectory: "./output",
};
const privateKey = JSON.parse(fs.readFileSync(config.keypair, "utf8"));
const getIrysUploader = async () => {
const irysUploader = await Uploader(Solana).withWallet(privateKey).withRpc(config.rpc);
return irysUploader;
};
const fundIrysWallet = async (amount: number) => {
const irysUploader = await getIrysUploader();
const fundTx = await irysUploader.fund(irysUploader.utils.toAtomic(amount));
}
const uploadFolder = async () => {
// Will output to uploads-manifest.json and
// uploads-manifest.csv in current directory
console.log("Uploading files...");
console.log(`☁️ Upload Directory: ${config.uploadsDirectory}\n`);
const irysUploader = await getIrysUploader();
const receipt = await irysUploader.uploadFolder(config.uploadsDirectory, {
indexFile: "", // Optional index file (file the user will load when accessing the manifest)
batchSize: 50, // Number of items to upload at once
keepDeleted: false, // whether to keep now deleted items from previous uploads
}); // Returns the manifest ID
console.log(`Files uploaded. Manifest ID ${receipt?.id}`);
fs.writeFileSync("uploads-manifest.json", JSON.stringify(receipt, null, 2));
}
if(process.argv[2] === "fund") {
fundIrysWallet(0.005);
} else if(process.argv[2] === "upload") {
uploadFolder();
}Upload Directory
Funding Irys Account
If this is your first upload or you've done a few uploads, it's probably time to top off your storage account using your Solana wallet. You can do so by running thebun fund script we made earlier.
Start Upload
Create a new folder calleduploads in the root of your project and place files to be uploaded. Run the following script to upload using the config in index.ts.
bun upload