SDK
The Tarobase JavaScript SDK allows you to interact with the Tarobase platform from your JavaScript or TypeScript applications. It provides a simple interface for authentication, data management, and real-time updates.
Installation
Install the SDK via npm:
npm install @tarobase/js-sdk
Or with Yarn:
yarn add @tarobase/js-sdk
Getting Started
Initialization
Initialize the SDK with your Application ID from the Tarobase Console:
import { init } from '@tarobase/js-sdk';
init({ appId: 'YOUR_APP_ID' });
Authentication
Connect a wallet and authenticate:
import { login } from '@tarobase/js-sdk';
// Prompts wallet connection
await login();
Listen for authentication state changes:
import { onAuthStateChanged } from '@tarobase/js-sdk';
onAuthStateChanged((user) => {
if (user) {
console.log('Connected wallet:', user.address);
} else {
console.log('No wallet connected');
}
});
Core Functions
Reading Data
import { get } from '@tarobase/js-sdk';
// Get data at a specific path
const data = await get('users/123/profile');
Writing Data
import { set } from '@tarobase/js-sdk';
// Set data at a specific path
await set('users/123/profile', {
name: 'Alice',
bio: 'Blockchain developer'
});
Real-time Updates
import { subscribe } from '@tarobase/js-sdk';
// Subscribe to data changes
const unsubscribe = subscribe('users/123/profile', {
onData: (data) => {
console.log('Profile updated:', data);
},
onError: (error) => {
console.error("Received error", error.message)
},
prompt: "Only profiles that have names starting with letter s"
);
// Call unsubscribe() to stop listening
On-Chain vs Off-Chain Data
The SDK automatically handles data storage location based on your application's policies:
- On-Chain Data: For paths configured as on-chain in your policy, data is stored on the blockchain
- Off-Chain Data: For paths configured as off-chain, data is stored in Tarobase's secure database
The storage location is transparent to your application code - the same set
, get
, and subscribe
functions work for both.
For more details on configuring storage policies, see the Policies documentation.
Additional Resources
Support
Need help? Join our Discord community or open an issue on GitHub.