0

Transcribes audio into the input language.

by
Published Oct 17, 2025
Script groqai Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Groq = {
3
  api_key: string;
4
};
5
type Base64 = string;
6
/**
7
 * Transcribes audio into the input language.
8
 *
9
 */
10
export async function main(
11
  auth: Groq,
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
    language?: string | false;
29
    model: string;
30
    prompt?: string;
31
    response_format?: "json" | "text" | "verbose_json";
32
    temperature?: number;
33
    timestamp_granularities?: "word" | "segment"[];
34
  },
35
) {
36
  const url = new URL(`https://api.groq.com/openai/v1/audio/transcriptions`);
37

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