JavaScript Integration Example
JavaScript
Beginner
Integrate ZeebeeAI services into web applications using our JavaScript SDK.
JavaScript SDK Example
This example demonstrates how to use the ZeebeeAI JavaScript SDK to implement chat functionality in web applications.
Installation
Include the ZeebeeChat SDK in your HTML file:
<script src="https://cdn.zeebee.ai/sdk/js/zeebee-chat-sdk.min.js"></script>
Or install via npm:
npm install zeebee-chat-sdk
Basic Usage
// Initialize the SDK
const chat = new ZeebeeChat({
apiUrl: 'https://api.zeebee.ai/v1',
apiKey: 'your-api-key',
defaultModel: 'gpt-4o'
});
// Send a message
async function sendMessage() {
try {
const response = await chat.sendMessage('Hello, how can you help me?', {
model: 'gpt-4o'
});
console.log('AI response:', response.message);
} catch (error) {
console.error('Error:', error);
}
}
Streaming Responses
// Stream a response
async function streamResponse() {
try {
await chat.sendMessage('Tell me a story about AI', {
model: 'gpt-4o',
stream: true,
onChunk: function(chunk) {
// Display each chunk as it arrives
console.log(chunk);
document.getElementById('response').textContent += chunk;
}
});
} catch (error) {
console.error('Error:', error);
}
}
Advanced Features
Event Handlers
// Set up event handlers
chat.onTypingStart = function() {
console.log('AI is typing...');
showTypingIndicator();
};
chat.onTypingEnd = function() {
console.log('AI finished typing');
hideTypingIndicator();
};
chat.onError = function(error) {
console.error('SDK Error:', error);
}
Managing Conversations
// Create a new conversation
await chat.createConversation('My Chat with AI');
// Get conversation history
const history = await chat.getConversation();
// List all conversations
const conversations = await chat.getConversations();
// Delete a conversation
await chat.deleteConversation(conversationId);
Need more examples?
Check out the full JavaScript SDK documentation for more advanced features and examples.