SDK Usage
SDK Usage
Learn how to use the ZIRI SDK in your applications.
Initialization
Create a new SDK instance:
import { UserSDK } from "@ziri/sdk";
const sdk = new UserSDK({
apiKey: "ziri-user-123-a1b2c3d4e5f67890",
proxyUrl: "http://localhost:3100", // Optional if ZIRI_PROXY_URL is set
});Configuration Options
apiKey(required) - Your ZIRI API key (format:ziri-{userId}-{hash})proxyUrl(optional) - ZIRI server URL (defaults toZIRI_PROXY_URLenv var orhttp://localhost:3100)timeoutMs(optional) - Request timeout in milliseconds (default:30000)fetch(optional) - Custom fetch implementation for environments without globalfetch
Making Requests
Chat Completions
const response = await sdk.chatCompletions({
provider: "openai",
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Hello!" }],
max_tokens: 100,
});
console.log(response.choices[0].message.content);Embeddings
const response = await sdk.embeddings({
provider: "openai",
model: "text-embedding-3-small",
input: "Hello, ZIRI!",
});
console.log(response.data[0].embedding);Images
const response = await sdk.images({
provider: "openai",
model: "dall-e-3",
prompt: "A white cat sitting on a windowsill",
size: "1024x1024",
quality: "standard",
});
console.log(response.data[0].url);Error Handling
The SDK throws errors for failed requests:
try {
await sdk.chatCompletions({
provider: "openai",
model: "gpt-4",
messages: [{ role: "user", content: "Hello" }],
});
} catch (error) {
if (error.message.includes("Authorization denied")) {
console.error("Request was denied by policy");
} else if (error.message.includes("Rate limit exceeded")) {
console.error("Rate limit hit, wait before retrying");
} else {
console.error("Request failed:", error.message);
}
}Getting User ID
Get the user ID from the API key:
const userId = sdk.getUserId();
console.log(userId); // "user-123"
TypeScript Usage
import { UserSDK, UserSDKConfig } from "@ziri/sdk";
const config: UserSDKConfig = {
apiKey: "ziri-user-123-a1b2c3d4e5f67890",
proxyUrl: "http://localhost:3100",
};
const sdk = new UserSDK(config);
const response = await sdk.chatCompletions({
provider: "openai",
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Hello!" }],
});Best Practices
- Reuse SDK instance - Create one instance and reuse it
- Handle errors - Always wrap requests in try-catch
- Set proxy URL - Use environment variable for flexibility
- Validate API key - SDK validates format on construction
Next Steps
- API Reference - Complete method documentation
- Integration Examples - Real-world examples