0
Create temporary authentication token for Streaming STT
One script reply has been approved by the moderators Verified

Create a temporary authentication token for Streaming Speech-to-Text

Created by hugo697 32 days ago Viewed 2799 times
0
Submitted by hugo697 Bun
Verified 32 days ago
1
//native
2
type Assemblyai = {
3
  apiKey: string;
4
};
5
/**
6
 * Create temporary authentication token for Streaming STT
7
 * Create a temporary authentication token for Streaming Speech-to-Text
8
 */
9
export async function main(auth: Assemblyai, body: { expires_in: number }) {
10
  const url = new URL(`https://api.assemblyai.com/v2/realtime/token`);
11

12
  const response = await fetch(url, {
13
    method: "POST",
14
    headers: {
15
      "Content-Type": "application/json",
16
      Authorization: "Bearer " + auth.apiKey,
17
    },
18
    body: JSON.stringify(body),
19
  });
20
  if (!response.ok) {
21
    const text = await response.text();
22
    throw new Error(`${response.status} ${text}`);
23
  }
24
  return await response.json();
25
}
26