-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Description
🌈 Feature
require('dotenv').config();
const axios = require('axios');
const apiKey = process.env.API_KEY;
if (!apiKey) {
console.error("Error: Missing API_KEY in .env");
process.exit(1);
}
async function createChatSession(externalUserId) {
const url = 'https://api.on-demand.io/chat/v1/sessions';
const headers = { apikey: apiKey };
const body = { agentIds: [], externalUserId };
try {
const { data } = await axios.post(url, body, { headers });
return data?.data?.id;
} catch (error) {
const message = error.response?.data || error.message;
console.error('Session creation failed:', message);
throw new Error(Failed to create session: ${message}
);
}
}
async function submitQuery(sessionId, query) {
const url = https://api.on-demand.io/chat/v1/sessions/${sessionId}/query
;
const headers = { apikey: apiKey };
const body = {
endpointId: 'predefined-openai-gpt4o',
query,
agentIds: ['agent-1712327325', 'agent-1713962163'],
responseMode: 'sync',
reasoningMode: 'medium'
};
try {
const { data } = await axios.post(url, body, { headers });
return data;
} catch (error) {
const message = error.response?.data || error.message;
console.error('Query failed:', message);
throw new Error(Failed to submit query: ${message}
);
}
}
(async () => {
try {
const externalUserId = 'sample-user-123';
const sessionId = await createChatSession(externalUserId);
const result = await submitQuery(sessionId, 'Put your query here');
console.log('Response:\n', JSON.stringify(result, null, 2));
} catch (err) {
console.error('Execution error:', err.message);
}
})();``