Skip to content

Commit a1616da

Browse files
committed
chore(quickstart/phone): simplify code snippets
1 parent 59ae122 commit a1616da

File tree

1 file changed

+80
-223
lines changed

1 file changed

+80
-223
lines changed

fern/quickstart/phone.mdx

Lines changed: 80 additions & 223 deletions
Original file line numberDiff line numberDiff line change
@@ -96,52 +96,20 @@ vapi assistant create
9696
```typescript
9797
import { VapiClient } from '@vapi-ai/server-sdk';
9898

99-
// Initialize the Vapi client
100-
const vapi = new VapiClient({
101-
token: 'your-api-key', // Replace with your actual API key
99+
const vapi = new VapiClient({ token: process.env.VAPI_API_KEY! });
100+
101+
const assistant = await vapi.assistants.create({
102+
name: 'Customer Support Assistant',
103+
model: {
104+
provider: 'openai',
105+
model: 'gpt-4o',
106+
messages: [{ role: 'system', content: 'You are Alex, a customer service voice assistant for TechSolutions.' }]
107+
},
108+
voice: { provider: '11labs', voice_id: 'cgSgspJ2msm6clMCkdW9' },
109+
firstMessage: 'Hi there, this is Alex from TechSolutions customer support. How can I help you today?'
102110
});
103111

104-
// Define the system prompt for customer support
105-
const systemPrompt = `You are Alex, a customer service voice assistant for TechSolutions. Your primary purpose is to help customers resolve issues with their products, answer questions about services, and ensure a satisfying support experience.
106-
- Sound friendly, patient, and knowledgeable without being condescending
107-
- Use a conversational tone with natural speech patterns
108-
- Speak with confidence but remain humble when you don'\''t know something
109-
- Demonstrate genuine concern for customer issues`;
110-
111-
async function createSupportAssistant() {
112-
try {
113-
const assistant = await vapi.assistants.create({
114-
name: 'Customer Support Assistant',
115-
// Configure the AI model
116-
model: {
117-
provider: 'openai',
118-
model: 'gpt-4o',
119-
messages: [
120-
{
121-
role: 'system',
122-
content: systemPrompt,
123-
},
124-
],
125-
},
126-
// Configure the voice
127-
voice: {
128-
provider: '11labs',
129-
voice_id: 'cgSgspJ2msm6clMCkdW9',
130-
},
131-
// Set the first message
132-
firstMessage: 'Hi there, this is Alex from TechSolutions customer support. How can I help you today?',
133-
});
134-
135-
console.log('Assistant created:', assistant.id);
136-
return assistant;
137-
} catch (error) {
138-
console.error('Error creating assistant:', error);
139-
throw error;
140-
}
141-
}
142-
143-
// Create the assistant
144-
createSupportAssistant();
112+
console.log(assistant.id);
145113
```
146114
</Step>
147115
</Steps>
@@ -157,50 +125,23 @@ vapi assistant create
157125

158126
<Step title="Create the assistant">
159127
```python
128+
import os
160129
from vapi import Vapi
161130

162-
# Initialize the Vapi client
163-
client = Vapi(token="your-api-key") # Replace with your actual API key
131+
client = Vapi(token=os.getenv("VAPI_API_KEY"))
164132

165-
# Define the system prompt for customer support
166-
system_prompt = """You are Alex, a customer service voice assistant for TechSolutions. Your primary purpose is to help customers resolve issues with their products, answer questions about services, and ensure a satisfying support experience.
167-
- Sound friendly, patient, and knowledgeable without being condescending
168-
- Use a conversational tone with natural speech patterns
169-
- Speak with confidence but remain humble when you don't know something
170-
- Demonstrate genuine concern for customer issues"""
171-
172-
def create_support_assistant():
173-
try:
174-
assistant = client.assistants.create(
175-
name="Customer Support Assistant",
176-
# Configure the AI model
177-
model={
178-
"provider": "openai",
179-
"model": "gpt-4o",
180-
"messages": [
181-
{
182-
"role": "system",
183-
"content": system_prompt,
184-
}
185-
],
186-
},
187-
# Configure the voice
188-
voice={
189-
"provider": "11labs",
190-
"voice_id": "cgSgspJ2msm6clMCkdW9",
191-
},
192-
# Set the first message
193-
first_message="Hi there, this is Alex from TechSolutions customer support. How can I help you today?",
194-
)
195-
196-
print(f"Assistant created: {assistant.id}")
197-
return assistant
198-
except Exception as error:
199-
print(f"Error creating assistant: {error}")
200-
raise error
201-
202-
# Create the assistant
203-
create_support_assistant()
133+
assistant = client.assistants.create(
134+
name="Customer Support Assistant",
135+
model={
136+
"provider": "openai",
137+
"model": "gpt-4o",
138+
"messages": [{"role": "system", "content": "You are Alex, a customer service voice assistant for TechSolutions."}],
139+
},
140+
voice={"provider": "11labs", "voice_id": "cgSgspJ2msm6clMCkdW9"},
141+
first_message="Hi there, this is Alex from TechSolutions customer support. How can I help you today?",
142+
)
143+
144+
print(assistant.id)
204145
```
205146
</Step>
206147
</Steps>
@@ -211,24 +152,16 @@ vapi assistant create
211152
<Step title="Create the assistant">
212153
```bash
213154
curl -X POST "https://api.vapi.ai/assistant" \
214-
-H "Authorization: Bearer your-api-key" \
155+
-H "Authorization: Bearer $VAPI_API_KEY" \
215156
-H "Content-Type: application/json" \
216157
-d '{
217158
"name": "Customer Support Assistant",
218159
"model": {
219160
"provider": "openai",
220161
"model": "gpt-4o",
221-
"messages": [
222-
{
223-
"role": "system",
224-
"content": "You are Alex, a customer service voice assistant for TechSolutions. Your primary purpose is to help customers resolve issues with their products, answer questions about services, and ensure a satisfying support experience.\n- Sound friendly, patient, and knowledgeable without being condescending\n- Use a conversational tone with natural speech patterns\n- Speak with confidence but remain humble when you don'\''t know something\n- Demonstrate genuine concern for customer issues"
225-
}
226-
]
227-
},
228-
"voice": {
229-
"provider": "11labs",
230-
"voice_id": "cgSgspJ2msm6clMCkdW9"
162+
"messages": [{ "role": "system", "content": "You are Alex, a customer service voice assistant for TechSolutions." }]
231163
},
164+
"voice": { "provider": "11labs", "voice_id": "cgSgspJ2msm6clMCkdW9" },
232165
"firstMessage": "Hi there, this is Alex from TechSolutions customer support. How can I help you today?"
233166
}'
234167
```
@@ -266,117 +199,69 @@ vapi assistant create
266199

267200
<Tab title="TypeScript (Server SDK)">
268201
<Steps>
269-
<Step title="Purchase a phone number">
202+
<Step title="Create a phone number (API)">
270203
```typescript
271-
async function purchasePhoneNumber() {
272-
try {
273-
// Purchase a phone number
274-
const phoneNumber = await vapi.phoneNumbers.create({
275-
fallbackDestination: {
276-
type: 'number',
277-
number: '+1234567890', // Your fallback number
278-
},
279-
});
280-
281-
console.log('Phone number created:', phoneNumber.number);
282-
return phoneNumber;
283-
} catch (error) {
284-
console.error('Error creating phone number:', error);
285-
throw error;
286-
}
287-
}
288-
```
289-
</Step>
290-
291-
<Step title="Configure inbound calls">
292-
```typescript
293-
async function configureInboundCalls(phoneNumberId: string, assistantId: string) {
294-
try {
295-
// Update phone number with assistant configuration
296-
const updatedNumber = await vapi.phoneNumbers.update(phoneNumberId, {
297-
assistantId: assistantId,
298-
});
299-
300-
console.log('Phone number configured for inbound calls');
301-
return updatedNumber;
302-
} catch (error) {
303-
console.error('Error configuring phone number:', error);
304-
throw error;
305-
}
306-
}
204+
const res = await fetch('https://api.vapi.ai/phone-number', {
205+
method: 'POST',
206+
headers: {
207+
Authorization: `Bearer ${process.env.VAPI_API_KEY}`,
208+
'Content-Type': 'application/json',
209+
},
210+
body: JSON.stringify({
211+
provider: 'vapi',
212+
assistantId: 'your-assistant-id',
213+
numberDesiredAreaCode: '415',
214+
}),
215+
});
216+
const phoneNumber = await res.json();
217+
console.log(phoneNumber.id);
307218
```
308219
</Step>
309220
</Steps>
310221
</Tab>
311222

312223
<Tab title="Python (Server SDK)">
313224
<Steps>
314-
<Step title="Purchase a phone number">
225+
<Step title="Create a phone number (API)">
315226
```python
316-
def purchase_phone_number():
317-
try:
318-
# Purchase a phone number
319-
phone_number = client.phone_numbers.create(
320-
fallback_destination={
321-
"type": "number",
322-
"number": "+1234567890", # Your fallback number
323-
}
324-
)
325-
326-
print(f"Phone number created: {phone_number.number}")
327-
return phone_number
328-
except Exception as error:
329-
print(f"Error creating phone number: {error}")
330-
raise error
331-
```
332-
</Step>
227+
import os, requests
333228

334-
<Step title="Configure inbound calls">
335-
```python
336-
def configure_inbound_calls(phone_number_id: str, assistant_id: str):
337-
try:
338-
# Update phone number with assistant configuration
339-
updated_number = client.phone_numbers.update(
340-
phone_number_id,
341-
assistant_id=assistant_id,
342-
)
343-
344-
print("Phone number configured for inbound calls")
345-
return updated_number
346-
except Exception as error:
347-
print(f"Error configuring phone number: {error}")
348-
raise error
229+
res = requests.post(
230+
"https://api.vapi.ai/phone-number",
231+
headers={
232+
"Authorization": f"Bearer {os.getenv('VAPI_API_KEY')}",
233+
"Content-Type": "application/json",
234+
},
235+
json={
236+
"provider": "vapi",
237+
"assistantId": "your-assistant-id",
238+
"numberDesiredAreaCode": "415",
239+
},
240+
timeout=30,
241+
)
242+
phone_number = res.json()
243+
print(phone_number["id"])
349244
```
350245
</Step>
351246
</Steps>
352247
</Tab>
353248

354249
<Tab title="cURL">
355250
<Steps>
356-
<Step title="Purchase a phone number">
251+
<Step title="Create a phone number (API)">
357252
```bash
358253
curl -X POST "https://api.vapi.ai/phone-number" \
359-
-H "Authorization: Bearer your-api-key" \
254+
-H "Authorization: Bearer $VAPI_API_KEY" \
360255
-H "Content-Type: application/json" \
361256
-d '{
362-
"fallbackDestination": {
363-
"type": "number",
364-
"number": "+1234567890"
365-
}
257+
"provider": "vapi",
258+
"assistantId": "your-assistant-id",
259+
"numberDesiredAreaCode": "415"
366260
}'
367261
```
368262
</Step>
369263

370-
<Step title="Configure inbound calls">
371-
```bash
372-
curl -X PATCH "https://api.vapi.ai/phone-number/{phone-number-id}" \
373-
-H "Authorization: Bearer your-api-key" \
374-
-H "Content-Type: application/json" \
375-
-d '{
376-
"assistantId": "your-assistant-id"
377-
}'
378-
```
379-
</Step>
264+
380265
</Steps>
381266
</Tab>
382267
</Tabs>
@@ -405,58 +290,30 @@ vapi assistant create
405290
<Tabs>
406291
<Tab title="TypeScript (Server SDK)">
407292
```typescript
408-
async function makeOutboundCall(assistantId: string, phoneNumber: string) {
409-
try {
410-
const call = await vapi.calls.create({
411-
assistant: {
412-
assistantId: assistantId,
413-
},
414-
phoneNumberId: 'your-phone-number-id', // Your Vapi phone number ID
415-
customer: {
416-
number: phoneNumber, // Target phone number
417-
},
418-
});
419-
420-
console.log('Outbound call initiated:', call.id);
421-
return call;
422-
} catch (error) {
423-
console.error('Error making outbound call:', error);
424-
throw error;
425-
}
426-
}
427-
428-
// Make a call to your own number for testing
429-
makeOutboundCall('your-assistant-id', '+1234567890');
293+
const call = await vapi.calls.create({
294+
assistant: { assistantId: 'your-assistant-id' },
295+
phoneNumberId: 'your-phone-number-id',
296+
customer: { number: '+1234567890' },
297+
});
298+
console.log(call.id);
430299
```
431300
</Tab>
432301

433302
<Tab title="Python (Server SDK)">
434303
```python
435-
def make_outbound_call(assistant_id: str, phone_number: str):
436-
try:
437-
call = client.calls.create(
438-
assistant_id=assistant_id,
439-
phone_number_id="your-phone-number-id", # Your Vapi phone number ID
440-
customer={
441-
"number": phone_number, # Target phone number
442-
},
443-
)
444-
445-
print(f"Outbound call initiated: {call.id}")
446-
return call
447-
except Exception as error:
448-
print(f"Error making outbound call: {error}")
449-
raise error
450-
451-
# Make a call to your own number for testing
452-
make_outbound_call("your-assistant-id", "+1234567890")
304+
call = client.calls.create(
305+
assistant_id="your-assistant-id",
306+
phone_number_id="your-phone-number-id",
307+
customer={"number": "+1234567890"},
308+
)
309+
print(call.id)
453310
```
454311
</Tab>
455312

456313
<Tab title="cURL">
457314
```bash
458315
curl -X POST "https://api.vapi.ai/call" \
459-
-H "Authorization: Bearer your-api-key" \
316+
-H "Authorization: Bearer $VAPI_API_KEY" \
460317
-H "Content-Type: application/json" \
461318
-d '{
462319
"assistant": {

0 commit comments

Comments
 (0)