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',
});
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);
}
};
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!"
}'
5. cURL / Node.js / PHP Quick Reference
cURL
Chat (OpenAI-compatible)
curl -sS -H "Authorization: Bearer $SYNAPLAN_API_KEY" -H "Content-Type: application/json" \
-X POST https://web.synaplan.com/api.php/v1/chat/completions \
-d '{"model":"gpt-4.1","messages":[{"role":"user","content":"Hello"}]}'
Chat streaming
curl -sS -H "Authorization: Bearer $SYNAPLAN_API_KEY" -H "Content-Type: application/json" \
-X POST https://web.synaplan.com/api.php/v1/chat/completions \
-d '{"model":"gpt-4.1","stream":true,"messages":[{"role":"user","content":"Stream please"}]}'
Image Generation
curl -sS -H "Authorization: Bearer $SYNAPLAN_API_KEY" -H "Content-Type: application/json" \
-X POST https://web.synaplan.com/api.php/v1/images/generations \
-d '{"prompt":"cat wearing sunglasses, vector style"}'
REST chat + SSE
last=$(curl -sS -H "Authorization: Bearer $SYNAPLAN_API_KEY" \
-F action=messageNew -F message="Who are you?" https://web.synaplan.com/api.php | jq -r '.lastIds[0]')
curl -N -sS -H "Authorization: Bearer $SYNAPLAN_API_KEY" \
-F action=chatStream -F lastIds="$last" https://web.synaplan.com/api.php
Models list
curl -sS -H "Authorization: Bearer $SYNAPLAN_API_KEY" https://web.synaplan.com/api.php/v1/models
Audio transcription
curl -sS -H "Authorization: Bearer $SYNAPLAN_API_KEY" -X POST \
-F file=@/path/to/audio.m4a \
https://web.synaplan.com/api.php/v1/audio/transcriptions
Node.js (axios)
const axios = require('axios');
async function chat() {
const res = await axios.post('https://web.synaplan.com/api.php/v1/chat/completions', {
model: 'gpt-4.1',
messages: [{ role: 'user', content: 'Hello' }]
}, {
headers: { Authorization: `Bearer ${process.env.SYNAPLAN_API_KEY}` }
});
console.log(res.data);
}
chat();
PHP
<?php
$ch = curl_init('https://web.synaplan.com/api.php/v1/chat/completions');
$payload = json_encode([
'model' => 'gpt-4.1',
'messages' => [ ['role'=>'user','content'=>'Hello'] ]
]);
curl_setopt_array($ch, [
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('SYNAPLAN_API_KEY'),
'Content-Type: application/json'
],
CURLOPT_POSTFIELDS => $payload,
CURLOPT_RETURNTRANSFER => true
]);
$res = curl_exec($ch);
curl_close($ch);
echo $res;