Creating a Chat Session
This guide explains how to create a new chat session using the ZeroinAI Chat Service API.
Overview
Creating a chat session is the first step in the conversation flow. When a user initiates a conversation with your AI assistant, you'll need to create a chat session to manage the conversation state and track token usage.
Prerequisites
- You have your ZeroinAI API Key
- You have decided on an initial token limit for the conversation
Step-by-Step Guide
1. Prepare Your Request
To create a new chat session, you'll need to make a POST request to the /chats/ endpoint with your API key and a JSON body containing the token limit.
// Example in JavaScript
const createChat = async () => {
const response = await fetch('https://api.zeroinai.com/micro-apps/for-api-providers/chat-service/chats/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': 'YOUR_API_KEY'
},
body: JSON.stringify({
token_limit: 1000.0
})
});
const data = await response.json();
return data;
};
2. Handle the Response
The API will respond with a JSON object containing the newly created chat session details, including the chat_id which you'll need for all subsequent requests.
// Example response handling
createChat()
.then(chatSession => {
console.log('Chat created with ID:', chatSession.chat_id);
// Store the chat_id for future use
localStorage.setItem('currentChatId', chatSession.chat_id);
})
.catch(error => {
console.error('Error creating chat:', error);
});
3. Store the Chat ID
Make sure to store the chat_id securely, as you'll need it for:
- Sending messages
- Retrieving message history
- Adding more tokens if needed
- Deleting the chat when the conversation is over
Token Management
The token_limit parameter determines how many tokens can be used in this chat session. Tokens are consumed when:
- Processing user messages
- Generating AI responses
When setting your initial token limit, consider:
- The expected length of the conversation
- The complexity of the queries
- Your budget constraints
If a chat session runs out of tokens, you can add more using the Update Chat Tokens endpoint.
Next Steps
Now that you've created a chat session, you can:
- Send messages and get AI responses
- Customize the chat widget if you're using our frontend components
Complete Code Example
// Complete example of creating a chat session
const API_URL = 'https://api.zeroinai.com/micro-apps/for-api-providers/chat-service';
const API_KEY = 'YOUR_API_KEY';
async function createChatSession() {
try {
const response = await fetch(`${API_URL}/chats/`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': API_KEY
},
body: JSON.stringify({
token_limit: 1000.0
})
});
if (!response.ok) {
throw new Error(`Error: ${response.status} ${response.statusText}`);
}
const chatSession = await response.json();
console.log('Chat session created:', chatSession);
// Store the chat ID for future use
sessionStorage.setItem('chatId', chatSession.chat_id);
return chatSession;
} catch (error) {
console.error('Failed to create chat session:', error);
throw error;
}
}
// Call the function to create a chat session
createChatSession();