Protect your API keys with a secure token provider pattern that keeps credentials safely on your backend server. Seamless authentication for frontend applications without compromising security.
Once your backend is set up, configure the SDK to use your token provider:
Copy
Ask AI
import { AnimusClient } from 'animus-client';const client = new AnimusClient({ tokenProviderUrl: 'https://your-backend.com/api/get-animus-token', chat: { model: 'vivian-llama3.1-70b-1.0-fp8', systemMessage: 'You are a helpful assistant.' }});// The SDK will automatically call your token provider when neededclient.chat.send("Hello!"); // Event-driven - listen for messageComplete event
The SDK doesn’t have built-in user authentication features. Instead, you handle user authentication in your backend token provider endpoint. Here’s the recommended pattern:
Copy
Ask AI
// Your backend handles user authentication// The SDK only needs the token provider URLconst client = new AnimusClient({ tokenProviderUrl: 'https://your-backend.com/api/get-animus-token', chat: { model: 'vivian-llama3.1-70b-1.0-fp8', systemMessage: 'You are a helpful assistant.' }});// Your backend endpoint should:// 1. Validate the user's session/token// 2. Only return Animus tokens for authenticated users// 3. Return 401 for unauthenticated requests
Token refresh happens automatically, but you can handle authentication errors:
Copy
Ask AI
import { AuthenticationError } from 'animus-client';try { // Event-driven approach - no await needed client.chat.send("Hello!"); // Or use completions() for direct API calls const response = await client.chat.completions({ messages: [{ role: 'user', content: 'Hello!' }] });} catch (error) { if (error instanceof AuthenticationError) { // Token might be expired or invalid // Your backend token provider may have rejected the request console.log('Authentication failed, redirecting to login...'); redirectToLogin(); }}
// Clear stored Animus token (forces refresh on next request)client.clearAuthToken();// Next request will call your token provider againclient.chat.send("This will fetch a new token");
Note: The SDK doesn’t provide methods to check if a token exists or inspect token details. Token management is handled automatically, and you only need to clear tokens when necessary (e.g., on user logout).
// Ensure your backend endpoint is correctconst client = new AnimusClient({ tokenProviderUrl: 'https://your-backend.com/api/get-animus-token', // Check this URL});
CORS Errors
Copy
Ask AI
// Backend: Enable CORS for your frontend domainapp.use(cors({ origin: 'https://your-frontend-domain.com'}));
Authentication Loops
Copy
Ask AI
// Check that your backend returns the correct format// Expected response: { "accessToken": "jwt_token_here" }