|
| 1 | +--- |
| 2 | +title: Non-streaming chat |
| 3 | +subtitle: Simple request-response chat patterns with complete responses |
| 4 | +--- |
| 5 | + |
| 6 | +## Overview |
| 7 | + |
| 8 | +Non-streaming chat returns the complete response after the assistant finishes processing. This is ideal for simple integrations where you don't need real-time output. |
| 9 | + |
| 10 | +Benefits: |
| 11 | +- **Simple integration** - Single request, single response |
| 12 | +- **Complete responses** - Get the full message at once |
| 13 | +- **Lower complexity** - No need to handle streaming events |
| 14 | +- **Better for batch processing** - Process multiple requests sequentially |
| 15 | + |
| 16 | +## Basic example |
| 17 | + |
| 18 | +```bash |
| 19 | +curl -X POST https://api.vapi.ai/chat \ |
| 20 | + -H "Authorization: Bearer YOUR_API_KEY" \ |
| 21 | + -H "Content-Type: application/json" \ |
| 22 | + -d '{ |
| 23 | + "assistantId": "your-assistant-id", |
| 24 | + "input": "What is the weather like today?" |
| 25 | + }' |
| 26 | +``` |
| 27 | + |
| 28 | +### JavaScript implementation |
| 29 | + |
| 30 | +```javascript |
| 31 | +const response = await fetch('https://api.vapi.ai/chat', { |
| 32 | + method: 'POST', |
| 33 | + headers: { |
| 34 | + 'Authorization': 'Bearer YOUR_API_KEY', |
| 35 | + 'Content-Type': 'application/json' |
| 36 | + }, |
| 37 | + body: JSON.stringify({ |
| 38 | + assistantId: 'your-assistant-id', |
| 39 | + input: 'What is the weather like today?' |
| 40 | + }) |
| 41 | +}); |
| 42 | + |
| 43 | +const chat = await response.json(); |
| 44 | +console.log(chat.output[0].content); // Assistant's response |
| 45 | +``` |
| 46 | + |
| 47 | +## Response structure |
| 48 | + |
| 49 | +```javascript |
| 50 | +{ |
| 51 | + "id": "chat_123456", |
| 52 | + "orgId": "org_789012", |
| 53 | + "assistantId": "assistant_345678", |
| 54 | + "name": "Weather Chat", |
| 55 | + "sessionId": "session_901234", |
| 56 | + "messages": [ |
| 57 | + { |
| 58 | + "role": "user", |
| 59 | + "content": "What is the weather like today?" |
| 60 | + } |
| 61 | + ], |
| 62 | + "output": [ |
| 63 | + { |
| 64 | + "role": "assistant", |
| 65 | + "content": "I'd be happy to help with weather information, but I'll need to know your location first. What city are you in?" |
| 66 | + } |
| 67 | + ], |
| 68 | + "createdAt": "2024-01-15T09:30:00Z", |
| 69 | + "updatedAt": "2024-01-15T09:30:01Z" |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## Custom assistant configuration |
| 74 | + |
| 75 | +Pass assistant configuration inline instead of using a pre-created assistant: |
| 76 | + |
| 77 | +```bash |
| 78 | +curl -X POST https://api.vapi.ai/chat \ |
| 79 | + -H "Authorization: Bearer YOUR_API_KEY" \ |
| 80 | + -H "Content-Type: application/json" \ |
| 81 | + -d '{ |
| 82 | + "input": "Help me plan a trip to Paris", |
| 83 | + "assistant": { |
| 84 | + "model": { |
| 85 | + "provider": "openai", |
| 86 | + "model": "gpt-4", |
| 87 | + "temperature": 0.7, |
| 88 | + "messages": [ |
| 89 | + { |
| 90 | + "role": "system", |
| 91 | + "content": "You are a helpful travel assistant specializing in European destinations." |
| 92 | + } |
| 93 | + ] |
| 94 | + }, |
| 95 | + "voice": { |
| 96 | + "provider": "azure", |
| 97 | + "voiceId": "andrew" |
| 98 | + } |
| 99 | + } |
| 100 | + }' |
| 101 | +``` |
| 102 | + |
| 103 | +### JavaScript with custom assistant |
| 104 | + |
| 105 | +```javascript |
| 106 | +const response = await fetch('https://api.vapi.ai/chat', { |
| 107 | + method: 'POST', |
| 108 | + headers: { |
| 109 | + 'Authorization': 'Bearer YOUR_API_KEY', |
| 110 | + 'Content-Type': 'application/json' |
| 111 | + }, |
| 112 | + body: JSON.stringify({ |
| 113 | + input: 'Help me plan a trip to Paris', |
| 114 | + assistant: { |
| 115 | + model: { |
| 116 | + provider: 'openai', |
| 117 | + model: 'gpt-4', |
| 118 | + temperature: 0.7, |
| 119 | + messages: [ |
| 120 | + { |
| 121 | + role: 'system', |
| 122 | + content: 'You are a helpful travel assistant specializing in European destinations.' |
| 123 | + } |
| 124 | + ] |
| 125 | + }, |
| 126 | + voice: { |
| 127 | + provider: 'azure', |
| 128 | + voiceId: 'andrew' |
| 129 | + } |
| 130 | + } |
| 131 | + }) |
| 132 | +}); |
| 133 | + |
| 134 | +const chat = await response.json(); |
| 135 | +console.log(chat.output[0].content); |
| 136 | +``` |
| 137 | + |
| 138 | +## Context management |
| 139 | + |
| 140 | +### Using sessions |
| 141 | + |
| 142 | +Sessions allow multiple chats to share the same context: |
| 143 | + |
| 144 | +```bash |
| 145 | +# Create a session first |
| 146 | +curl -X POST https://api.vapi.ai/session \ |
| 147 | + -H "Authorization: Bearer YOUR_API_KEY" \ |
| 148 | + -H "Content-Type: application/json" \ |
| 149 | + -d '{ |
| 150 | + "assistantId": "your-assistant-id" |
| 151 | + }' |
| 152 | +``` |
| 153 | + |
| 154 | +```bash |
| 155 | +# Use the session for multiple chats |
| 156 | +curl -X POST https://api.vapi.ai/chat \ |
| 157 | + -H "Authorization: Bearer YOUR_API_KEY" \ |
| 158 | + -H "Content-Type: application/json" \ |
| 159 | + -d '{ |
| 160 | + "sessionId": "session_abc123", |
| 161 | + "input": "My name is Alice" |
| 162 | + }' |
| 163 | +``` |
| 164 | + |
| 165 | +```bash |
| 166 | +# Continue the conversation |
| 167 | +curl -X POST https://api.vapi.ai/chat \ |
| 168 | + -H "Authorization: Bearer YOUR_API_KEY" \ |
| 169 | + -H "Content-Type: application/json" \ |
| 170 | + -d '{ |
| 171 | + "sessionId": "session_abc123", |
| 172 | + "input": "What is my name?" |
| 173 | + }' |
| 174 | +``` |
| 175 | + |
| 176 | +### JavaScript session example |
| 177 | + |
| 178 | +```javascript |
| 179 | +// Create a session |
| 180 | +const sessionResponse = await fetch('https://api.vapi.ai/session', { |
| 181 | + method: 'POST', |
| 182 | + headers: { |
| 183 | + 'Authorization': 'Bearer YOUR_API_KEY', |
| 184 | + 'Content-Type': 'application/json' |
| 185 | + }, |
| 186 | + body: JSON.stringify({ |
| 187 | + assistantId: 'your-assistant-id' |
| 188 | + }) |
| 189 | +}); |
| 190 | + |
| 191 | +const session = await sessionResponse.json(); |
| 192 | + |
| 193 | +// Use the session for multiple chats |
| 194 | +const chat1 = await fetch('https://api.vapi.ai/chat', { |
| 195 | + method: 'POST', |
| 196 | + headers: { |
| 197 | + 'Authorization': 'Bearer YOUR_API_KEY', |
| 198 | + 'Content-Type': 'application/json' |
| 199 | + }, |
| 200 | + body: JSON.stringify({ |
| 201 | + sessionId: session.id, |
| 202 | + input: 'My name is Alice' |
| 203 | + }) |
| 204 | +}); |
| 205 | + |
| 206 | +const chat1Data = await chat1.json(); |
| 207 | +console.log(chat1Data.output[0].content); // "Nice to meet you, Alice!" |
| 208 | + |
| 209 | +const chat2 = await fetch('https://api.vapi.ai/chat', { |
| 210 | + method: 'POST', |
| 211 | + headers: { |
| 212 | + 'Authorization': 'Bearer YOUR_API_KEY', |
| 213 | + 'Content-Type': 'application/json' |
| 214 | + }, |
| 215 | + body: JSON.stringify({ |
| 216 | + sessionId: session.id, |
| 217 | + input: 'What is my name?' // Assistant will remember "Alice" |
| 218 | + }) |
| 219 | +}); |
| 220 | + |
| 221 | +const chat2Data = await chat2.json(); |
| 222 | +console.log(chat2Data.output[0].content); // "Your name is Alice." |
| 223 | +``` |
| 224 | + |
| 225 | +### Using previous chat |
| 226 | + |
| 227 | +Link chats together without creating a session: |
| 228 | + |
| 229 | +```bash |
| 230 | +# First chat |
| 231 | +curl -X POST https://api.vapi.ai/chat \ |
| 232 | + -H "Authorization: Bearer YOUR_API_KEY" \ |
| 233 | + -H "Content-Type: application/json" \ |
| 234 | + -d '{ |
| 235 | + "assistantId": "your-assistant-id", |
| 236 | + "input": "I need help with Python programming" |
| 237 | + }' |
| 238 | +``` |
| 239 | + |
| 240 | +```bash |
| 241 | +# Continue conversation with previous chat ID |
| 242 | +curl -X POST https://api.vapi.ai/chat \ |
| 243 | + -H "Authorization: Bearer YOUR_API_KEY" \ |
| 244 | + -H "Content-Type: application/json" \ |
| 245 | + -d '{ |
| 246 | + "assistantId": "your-assistant-id", |
| 247 | + "previousChatId": "chat_abc123", |
| 248 | + "input": "Show me how to create a list" |
| 249 | + }' |
| 250 | +``` |
| 251 | + |
| 252 | +### JavaScript previous chat example |
| 253 | + |
| 254 | +```javascript |
| 255 | +// First chat |
| 256 | +const firstChat = await fetch('https://api.vapi.ai/chat', { |
| 257 | + method: 'POST', |
| 258 | + headers: { |
| 259 | + 'Authorization': 'Bearer YOUR_API_KEY', |
| 260 | + 'Content-Type': 'application/json' |
| 261 | + }, |
| 262 | + body: JSON.stringify({ |
| 263 | + assistantId: 'your-assistant-id', |
| 264 | + input: 'I need help with Python programming' |
| 265 | + }) |
| 266 | +}); |
| 267 | + |
| 268 | +const firstChatData = await firstChat.json(); |
| 269 | + |
| 270 | +// Continue conversation |
| 271 | +const secondChat = await fetch('https://api.vapi.ai/chat', { |
| 272 | + method: 'POST', |
| 273 | + headers: { |
| 274 | + 'Authorization': 'Bearer YOUR_API_KEY', |
| 275 | + 'Content-Type': 'application/json' |
| 276 | + }, |
| 277 | + body: JSON.stringify({ |
| 278 | + assistantId: 'your-assistant-id', |
| 279 | + previousChatId: firstChatData.id, |
| 280 | + input: 'Show me how to create a list' |
| 281 | + }) |
| 282 | +}); |
| 283 | + |
| 284 | +const secondChatData = await secondChat.json(); |
| 285 | +console.log(secondChatData.output[0].content); |
| 286 | +``` |
| 287 | + |
| 288 | +## Message types |
| 289 | + |
| 290 | +### User message |
| 291 | +```javascript |
| 292 | +{ |
| 293 | + "role": "user", |
| 294 | + "content": "Hello, how are you?", |
| 295 | + "name": "Alice" // Optional |
| 296 | +} |
| 297 | +``` |
| 298 | + |
| 299 | +### Assistant message |
| 300 | +```javascript |
| 301 | +{ |
| 302 | + "role": "assistant", |
| 303 | + "content": "I'm doing well, thank you!", |
| 304 | + "tool_calls": [...], // Optional tool calls |
| 305 | + "refusal": null // Optional refusal message |
| 306 | +} |
| 307 | +``` |
| 308 | + |
| 309 | +### System message |
| 310 | +```javascript |
| 311 | +{ |
| 312 | + "role": "system", |
| 313 | + "content": "You are a helpful assistant specializing in technical support." |
| 314 | +} |
| 315 | +``` |
| 316 | + |
| 317 | +### Tool message |
| 318 | +```javascript |
| 319 | +{ |
| 320 | + "role": "tool", |
| 321 | + "content": "Weather data retrieved successfully", |
| 322 | + "tool_call_id": "call_123456" |
| 323 | +} |
| 324 | +``` |
| 325 | + |
| 326 | +### Developer message |
| 327 | +```javascript |
| 328 | +{ |
| 329 | + "role": "developer", |
| 330 | + "content": "Always be concise in your responses." |
| 331 | +} |
| 332 | +``` |
| 333 | + |
| 334 | +## Best practices |
| 335 | + |
| 336 | +<Tip> |
| 337 | +**When to use non-streaming** |
| 338 | +- Simple chatbots or FAQ systems |
| 339 | +- Batch processing of messages |
| 340 | +- When you need the complete response before proceeding |
| 341 | +- Integrations where real-time display isn't important |
| 342 | +</Tip> |
| 343 | + |
| 344 | +<Note> |
| 345 | +**Performance considerations** |
| 346 | +- Responses may take longer for complex queries |
| 347 | +- Use streaming for better user experience with long responses |
| 348 | +- Consider timeout settings for your requests |
| 349 | +</Note> |
| 350 | + |
| 351 | +## Next steps |
| 352 | + |
| 353 | +- Try [streaming chat](/docs/chat/streaming) for real-time responses |
| 354 | +- Explore [OpenAI compatibility](/docs/chat/openai-compatibility) for existing integrations |
| 355 | +- View the [API reference](/api-reference/chats/chat-controller-create-chat) for all parameters |
| 356 | +- Learn about [assistants](/docs/assistants) to customize behavior |
0 commit comments