Quick start
Upload, pin, and retrieve your first file with Meshkit in under 5 minutes.
1. Import and initialize
import { init, setupGracefulShutdown } from '@ipfs-meshkit/meshkit';
const { meshkit, localNode } = await init({ localNode: true });
setupGracefulShutdown(localNode);localNode: true starts or attaches to a Kubo daemon on http://127.0.0.1:5001 and stores the repo in ./.ipfs.
2. Upload content
upload accepts a Uint8Array. Use TextEncoder for strings, or read a file with fs.readFile:
const bytes = new TextEncoder().encode('Hello, IPFS!');
const cid = await meshkit.upload(bytes);
console.log(cid); // Qm...3. Pin it
Pinned content is excluded from garbage collection:
await meshkit.pin(cid);4. Retrieve by CID
const retrieved = await meshkit.retrieve(cid);
console.log(new TextDecoder().decode(retrieved)); // Hello, IPFS!Full example
import { readFile } from 'node:fs/promises';
import { init, setupGracefulShutdown } from '@ipfs-meshkit/meshkit';
const { meshkit, localNode } = await init({ localNode: true });
setupGracefulShutdown(localNode);
const file = await readFile('./document.pdf');
const cid = await meshkit.upload(file);
await meshkit.pin(cid);
console.log('Uploaded and pinned:', cid);
console.log('Active nodes:', meshkit.activeNodes);Remote node (no local daemon)
If Kubo is already running on a server, skip localNode and pass the URL directly:
import { init } from '@ipfs-meshkit/meshkit';
const { meshkit } = await init({
nodes: ['http://192.168.1.100:5001'],
});
const cid = await meshkit.upload(new TextEncoder().encode('remote upload'));CommonJS
const { init } = require('@ipfs-meshkit/meshkit');
(async () => {
const { meshkit } = await init({ nodes: ['http://127.0.0.1:5001'] });
console.log(await meshkit.upload(Buffer.from('hello')));
})();