For AI agents: a documentation index is available at the root level at /llms.txt and /llms-full.txt. Append /llms.txt to any URL for a page-level index, or .md for the markdown version of any page.
WebsiteStatusSupportDashboard
DocumentationAPI ReferenceMCPSDKsCLI (new)What's New?
DocumentationAPI ReferenceMCPSDKsCLI (new)What's New?
  • Get started
    • Introduction
    • Phone calls
    • Web calls
    • Vapi Guides
    • Composer
    • CLI quickstart
  • Assistants
    • Quickstart
    • Tools
    • Custom keywords
    • Custom voices
    • Custom transcriber
    • Custom TTS
  • Observability
    • Boards
  • Squads
    • Quickstart
    • Overview
    • Handoff tool
    • Passing data between assistants
  • Best practices
    • Prompting guide
    • Debugging voice agents
    • Enterprise environments (DEV/UAT/PROD)
    • IVR navigation
  • Phone numbers
    • Free Vapi number
    • Inbound SMS
    • Phone Number Hooks
  • Calls
    • Call end reasons
    • Troubleshoot call errors
  • Outbound Campaigns
    • Quickstart
    • Overview
  • Chat
    • Quickstart
    • Streaming
    • Non-streaming
    • OpenAI compatibility
    • Session management
    • Variable substitution
    • SMS chat
    • Web widget
    • Webhooks
  • Workflows
    • Quickstart
    • Overview
LogoLogo
WebsiteStatusSupportDashboard
On this page
  • Overview
  • Prerequisites
  • Scenario
  • 1. Basic Non-Streaming Implementation
  • 2. Context Management with previousChatId
  • 3. Custom Assistant Configuration
  • Next Steps
Chat

Non-streaming chat

Build reliable chat integrations with complete response patterns for batch processing and simple UIs
Was this page helpful?
Edit this page
Previous

OpenAI compatibility

Seamlessly migrate existing OpenAI integrations to Vapi with zero code changes
Next
Built with

Overview

Build a chat integration that receives complete responses after processing, perfect for batch processing, simple UIs, or when you need the full response before proceeding. Ideal for integrations where real-time display isn’t essential.

What You’ll Build:

  • Simple request-response chat patterns with immediate complete responses
  • Context management using previousChatId for linked conversations
  • Basic integration with predictable response timing

For comprehensive context management options including sessions, see Session management.

Prerequisites

  • Completed Chat quickstart tutorial
  • Understanding of basic HTTP requests and JSON handling
  • Familiarity with JavaScript/TypeScript promises or async/await

Scenario

We’ll build a help desk system for “TechFlow” that processes support messages through text chat and maintains conversation history using previousChatId.


1. Basic Non-Streaming Implementation

1

Create a simple chat function

Start with a basic non-streaming chat implementation:

Basic Non-Streaming Request
$curl -X POST https://api.vapi.ai/chat \
> -H "Authorization: Bearer YOUR_API_KEY" \
> -H "Content-Type: application/json" \
> -d '{
> "assistantId": "your-assistant-id",
> "input": "I need help resetting my password"
> }'
2

Understand the response structure

Non-streaming responses come back as complete JSON objects:

Complete Chat Response
1{
2 "id": "chat_123456",
3 "orgId": "org_789012",
4 "assistantId": "assistant_345678",
5 "name": "Password Reset Help",
6 "sessionId": "session_901234",
7 "messages": [
8 {
9 "role": "user",
10 "content": "I need help resetting my password"
11 }
12 ],
13 "output": [
14 {
15 "role": "assistant",
16 "content": "I can help you reset your password. First, let me verify your account information..."
17 }
18 ],
19 "createdAt": "2024-01-15T09:30:00Z",
20 "updatedAt": "2024-01-15T09:30:01Z"
21}
3

Implement in TypeScript

Create a reusable function for non-streaming chat:

non-streaming-chat.ts
1async function sendChatMessage(
2 message: string,
3 previousChatId?: string
4): Promise<{ chatId: string; response: string }> {
5 const response = await fetch('https://api.vapi.ai/chat', {
6 method: 'POST',
7 headers: {
8 'Authorization': 'Bearer YOUR_API_KEY',
9 'Content-Type': 'application/json'
10 },
11 body: JSON.stringify({
12 assistantId: 'your-assistant-id',
13 input: message,
14 ...(previousChatId && { previousChatId })
15 })
16 });
17
18 const chat = await response.json();
19 return {
20 chatId: chat.id,
21 response: chat.output[0].content
22 };
23}

2. Context Management with previousChatId

1

Link chats for conversation context

Use previousChatId to maintain context across multiple chats:

conversation-chain.ts
1async function createConversation() {
2 let lastChatId: string | undefined;
3
4 async function sendMessage(input: string): Promise<string> {
5 const response = await fetch('https://api.vapi.ai/chat', {
6 method: 'POST',
7 headers: {
8 'Authorization': 'Bearer YOUR_API_KEY',
9 'Content-Type': 'application/json'
10 },
11 body: JSON.stringify({
12 assistantId: 'your-assistant-id',
13 input: input,
14 ...(lastChatId && { previousChatId: lastChatId })
15 })
16 });
17
18 const chat = await response.json();
19 lastChatId = chat.id;
20 return chat.output[0].content;
21 }
22
23 return { sendMessage };
24}
25
26// Usage
27const conversation = await createConversation();
28
29const response1 = await conversation.sendMessage("Hello, I'm Alice");
30console.log(response1);
31
32const response2 = await conversation.sendMessage("What's my name?");
33console.log(response2); // Should remember "Alice"

3. Custom Assistant Configuration

1

Use inline assistant configuration

Instead of pre-created assistants, define configuration per request:

Custom Assistant Request
$curl -X POST https://api.vapi.ai/chat \
> -H "Authorization: Bearer YOUR_API_KEY" \
> -H "Content-Type: application/json" \
> -d '{
> "input": "I need help with enterprise features",
> "assistant": {
> "model": {
> "provider": "openai",
> "model": "gpt-4o",
> "temperature": 0.7,
> "messages": [
> {
> "role": "system",
> "content": "You are a helpful technical support agent specializing in enterprise features."
> }
> ]
> }
> }
> }'
2

Create specialized chat handlers

Build different chat handlers for different types of requests:

specialized-handlers.ts
1async function createSpecializedChat(systemPrompt: string) {
2 return async function(userInput: string): Promise<string> {
3 const response = await fetch('https://api.vapi.ai/chat', {
4 method: 'POST',
5 headers: {
6 'Authorization': 'Bearer YOUR_API_KEY',
7 'Content-Type': 'application/json'
8 },
9 body: JSON.stringify({
10 input: userInput,
11 assistant: {
12 model: {
13 provider: 'openai',
14 model: 'gpt-4o',
15 temperature: 0.3,
16 messages: [{ role: 'system', content: systemPrompt }]
17 }
18 }
19 })
20 });
21
22 const chat = await response.json();
23 return chat.output[0].content;
24 };
25}
26
27const technicalSupport = await createSpecializedChat(
28 "You are a technical support specialist. Ask clarifying questions and provide step-by-step troubleshooting."
29);
30
31const billingSupport = await createSpecializedChat(
32 "You are a billing support specialist. Be precise about billing terms and always verify account information."
33);
34
35// Usage
36const techResponse = await technicalSupport("My API requests are returning 500 errors");
37const billingResponse = await billingSupport("I was charged twice this month");

Next Steps

Enhance your non-streaming chat system further:

  • Add streaming capabilities - Upgrade to real-time responses for better UX
  • OpenAI compatibility - Use familiar OpenAI SDK patterns
  • Integrate tools - Enable your assistant to call external APIs and databases
  • Session management - Learn about advanced context management with sessions
  • Add voice capabilities - Extend your text chat to voice interactions

Need help? Chat with the team on our Discord or mention us on X/Twitter.