Quick Start
Get Swarm ID integrated into your dApp in just a few steps.
Installation
# Using pnpm (recommended)pnpm add @snaha/swarm-id
# Using npmnpm install @snaha/swarm-id
# Using yarnyarn add @snaha/swarm-idBasic 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', }, onAuthChange: (authenticated) => { console.log('Auth status:', authenticated) },})
// 2. Initialize (creates hidden iframe)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()// Open authentication page in new window/browser tab (useful for custom UI)const authUrl = client.connect()console.log('Authentication opened at:', authUrl)
// 4. Check if user is authenticatedconst status = await client.checkAuthStatus()
// 5. Upload data (after authentication)if (status.authenticated) { const result = await client.uploadData(new TextEncoder().encode('Hello, Swarm!')) console.log('Uploaded:', result.reference)}
// 6. Cleanup when doneclient.destroy()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 |
onAuthChange | function | No | Callback when authentication status changes |
Data Operations
Note: Data operations require the user to be authenticated first. Use
client.connect()or the iframe button to authenticate.
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
// 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
// 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
# Clone the repositorygit clone https://github.com/snaha/swarm-id
# Install dependenciespnpm install
# Start both demo and identity UIpnpm devOpen http://localhost:3000 — that’s it!
- Demo app runs on port 3000
- Identity UI runs on port 5174
- 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
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
- Architecture Overview - Understand how Swarm ID works
- API Reference - Detailed API documentation