0

Calls create

by
Published Apr 8, 2025
Script ultravox Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Ultravox = {
3
  apiKey: string;
4
};
5
/**
6
 * Calls create
7
 *
8
 */
9
export async function main(
10
  auth: Ultravox,
11
  priorCallId: string | undefined,
12
  body: {
13
    systemPrompt?: string;
14
    temperature?: number;
15
    model?: string;
16
    voice?: string;
17
    languageHint?: string;
18
    initialMessages?: {
19
      role?:
20
        | "MESSAGE_ROLE_UNSPECIFIED"
21
        | "MESSAGE_ROLE_USER"
22
        | "MESSAGE_ROLE_AGENT"
23
        | "MESSAGE_ROLE_TOOL_CALL"
24
        | "MESSAGE_ROLE_TOOL_RESULT";
25
      text?: string;
26
      invocationId?: string;
27
      toolName?: string;
28
      errorDetails?: string;
29
      medium?:
30
        | "MESSAGE_MEDIUM_UNSPECIFIED"
31
        | "MESSAGE_MEDIUM_VOICE"
32
        | "MESSAGE_MEDIUM_TEXT";
33
      callStageMessageIndex?: number;
34
      callStageId?: string;
35
    }[];
36
    joinTimeout?: string;
37
    maxDuration?: string;
38
    timeExceededMessage?: string;
39
    inactivityMessages?: {
40
      duration?: string;
41
      message?: string;
42
      endBehavior?:
43
        | "END_BEHAVIOR_UNSPECIFIED"
44
        | "END_BEHAVIOR_HANG_UP_SOFT"
45
        | "END_BEHAVIOR_HANG_UP_STRICT";
46
    }[];
47
    selectedTools?: {
48
      toolId?: string;
49
      toolName?: string;
50
      temporaryTool?: {
51
        modelToolName?: string;
52
        description?: string;
53
        dynamicParameters?: {
54
          name?: string;
55
          location?:
56
            | "PARAMETER_LOCATION_UNSPECIFIED"
57
            | "PARAMETER_LOCATION_QUERY"
58
            | "PARAMETER_LOCATION_PATH"
59
            | "PARAMETER_LOCATION_HEADER"
60
            | "PARAMETER_LOCATION_BODY";
61
          schema?: {};
62
          required?: false | true;
63
        }[];
64
        staticParameters?: {
65
          name?: string;
66
          location?:
67
            | "PARAMETER_LOCATION_UNSPECIFIED"
68
            | "PARAMETER_LOCATION_QUERY"
69
            | "PARAMETER_LOCATION_PATH"
70
            | "PARAMETER_LOCATION_HEADER"
71
            | "PARAMETER_LOCATION_BODY";
72
          value?: {};
73
        }[];
74
        automaticParameters?: {
75
          name?: string;
76
          location?:
77
            | "PARAMETER_LOCATION_UNSPECIFIED"
78
            | "PARAMETER_LOCATION_QUERY"
79
            | "PARAMETER_LOCATION_PATH"
80
            | "PARAMETER_LOCATION_HEADER"
81
            | "PARAMETER_LOCATION_BODY";
82
          knownValue?:
83
            | "KNOWN_PARAM_UNSPECIFIED"
84
            | "KNOWN_PARAM_CALL_ID"
85
            | "KNOWN_PARAM_CONVERSATION_HISTORY"
86
            | "KNOWN_PARAM_OUTPUT_SAMPLE_RATE";
87
        }[];
88
        requirements?: {
89
          httpSecurityOptions?: { options?: { requirements?: {} }[] };
90
          requiredParameterOverrides?: string[];
91
        };
92
        timeout?: string;
93
        precomputable?: false | true;
94
        http?: { baseUrlPattern?: string; httpMethod?: string };
95
        client?: {};
96
      };
97
      nameOverride?: string;
98
      authTokens?: {};
99
      parameterOverrides?: {};
100
    }[];
101
    medium?: {
102
      webRtc?: {};
103
      twilio?: {};
104
      serverWebSocket?: {
105
        inputSampleRate?: number;
106
        outputSampleRate?: number;
107
        clientBufferSizeMs?: number;
108
      };
109
      telnyx?: {};
110
      plivo?: {};
111
    };
112
    recordingEnabled?: false | true;
113
    firstSpeaker?:
114
      | "FIRST_SPEAKER_UNSPECIFIED"
115
      | "FIRST_SPEAKER_AGENT"
116
      | "FIRST_SPEAKER_USER";
117
    transcriptOptional?: false | true;
118
    initialOutputMedium?:
119
      | "MESSAGE_MEDIUM_UNSPECIFIED"
120
      | "MESSAGE_MEDIUM_VOICE"
121
      | "MESSAGE_MEDIUM_TEXT";
122
    vadSettings?: {
123
      turnEndpointDelay?: string;
124
      minimumTurnDuration?: string;
125
      minimumInterruptionDuration?: string;
126
    };
127
    firstSpeakerSettings?: {
128
      user?: {};
129
      agent?: { uninterruptible?: false | true; text?: string };
130
    };
131
  },
132
) {
133
  const url = new URL(`https://api.ultravox.ai/api/calls`);
134
  for (const [k, v] of [["priorCallId", priorCallId]]) {
135
    if (v !== undefined && v !== "" && k !== undefined) {
136
      url.searchParams.append(k, v);
137
    }
138
  }
139
  const response = await fetch(url, {
140
    method: "POST",
141
    headers: {
142
      "Content-Type": "application/json",
143
      "X-API-Key": auth.apiKey,
144
    },
145
    body: JSON.stringify(body),
146
  });
147
  if (!response.ok) {
148
    const text = await response.text();
149
    throw new Error(`${response.status} ${text}`);
150
  }
151
  return await response.json();
152
}
153