Quick Start
This guide will help you build your first integration with the CyberPod SDK.
1. Initialize the SDK
Choose one of the following authentication methods:
Path Adapter Required
For current backends, add enablePathAdapter: true to your config. See Path Adapter for details.
Option A: API Key (Recommended for server-side)
import { initSDK } from "@cpod/sdk";
initSDK({
baseUrl: process.env.NEXT_PUBLIC_CPOD_BASE_URL!,
apiKey: process.env.CPOD_API_KEY!,
enablePathAdapter: true, // Required for current backends
});
Option B: Username/Password (For client-side apps)
import { setBaseUrl, authenticateUser } from "@cpod/sdk";
setBaseUrl(process.env.NEXT_PUBLIC_CPOD_BASE_URL!);
const tokens = await authenticateUser({
username: "user@example.com",
password: "your-password",
});
// Tokens are automatically stored for subsequent requests
2. Make API Calls
Get Current User
import { getUser } from "@cpod/sdk";
const user = await getUser();
console.log(`Hello, ${user.name}!`);
Chat Completion
import { chat } from "@cpod/sdk";
// Non-streaming
const response = await chat.complete({
message: "What is TypeScript?",
});
console.log(response.content);
// Streaming
for await (const chunk of chat.stream({ message: "Write a haiku" })) {
process.stdout.write(chunk.content);
}
File Operations
import { uploadFile, searchFiles } from "@cpod/sdk";
// Upload a file
const file = await uploadFile({
file: myFile,
folder_id: "folder-123",
});
// Search files
const results = await searchFiles({
query: "quarterly report",
limit: 10,
});
3. Handle Errors
import { CyberPodError, authenticateUser } from "@cpod/sdk";
try {
await authenticateUser({ username, password });
} catch (error) {
if (error instanceof CyberPodError) {
switch (error.status) {
case 401:
console.error("Invalid credentials");
break;
case 429:
console.error("Rate limited, try again later");
break;
default:
console.error(`API error: ${error.message}`);
}
}
throw error;
}
Complete Example
import {
initSDK,
getUser,
chat,
CyberPodError,
} from "@cpod/sdk";
async function main() {
// Initialize with API key
initSDK({
baseUrl: process.env.NEXT_PUBLIC_CPOD_BASE_URL!,
apiKey: process.env.CPOD_API_KEY!,
enablePathAdapter: true, // Required for current backends
});
try {
// Get user info
const user = await getUser();
console.log(`Logged in as ${user.name}`);
// Chat with AI
const response = await chat.complete({
message: "Hello! What can you help me with?",
});
console.log("AI:", response.content);
} catch (error) {
if (error instanceof CyberPodError) {
console.error(`API Error [${error.status}]: ${error.message}`);
} else {
throw error;
}
}
}
main();
Next Steps
- Authentication Guide - Learn about token management
- Chat Completions - Advanced chat features
- API Reference - Explore all functions