Client Configuration
The Sona class is your main entry point for interacting with the Sona API.
Constructor Options
import { Sona } from '@sonabuild/kit';
const sona = new Sona({
wallet: userPublicKey // Required: Solana wallet address as string
});
wallet
Type: string Required: Yes
The user's Solana wallet public key as a string.
// From @solana/web3.js
wallet: wallet.publicKey.toString()
// From wallet adapter
wallet: publicKey.toString()
// Direct string
wallet: '7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU'
The wallet parameter is used to:
- Identify the user for transaction building
- Determine token account addresses
- Sign transactions (handled by your wallet integration)
Dynamic Protocol Access
Sona uses JavaScript Proxies to provide dynamic protocol access:
// Access any protocol method
const intent = await sona.protocol.method(params);
// Examples:
await sona.solend.deposit({ amount: 100_000_000 });
await sona.jupiter.swap({ inputMint, outputMint, amount });
await sona.wallet.transfer({ recipient, amount });
The client automatically routes these calls to the appropriate API endpoints without requiring explicit method definitions.
Error Handling
The client throws errors for invalid configuration and failed requests:
try {
const intent = await sona.solend.deposit({
amount: 100_000_000 // 100 USDC
});
if (!await intent.verify()) {
throw new Error('Attestation verification failed');
}
await intent.confirm(signAndSendFunction);
} catch (error) {
if (error.message.includes('Unknown route')) {
console.error('Protocol method not supported');
} else if (error.message.includes('Attestation')) {
console.error('Security verification failed');
} else {
console.error('Request failed:', error.message);
}
}
TypeScript Support
The Kit is written in JavaScript with JSDoc annotations for TypeScript compatibility:
import { Sona } from '@sonabuild/kit';
import type { Intent } from '@sonabuild/kit';
const sona = new Sona({
wallet: publicKey.toString()
});
const intent = await sona.solend.deposit({
amount: 100_000_000 // 100 USDC (6 decimals)
});
// Verify and confirm
const isValid = await intent.verify();
await intent.confirm(signAndSendFunction);
While explicit .d.ts TypeScript definition files are not included, TypeScript can infer types from the JSDoc annotations in the source code. The Kit exports core types like Intent and provides basic type safety for method signatures and return values.