中文文档 | English
Not yet released.
A modern, type-safe TypeScript SDK for the ZAI API, inspired by the OpenAI SDK design patterns.
- 🚀 Full TypeScript Support: Complete type definitions for all API endpoints
- 🔧 OpenAI-Style API: Familiar interface for developers coming from OpenAI
- ⚡ Modern Architecture: Built with modern JavaScript/TypeScript best practices
- 🛡️ Type Safety: Comprehensive type checking and IntelliSense support
- 📦 Tree Shakeable: Import only what you need
- 🌊 Streaming Support: Full support for streaming responses
- 🔐 Secure: Built-in JWT authentication with token caching
npm install zai-sdk
# or
yarn add zai-sdk
# or
pnpm add zai-sdk
import { ZAI } from 'zai-sdk';
// Method 1: Initialize with constructor
const client = new ZAI({
apiKey: 'your-api-key.secret', // or set ZAI_API_KEY environment variable
});
// Method 2: Use static factory method for ZHIPU AI environment
const zhipuClient = ZAI.ofZHIPU('your-api-key.secret');
// Method 3: Use static factory method for ZAI environment
const zaiClient = ZAI.ofZAI('your-api-key.secret');
// Or with additional options
const client = new ZAI({
apiKey: 'your-api-key.secret',
baseURL: 'https://api.z.ai/api/paas/v4/', // optional
timeout: 30000, // optional
maxRetries: 3, // optional
});
// Static methods also support additional options
const zhipuClientWithOptions = ZAI.ofZHIPU('your-api-key.secret', {
timeout: 30000,
maxRetries: 3,
});
// Basic chat completion
const completion = await client.chat.create({
model: 'glm-4',
messages: [
{ role: 'user', content: 'Hello, how are you?' }
],
});
console.log(completion.choices[0].message.content);
// Streaming chat completion
const stream = await client.chat.createStream({
model: 'glm-4',
messages: [
{ role: 'user', content: 'Tell me a story' }
],
});
stream.on('data', (chunk) => {
process.stdout.write(chunk.toString());
});
const completion = await client.chat.create(
{
model: 'glm-4',
messages: [{ role: 'user', content: 'Hello' }],
},
{
timeout: 10000,
headers: {
'Custom-Header': 'value',
},
}
);
This SDK is designed to be compatible with:
- Node.js 14+
- TypeScript 4.5+
- Modern browsers (with bundlers)
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- Visit Z.ai Platform
- Visit ZHIPU AI Open Platform
- GitHub Repository