Code Examples
Explore common use cases for the Synaplan API with these code snippets.
1. Chat Completion (OpenAI SDK)
Since Synaplan is OpenAI-compatible, you can use the official OpenAI library:
Node.js / TypeScript
import OpenAI from 'openai';
const openai = new OpenAI({
apiKey: 'YOUR_SYNAPLAN_API_KEY',
baseURL: 'https://web.synaplan.com/api/v1',
});
async function main() {
const chatCompletion = await openai.chat.completions.create({
messages: [{ role: 'user', content: 'What is Synaplan?' }],
model: 'gpt-4o', // Or any model available in your Synaplan instance
});
console.log(chatCompletion.choices[0].message.content);
}
main();
2. Document Upload for RAG
Upload a file to be processed and indexed for semantic search.
cURL
curl -X POST "https://web.synaplan.com/api/v1/documents" \
-H "Authorization: Bearer YOUR_API_KEY" \
-F "file=@/path/to/your/document.pdf" \
-F "collection=knowledge_base"
3. Streaming Chat Responses (SSE)
Use the native EventSource API in the browser to receive real-time updates.
JavaScript
// 1. Get a short-lived SSE token first (via your backend or API key)
const response = await fetch('https://web.synaplan.com/api/v1/auth/token', {
headers: { 'Authorization': 'Bearer YOUR_API_KEY' }
});
const { token } = await response.json();
// 2. Connect to the stream
const eventSource = new EventSource(`https://web.synaplan.com/api/v1/messages/stream?token=${token}`);
eventSource.onmessage = (event) => {
const data = JSON.parse(event.data);
if (data.content) {
process.stdout.write(data.content); // Append chunk to UI
}
};
eventSource.onerror = (err) => {
console.error("EventSource failed:", err);
eventSource.close();
};
4. Multi-Channel Integration (WhatsApp)
Trigger an AI-powered message to be sent via WhatsApp.
cURL
curl -X POST "https://web.synaplan.com/api/v1/channels/whatsapp/send" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": "491701234567",
"message": "Hello from Synaplan AI!"
}'