Build your first TinyCloud backend service in under 5 minutes
Get TinyCloud running in a Node.js environment and store your first piece of data. By the end of this guide you will have a working script that authenticates with a private key, reads and writes to a space, and creates a delegation for another user.
For development, you can generate a test private key using: node -e "console.log('0x' + require('crypto').randomBytes(32).toString('hex'))"
5
Initialize TinyCloud
6
Create a TinyCloud instance with your configuration.
7
Quick
Copy
import { TinyCloudNode } from '@tinycloud/node-sdk';const tc = new TinyCloudNode({ privateKey: process.env.TINYCLOUD_PRIVATE_KEY, host: 'https://node.tinycloud.xyz', prefix: 'myapp',});
Explained
Copy
import { TinyCloudNode } from '@tinycloud/node-sdk';const tc = new TinyCloudNode({ // Your Ethereum private key (from environment variable) privateKey: process.env.TINYCLOUD_PRIVATE_KEY, // The TinyCloud node to connect to host: 'https://node.tinycloud.xyz', // Optional: prefix for all your KV keys prefix: 'myapp', // Automatically create a space on first sign-in (default: false) autoCreateSpace: true,});
// signIn() authenticates using your private key:// 1. Derives your Ethereum address from the private key// 2. Signs a SIWE message to prove ownership// 3. Establishes an authenticated session with the TinyCloud node// 4. Creates a space for you if one does not existawait tc.signIn();// After sign-in, your space ID and DID are availableconsole.log('Space:', tc.spaceId);// tc.did returns your primary DID (user identity)console.log('DID:', tc.did); // did:pkh:eip155:1:0x...
11
Store Data
12
Save data to your space using the key-value store via tc.kv.
// Store a JSON object under the key 'config'const putResult = await tc.kv.put('config', { version: '1.0.0', lastUpdated: new Date().toISOString(),});if (putResult.ok) { console.log('Data saved!');}// Data is now stored in your space on TinyCloud
14
Retrieve Data
15
Read the data back from storage.
16
Quick
Copy
const result = await tc.kv.get('config');if (result.ok) { console.log('Retrieved:', result.data.data);}
Explained
Copy
// Retrieve the value stored under 'config'const result = await tc.kv.get('config');// Returns the original object: { version: '1.0.0', lastUpdated: '...' }if (result.ok) { console.log('Retrieved:', result.data.data);}
// List all keys matching a prefix// Call without arguments to list everything in the spaceconst listResult = await tc.kv.list();if (listResult.ok) { console.log('All keys:', listResult.data.keys);}// Remove a key and its valueconst delResult = await tc.kv.del('config');if (delResult.ok) { console.log('Deleted config');}
You can create a TinyCloud instance without a private key. This is useful for clients that only need to receive delegations from other users, not own a space.
Quick
Explained
Copy
const bob = new TinyCloudNode();console.log(bob.did); // did:key:z6Mk... (session key DID)console.log(bob.isSessionOnly); // true
Copy
// No config required -- a session key is generated automaticallyconst bob = new TinyCloudNode();// In session-only mode, .did returns a session key DID// (since there is no primary identity to authenticate with)console.log(bob.did); // did:key:z6Mk...console.log(bob.isSessionOnly); // true// Session-only clients cannot create spaces or write// to their own storage, but they CAN receive delegations// from other users and access delegated data
Session-only clients generate an ephemeral did:key identity. They can receive delegations and read/write to other users’ spaces, but they do not have their own space.
Grant another user or service scoped access to your space.
After signIn, tc.did returns your primary DID (the identity you authenticated with). Before signIn, it returns your session key DID. For delegations, always use the recipient’s primary DID (their tc.did after they’ve signed in).
Quick
Explained
Copy
// Alice creates a delegation for Bob (after Bob has signed in)const delegation = await alice.createDelegation({ delegateDID: bob.did, path: 'shared/', actions: ['tinycloud.kv/get', 'tinycloud.kv/put'],});// Bob uses the delegation to access Alice's dataconst access = await bob.useDelegation(delegation);const result = await access.kv.get('shared/data');if (result.ok) { console.log(result.data.data);}
Copy
// Alice grants Bob access to her 'shared/' prefixconst delegation = await alice.createDelegation({ // Bob's primary DID (tc.did after signIn) // This is the identity Bob authenticated with delegateDID: bob.did, // Path prefix the delegation covers path: 'shared/', // Specific actions Bob is allowed to perform actions: ['tinycloud.kv/get', 'tinycloud.kv/put'],});// Bob loads the delegation and gets a scoped clientconst access = await bob.useDelegation(delegation);// Bob can now read and write under Alice's 'shared/' prefixconst getResult = await access.kv.get('shared/data');if (getResult.ok) { console.log(getResult.data.data);}const putResult = await access.kv.put('shared/response', { message: 'Thanks!' });if (putResult.ok) { console.log('Response saved!');}
By default, sessions are stored in memory and lost when your process exits. For long-running services, use file-based session storage.
Memory (Default)
File Storage
Copy
import { TinyCloudNode, MemorySessionStorage } from '@tinycloud/node-sdk';const tc = new TinyCloudNode({ privateKey: process.env.TINYCLOUD_PRIVATE_KEY, host: 'https://node.tinycloud.xyz', // Explicit but not required -- memory storage is the default sessionStorage: new MemorySessionStorage(),});
Copy
import { TinyCloudNode, FileSessionStorage } from '@tinycloud/node-sdk';const tc = new TinyCloudNode({ privateKey: process.env.TINYCLOUD_PRIVATE_KEY, host: 'https://node.tinycloud.xyz', // Persists the session to disk so it survives process restarts sessionStorage: new FileSessionStorage('./.tinycloud-session'),});
File-based storage is recommended for production services to avoid re-authentication on every restart. Add the session file to your .gitignore.