API Integration
Learn how to integrate Zonia's capabilities into your applications
Getting Started with the API
Basic Setup
// Example API Integration
const sendMessage = async (message) => {
const response = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ message })
});
const data = await response.json();
return data;
};
// Usage
const response = await sendMessage("Your message here");
console.log(response);Core API Methods
Text Analysis
// Example text analysis request
const analyzeText = async (text) => {
const response = await fetch('/api/analyze', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ text })
});
const data = await response.json();
return data;
};
// Usage
const analysis = await analyzeText("Your content here");
console.log(analysis);Content Generation
// Example content generation request
const generateContent = async (params) => {
const response = await fetch('/api/generate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(params)
});
const data = await response.json();
return data;
};
// Usage
const content = await generateContent({
type: "article",
topic: "AI Technology",
tone: "professional",
length: "medium"
});Chat Conversations
// Example chat conversation
const chat = async (message, context = {}) => {
const response = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
message,
context
})
});
const data = await response.json();
return data;
};
// Usage
const response = await chat("How can I improve my code?", {
language: "JavaScript",
codeSnippet: "your code here"
});Error Handling
try {
const response = await fetch('/api/analyze', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
text: "Your content"
})
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.message);
}
const data = await response.json();
return data;
} catch (error) {
console.error('API Error:', error.message);
// Handle different error cases
if (error.message.includes('rate limit')) {
// Handle rate limiting
} else if (error.message.includes('unauthorized')) {
// Handle authentication error
}
// Handle other errors
}Best Practices
Rate Limiting
Implement proper rate limiting and caching strategies to optimize API usage
Error Handling
Always implement comprehensive error handling for better reliability
Security
Never expose your API key in client-side code
Response Handling
Always validate and sanitize API responses before using them