Fetching Data with Arweave JS
Arweave JS is the official JavaScript/TypeScript SDK for interacting with the Arweave network.
This guide covers the basics of fetching data from known transaction IDs.
Installation
NPM
npm install --save arweave
Initialization
Basic Initialization
import Arweave from "arweave";
// Initialize with default settings (recommended for web)
const arweave = Arweave.init({});
// Or specify a custom gateway
const arweave = Arweave.init({
host: "arweave.net",
port: 443,
protocol: "https",
});
Fetching Data
Get Transaction Data
The recommended way to fetch transaction data:
const txId = "sHqUBKFeS42-CMCvNqPR31yEP63qSJG3ImshfwzJJF8";
// Get data as base64url encoded string
arweave.transactions.getData(txId).then((data) => {
console.log("Raw data:", data);
});
// Get data decoded to Uint8Array (for binary data)
arweave.transactions.getData(txId, { decode: true }).then((data) => {
console.log("Decoded data:", data);
});
// Get data decoded as string
arweave.transactions
.getData(txId, { decode: true, string: true })
.then((data) => {
console.log("String data:", data);
});
Get Full Transaction
Fetch complete transaction information including metadata:
const txId = "sHqUBKFeS42-CMCvNqPR31yEP63qSJG3ImshfwzJJF8";
arweave.transactions.get(txId).then((transaction) => {
console.log("Transaction ID:", transaction.id);
console.log("Owner:", transaction.owner);
console.log("Data size:", transaction.data_size);
console.log("Tags:", transaction.tags);
// Note: data field may not be included for large transactions
if (transaction.data) {
console.log("Data included:", transaction.data);
}
});
Working with Tags
Decode Transaction Tags
Tags are base64url encoded by default. Use the built-in decoder:
const txId = "sHqUBKFeS42-CMCvNqPR31yEP63qSJG3ImshfwzJJF8";
arweave.transactions.get(txId).then((transaction) => {
transaction.tags.forEach((tag) => {
// Decode tag name and value
const key = tag.get("name", { decode: true, string: true });
const value = tag.get("value", { decode: true, string: true });
console.log(`${key}: ${value}`);
});
});
Working with Different Data Types
JSON Data
const txId = "sHqUBKFeS42-CMCvNqPR31yEP63qSJG3ImshfwzJJF8";
arweave.transactions
.getData(txId, { decode: true, string: true })
.then((data) => {
try {
const jsonData = JSON.parse(data);
console.log("JSON data:", jsonData);
} catch (e) {
console.log("Data is not valid JSON:", data);
}
});
Binary Data
const txId = "sHqUBKFeS42-CMCvNqPR31yEP63qSJG3ImshfwzJJF8";
arweave.transactions.getData(txId, { decode: true }).then((data) => {
console.log("Binary data size:", data.byteLength);
// data is a Uint8Array
});
Best Practices
- Use
getData()instead ofget()when you only need the data - Always handle potential errors when fetching data
- Check transaction status before processing data for critical operations
- Use appropriate decoding options based on your expected data type
Resources
- Arweave JS GitHub Repository
- GraphQL Guide - For finding transactions