0

Create access token

by
Published Apr 8, 2025

Create a new API token for the logged in user

Script speechify Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Speechify = {
3
  token: string;
4
};
5
/**
6
 * Create access token
7
 * Create a new API token for the logged in user
8
 */
9
export async function main(
10
  auth: Speechify,
11
  body: {
12
    grant_type: "client_credentials";
13
    scope?:
14
      | "audio:speech"
15
      | "audio:stream"
16
      | "audio:all"
17
      | "voices:read"
18
      | "voices:create"
19
      | "voices:delete"
20
      | "voices:all";
21
  },
22
) {
23
  const url = new URL(`https://api.sws.speechify.com/v1/auth/token`);
24

25
  const response = await fetch(url, {
26
    method: "POST",
27
    headers: {
28
      "Content-Type": "application/json",
29
      Authorization: "Bearer " + auth.token,
30
    },
31
    body: JSON.stringify(body),
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.text();
38
}
39