Skip to content

Commit 3186ef2

Browse files
committed
feat: support token-based auth endpoint
1 parent 759b00f commit 3186ef2

6 files changed

Lines changed: 67 additions & 0 deletions

File tree

examples/node-auth/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const { createClient } = require("../../dist/main/index");
2+
const fs = require("fs");
3+
const path = require("path");
4+
5+
const transcribeUrl = async () => {
6+
const deepgram = createClient(process.env.DEEPGRAM_API_KEY);
7+
const { result: token, error: tokenError } = await deepgram.auth.grantToken();
8+
if (tokenError) {
9+
throw tokenError;
10+
}
11+
console.log("Token", token);
12+
13+
console.log("Transcribing URL", "https://dpgr.am/spacewalk.wav");
14+
deepgram.token = token.access_token;
15+
const { result, error } = await deepgram.listen.prerecorded.transcribeUrl(
16+
{
17+
url: "https://dpgr.am/spacewalk.wav",
18+
},
19+
{
20+
model: "nova-3",
21+
keyterm: ["spacewalk"],
22+
}
23+
);
24+
25+
if (error) console.error(error);
26+
if (!error) console.dir(result, { depth: 5 });
27+
};
28+
29+
transcribeUrl();

src/DeepgramClient.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DeepgramVersionError } from "./lib/errors";
22
import {
33
AbstractClient,
44
AgentLiveClient,
5+
AuthRestClient,
56
ListenClient,
67
ManageClient,
78
ReadClient,
@@ -91,6 +92,10 @@ export default class DeepgramClient extends AbstractClient {
9192
return new AgentLiveClient(this.options, endpoint);
9293
}
9394

95+
get auth(): AuthRestClient {
96+
return new AuthRestClient(this.options);
97+
}
98+
9499
/**
95100
* @deprecated
96101
* @see https://dpgr.am/js-v3
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface GrantTokenResponse {
2+
access_token: string;
3+
expires_in: number;
4+
}

src/lib/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export * from "./GetProjectUsageRequestsSchema";
2626
export * from "./GetProjectUsageSummaryResponse";
2727
export * from "./GetProjectUsageSummarySchema";
2828
export * from "./GetTokenDetailsResponse";
29+
export * from "./GrantTokenResponse";
2930
export * from "./ListOnPremCredentialsResponse";
3031
export * from "./LiveConfigOptions";
3132
export * from "./LiveMetadataEvent";

src/packages/AuthRestClient.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { isDeepgramError } from "../lib/errors";
2+
import type { DeepgramResponse } from "../lib/types/DeepgramResponse";
3+
import type { GrantTokenResponse } from "../lib/types/GrantTokenResponse";
4+
import { AbstractRestClient } from "./AbstractRestClient";
5+
6+
export class AuthRestClient extends AbstractRestClient {
7+
public namespace: string = "auth";
8+
9+
public async grantToken(
10+
endpoint = ":version/auth/grant"
11+
): Promise<DeepgramResponse<GrantTokenResponse>> {
12+
try {
13+
const requestUrl = this.getRequestUrl(endpoint);
14+
const result: GrantTokenResponse = await this.post(requestUrl, "").then((result) =>
15+
result.json()
16+
);
17+
18+
return { result, error: null };
19+
} catch (error) {
20+
if (isDeepgramError(error)) {
21+
return { result: null, error };
22+
}
23+
24+
throw error;
25+
}
26+
}
27+
}

src/packages/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export * from "./AbstractClient";
22
export * from "./AbstractLiveClient";
33
export * from "./AbstractRestClient";
44
export * from "./AgentLiveClient";
5+
export * from "./AuthRestClient";
56
export * from "./ListenClient";
67
export * from "./ListenLiveClient";
78
export * from "./ListenRestClient";

0 commit comments

Comments
 (0)