API client for the ORBIT MCP server
npm install @schmitech/chatbot-apiA TypeScript/JavaScript client for seamless interaction with the ORBIT server, supporting API key authentication and session tracking.
``bash`
npm install @schmitech/chatbot-api
First, initialize the API client with your server details:
`typescript
import { ApiClient } from '@schmitech/chatbot-api';
const client = new ApiClient({
apiUrl: 'https://your-api-server.com',
apiKey: 'your-api-key',
sessionId: 'optional-session-id' // Optional, for conversation tracking
});
`
`typescript`
async function chat() {
const stream = client.streamChat('Hello, how can I help?');
for await (const response of stream) {
console.log(response.text);
if (response.done) {
console.log('Chat complete!');
}
}
}$3
First, verify you have Node.js and its package manager, npm, installed. Then create a new folder.
`bash`
node -v
npm -v
Initialize a Node.js Project
`bash`
npm init -y
Modify package.json
`json`
{
"name": "orbit-node",
"version": "1.0.0",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"@schmitech/chatbot-api": "^0.5.0"
}
}
Install chatbot api
`bash`
npm install @schmitech/chatbot-api
Run this test
`bash`
node test/test-npm-package.js "how many r are in a strawberry?" "http://localhost:3000" "my-session-123"
Here's how to use the API in a React component:
`tsx
import React, { useState, useMemo } from 'react';
import { ApiClient } from '@schmitech/chatbot-api';
function ChatComponent() {
const [messages, setMessages] = useState
const [input, setInput] = useState('');
// Initialize client (memoize to avoid re-creation on every render)
const client = useMemo(() => new ApiClient({
apiUrl: 'https://your-api-server.com',
apiKey: 'your-api-key',
sessionId: 'user_123_session_456' // Optional
}), []);
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault();
if (!input.trim()) return;
setMessages(prev => [...prev, { text: input, isUser: true }]);
let responseText = '';
const stream = client.streamChat(input);
for await (const response of stream) {
responseText += response.text;
setMessages(prev => {
const newMessages = [...prev];
if (newMessages.length > 0 && !newMessages[newMessages.length - 1].isUser && responseText.startsWith(newMessages[newMessages.length - 1].text)) {
// Update existing bot message if it's the last one
newMessages[newMessages.length - 1] = { text: responseText, isUser: false };
return newMessages;
}
return [...prev, { text: responseText, isUser: false }];
});
if (response.done) break;
}
setInput('');
};
return (
📱 Mobile Usage
$3
`typescript
import { configureApi, streamChat } from '@schmitech/chatbot-api';// Configure once at app startup
configureApi({
apiUrl: 'https://your-api-server.com',
apiKey: 'your-api-key',
sessionId: 'user_123_session_456' // Optional
});
async function handleChat(message: string) {
for await (const response of streamChat(message, true)) {
// Handle streaming response
console.log(response.text);
if (response.done) {
console.log('Chat complete!');
}
}
}
`🌐 CDN Integration
You can also use the API directly in the browser via CDN:
`html
`📚 API Reference
$3
Configure the API client with server details.
| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
|
apiUrl | string | Yes | Chatbot API server URL |
| apiKey | string | Yes | API key for authentication |
| sessionId | string | No | Session ID for conversation tracking |$3
Stream chat responses from the server.
| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
|
message | string | - | The message to send to the chat |
| stream | boolean | true | Whether to stream the response |Returns an AsyncGenerator that yields
StreamResponse objects:`typescript
interface StreamResponse {
text: string; // The text content of the response
done: boolean; // Whether this is the final response
}
``- Always use HTTPS for your API URL
- Keep your API key secure and never expose it in client-side code
- Consider using environment variables for sensitive configuration