Authentication
Authentication
The Divinci AI SDK supports multiple authentication methods.
API Key Authentication
The simplest method - use an API key directly:
import { DivinciServer } from "@divinci-ai/server";
const divinci = new DivinciServer({ apiKey: process.env.DIVINCI_API_KEY,});import { DivinciClient } from "@divinci-ai/client";
const client = new DivinciClient({ releaseId: "rel_abc123", apiKey: "divinci_key_...",});External User Authentication
For embedded chat widgets, identify users with external user data:
const client = new DivinciClient({ releaseId: "rel_abc123", apiKey: "divinci_key_...", externalUser: { id: "user_123", tier: "premium", metadata: { company: "Acme Inc", }, },});JWT Token Authentication
For production browser applications, use JWT tokens from your backend:
const client = new DivinciClient({ releaseId: "rel_abc123", getToken: async () => { const response = await fetch("/api/divinci-token"); const data = await response.json(); return data.token; },});Your backend generates the token:
// Express backendapp.get("/api/divinci-token", async (req, res) => { const token = await divinci.auth.createToken({ userId: req.user.id, releaseId: "rel_abc123", expiresIn: "1h", }); res.json({ token });});Security Best Practices
- Never commit API keys - Use environment variables
- Rotate keys regularly - Update keys periodically
- Use separate keys - Different keys for dev/staging/production
- Limit key permissions - Use scoped keys when possible
- Monitor usage - Track API key usage for anomalies