Irys SDK
Installing
Install using npm:
npm install @irys/sdk
or yarn:
yarn add @irys/sdk
If you get a warning saying bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)
during
install, it can be safely ignored. For details on how make it go away, see our troubleshooting
guide.
Importing
import Irys from "@irys/sdk";
Connecting to Irys
Connect to any our mainnet or devnet, using a serialized JWK file when using an Arweave wallet:
const getIrysArweave = async () => {
const network = "mainnet";
const token = "arweave";
const key = JSON.parse(fs.readFileSync("arweaveWallet.json").toString());
const irys = new Irys({
network, // "mainnet" or "devnet"
token, // Token used for payment and signing
key, // Arweave wallet
});
return irys;
};
Or a private key when using an EVM or Solana wallet:
const getIrys = async () => {
const network = "mainnet";
// Devnet RPC URLs change often, use a recent one from https://chainlist.org/
const providerUrl = "";
const token = "ethereum";
const irys = new Irys({
network, // "mainnet" or "devnet"
token, // Token used for payment
key: process.env.PRIVATE_KEY, // ETH or SOL private key
config: { providerUrl }, // Optional provider URL, only required when using Devnet
});
return irys;
};
Provider URLs
Use the providerUrl
parameter to specify an RPC provider.
- When working with Nodes 1 and 2, this value is optional and will default to the main public RPC provider for the chosen chain.
- When working with Devnet, this parameter is required.
- When working with Aptos, use one of "mainnet" | "testnet" | "devnet" instead of a URL.
Funding a node
Fund a node using any of our supported tokens:
const fundNode = async () => {
const irys = await getIrys();
try {
const fundTx = await irys.fund(irys.utils.toAtomic(0.05));
console.log(`Successfully funded ${irys.utils.fromAtomic(fundTx.quantity)} ${irys.token}`);
} catch (e) {
console.log("Error uploading data ", e);
}
};
Uploading
Data uploaded to Irys is given a millisecond-accurate timestamp, attributes and authorship details before being passed to Arweave for permanent storage. This information is used to create a signed receipt that be trustlessly verified.
Uploading data
const uploadData = async () => {
const irys = await getIrys();
const dataToUpload = "GM world.";
try {
const receipt = await irys.upload(dataToUpload);
console.log(`Data uploaded ==> https://gateway.irys.xyz/${receipt.id}`);
} catch (e) {
console.log("Error uploading data ", e);
}
};
Uploading a file
const uploadFile = async () => {
const irys = await getIrys();
// Your file
const fileToUpload = "./myImage.png";
const tags = [{ name: "application-id", value: "MyNFTDrop" }];
try {
const receipt = await irys.uploadFile(fileToUpload, { tags: tags });
console.log(`File uploaded ==> https://gateway.irys.xyz/${receipt.id}`);
} catch (e) {
console.log("Error uploading file ", e);
}
};
Uploading a folder
You can upload a group of files as a single transaction from both the server and the browser.
When uploading a folder, files can be accessed either directly at
https://gateway.irys.xyz/[transaction-id]
or https://gateway.irys.xyz/[manifest-id]/[file-name]
Server
const uploadFolder = async () => {
const irys = await getIrys();
// Upload an entire folder
const folderToUpload = "./my-images/"; // Path to folder
try {
const receipt = await irys.uploadFolder("./" + folderToUpload, {
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}`);
} catch (e) {
console.log("Error uploading file ", e);
}
};
Browser
const webIrys = await getWebIrys();
const files: File[] = [];
const tags: { name: string; value: string }[][] = [];
// Convert Files to TaggedFiles
const taggedFiles = files.map((f: TaggedFile, i: number) => {
f.tags = tags[i];
return f;
});
const response = await webIrys.uploadFolder(taggedFiles);
Querying
In addition to using the query package to search Irys and Arweave, you can also search directly from the Irys SDK. The following shows how to search for all transactions posted to Irys mainnet, paid for with Matic, and uploaded during a three day period.
For more details on all search functions, see query package documentation.
const result = await irys
.search("irys:transactions")
.network("mainnet")
.token("ethereum")
.fromTimestamp(new Date("2023-07-01"))
.toTimestamp(new Date("2023-07-03"));
3rd party build tools
Parcel
If using Parcel (opens in a new tab), you will need to manually enable package exports (opens in a new tab) by adding the following to the package.json
file in your project root directory.
{
"@parcel/resolver-default": {
"packageExports": true
}
}