Use this file to discover all available pages before exploring further.
Before diving into advanced SDK usage, it’s important to understand a few key concepts and patterns central to Sage Protocol and its SDK. This section covers essential architectural and usage details to ensure a smooth integration process.
Sage Protocol isolates content and interactions per application by using a unique identifier (appId). Each application maintains distinct channels and posts, while user identities and social graphs remain global across all Sage-compatible apps.What this means for you:
Users have a single, global identity and consistent social relationships everywhere.
Each application provides tailored content and distinct community interactions..
Example React Integration using appId:
import { SageProvider } from '@sageprotocol/sdk/react';const App = () => ( <SageProvider appId="0x123..." // Your application's unique identifier channelRegistryId="0x456..." // Registry managing channels specific to your app network="testnet" // 'mainnet' or 'testnet' > {/* Your app components */} </SageProvider>);export default App;
Example Direct Javascript Integration using appId
import { SageClient } from '@sageprotocol/sdk';const sageClient = new SageClient({ appId: '0x123...', // Your application's unique identifier channelRegistryId: '0x456...', // Registry managing channels specific to your app network: 'testnet' // 'mainnet' or 'testnet'});await sageClient.initialize();
This structure demonstrates how applications utilize distinct contexts (appId) to maintain content isolation while benefiting from universal user identities.
Currently, Sage Protocol requires invite codes and keys for user creation. This temporary mechanism allows applications to control growth and maintain initial engagement quality.
Invite Code: Unique code authorizing user creation.
Invite Key: Secure key accompanying the invite code.
(Note: Future protocol versions will make invite codes optional.)Example React Hook for Creating Users with Invites:
Replace someAction with the specific method name you’re invoking from sageClient. This accurately represents the direct JavaScript integration pattern documented in your SDK.
This approach ensures secure, transparent blockchain interactions.
The Sage SDK provides explicit event-monitoring capabilities. Events (e.g., user creation, posts, channel updates) are emitted on-chain, enabling your application to:
React to user actions in real-time.
Gather precise analytics and track activity.
Example: Querying new user creation events using React hooks:
import { useUser } from '@sageprotocol/sdk/react';function UserEventsComponent() { const { queryCreateEvents } = useUser(); const fetchEvents = async () => { const result = await queryCreateEvents({}); result.events.forEach(event => { console.log(`New user created: ${event.userName} (User ID: ${event.userId})`); console.log(`Owned User ID: ${event.userOwnedId}, Shared User ID: ${event.userSharedId}`); if (event.referredBy) { console.log(`Referred by user ID: ${event.referredBy}`); } }); if (result.hasNextPage) { const nextBatch = await queryCreateEvents({ cursor: result.nextCursor }); // Process next batch similarly } }; return <button onClick={fetchEvents}>Fetch New Users</button>;}export default UserEventsComponent;
Example: Querying new user creation events using direct JavaScript integration
// Monitor new user creation eventsconst result = await sageClient.queryUserCreateEvents({});result.events.forEach(event => { console.log(`New user created: ${event.userName} (User ID: ${event.userId})`); console.log(`Owned User ID: ${event.userOwnedId}, Shared User ID: ${event.userSharedId}`); if (event.referredBy) { console.log(`Referred by user ID: ${event.referredBy}`); }});// Continue querying from the last event fetchedif (result.hasNextPage) { const nextBatch = await sageClient.queryUserCreateEvents({ cursor: result.nextCursor }); // Process next batch similarly}
For React apps, the Sage SDK provides built-in context (SageProvider) and custom hooks (useUser, useChannel, usePost) to simplify state management and interactions with blockchain primitives.Example: Following a channel using React hooks:
With these key concepts clarified, you’re now well-equipped to explore Sage SDK’s core primitives and fully utilize its powerful decentralized social functionalities. Proceed to: