Quick Start
Get Swarm ID integrated into your dApp in just a few steps.
Installation
Section titled “Installation”# Using pnpm (recommended)pnpm add @snaha/swarm-id
# Using npmnpm install @snaha/swarm-id
# Using yarnyarn add @snaha/swarm-idBasic Usage
Section titled “Basic Usage”import { SwarmIdClient } from '@snaha/swarm-id'
// 1. Create the clientconst client = new SwarmIdClient({ iframeOrigin: 'https://swarm-id.snaha.net', metadata: { name: 'My dApp', description: 'A demo Swarm application', }, onConnectionChange: (info) => { console.log('Connection changed:', info.identity?.name, 'canUpload=', info.canUpload) },})
// 2. Initialize (creates hidden iframe; populates client.connectionInfo)await client.initialize()
// 3a. Option A: Use iframe authentication button// The iframe will show a connect/disconnect button automatically
// 3b. Option B: Manual authentication with connect()// Opens the authentication page in a new window/tab (useful for custom UI)await client.connect()
// 4. Read current connection state synchronouslyconst info = client.connectionInfo
// 5. Upload data (requires authentication AND upload capability — a connected// user can still have canUpload=false if they have no postage stamp and no// subsidised gateway is configured)if (info.identity && info.canUpload) { const result = await client.uploadData(new TextEncoder().encode('Hello, Swarm!')) console.log('Uploaded:', result.reference)}
// 6. Cleanup when doneclient.destroy()Configuration Options
Section titled “Configuration Options”The SwarmIdClient constructor accepts the following options:
| Option | Type | Required | Description |
|---|---|---|---|
iframeOrigin |
string |
Yes | Origin of the Swarm ID iframe (e.g., https://swarm-id.snaha.net) |
metadata |
object |
No | App metadata shown during authentication |
metadata.name |
string |
No | Your app’s name |
metadata.description |
string |
No | Brief description of your app |
onConnectionChange |
function |
No | Callback when the dApp-visible connection state changes (identity, stamp, account, auth) |
Data Operations
Section titled “Data Operations”Note: Data operations require the user to be authenticated first. Use
client.connect()or the iframe button to authenticate.
Upload Data
Section titled “Upload Data”// Simple uploadconst result = await client.uploadData(data)
// Upload with encryptionconst result = await client.uploadData(data, { encrypt: true })
// Upload with progress trackingconst result = await client.uploadData(data, {}, (progress) => { console.log(`${progress.percent}% uploaded`)})Download Data
Section titled “Download Data”// Download by referenceconst data = await client.downloadData(reference)
// Download encrypted data (reference will be 128 chars instead of 64)const data = await client.downloadData(encryptedReference)File Operations
Section titled “File Operations”// Upload a fileconst result = await client.uploadFile(file, 'myfile.txt')
// Download a fileconst fileData = await client.downloadFile(reference)console.log(fileData.name, fileData.data)Local Development
Section titled “Local Development”# Clone the repositorygit clone https://github.com/snaha/swarm-id
# Install dependenciespnpm install
# Start both demo and identity UIpnpm devOpen http://localhost:3500 — that’s it!
- Demo app runs on port 3500
- Identity UI runs on port 5500
- No HTTPS, certificates, or custom domains required (
localhostis a secure context)
For advanced setup (local Bee cluster, SSH tunnels for real-domain testing, known dev keys), see the Local Development guide.
Import & Export
Section titled “Import & Export”The library also supports account backup and recovery via encrypted .swarmid files and Swarm-based restoration. See the Architecture Overview for details on the file format and flows, and the API Reference for function documentation.
Next Steps
Section titled “Next Steps”- Architecture Overview - Understand how Swarm ID works
- API Reference - Detailed API documentation