0

Voices create

by
Published Apr 8, 2025

Create a new cloned voice from an audio sample. The created voice will be private to your account.

Script ultravox Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Ultravox = {
3
  apiKey: string;
4
};
5
type Base64 = string;
6
/**
7
 * Voices create
8
 * Create a new cloned voice from an audio sample. The created voice will be private to your account.
9
 */
10
export async function main(
11
  auth: Ultravox,
12
  body: {
13
    file: {
14
      base64: Base64;
15
      type:
16
        | "image/png"
17
        | "image/jpeg"
18
        | "image/gif"
19
        | "application/pdf"
20
        | "appication/json"
21
        | "text/csv"
22
        | "text/plain"
23
        | "audio/mpeg"
24
        | "audio/wav"
25
        | "video/mp4";
26
      name: string;
27
    };
28
    name: string;
29
    description?: string;
30
  },
31
) {
32
  const url = new URL(`https://api.ultravox.ai/api/voices`);
33

34
  const formData = new FormData();
35
  for (const [k, v] of Object.entries(body)) {
36
    if (v !== undefined && v !== "") {
37
      if (["file"].includes(k)) {
38
        const { base64, type, name } = v as {
39
          base64: Base64;
40
          type: string;
41
          name: string;
42
        };
43
        formData.append(
44
          k,
45
          new Blob([Uint8Array.from(atob(base64), (m) => m.codePointAt(0)!)], {
46
            type,
47
          }),
48
          name,
49
        );
50
      } else {
51
        formData.append(k, String(v));
52
      }
53
    }
54
  }
55
  const response = await fetch(url, {
56
    method: "POST",
57
    headers: {
58
      "X-API-Key": auth.apiKey,
59
    },
60
    body: formData,
61
  });
62
  if (!response.ok) {
63
    const text = await response.text();
64
    throw new Error(`${response.status} ${text}`);
65
  }
66
  return await response.json();
67
}
68