Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tightly couple the api key to the proxy logic #206

Merged
merged 2 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ import { createClient } from "@deepgram/sdk";

const deepgram = createClient(DEEPGRAM_API_KEY, {
global: { url: "https://api.beta.deepgram.com" },
// proxy: { url: "http://localhost:8080" }
// restProxy: { url: "http://localhost:8080" }
});
```

Expand All @@ -150,12 +150,16 @@ This SDK now works in the browser. If you'd like to make REST-based requests (pr
import { createClient } from "@deepgram/sdk";

const deepgram = createClient("proxy", {
proxy: { url: "http://localhost:8080" },
restProxy: { url: "http://localhost:8080" },
});
```

> Important: You must pass `"proxy"` as your API key, and use the proxy to set the `Authorization` header to your Deepgram API key.

Your proxy service should replace the Authorization header with `Authorization: token <DEEPGRAM_API_KEY>` and return results verbatim to the SDK.

Check out our example Node-based proxy here: [Deepgram Node Proxy](https://github.com/deepgram-devs/deepgram-node-proxy).

# Transcription (Synchronous)

## Remote Files
Expand Down
11 changes: 9 additions & 2 deletions src/packages/AbstractClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { DEFAULT_OPTIONS, DEFAULT_URL } from "../lib/constants";
import { DeepgramError } from "../lib/errors";
import { applySettingDefaults, stripTrailingSlash } from "../lib/helpers";
import { DeepgramClientOptions } from "../lib/types";

Expand All @@ -19,18 +20,24 @@ export abstract class AbstractClient {
}

if (!this.key) {
throw new Error("A deepgram API key is required");
throw new DeepgramError("A deepgram API key is required");
}

this.options = applySettingDefaults(options, DEFAULT_OPTIONS);

if (!this.options.global?.url) {
throw new Error(
throw new DeepgramError(
`An API URL is required. It should be set to ${DEFAULT_URL} by default. No idea what happened!`
);
}

if (this.willProxy()) {
if (this.key !== "proxy") {
throw new DeepgramError(
`Do not attempt to pass any other API key than the string "proxy" when making proxied REST requests. Please ensure your proxy application is responsible for writing our API key to the Authorization header.`
);
}

this.baseUrl = this.resolveBaseUrl(this.options.restProxy?.url as string);

if (this.options.global.headers) {
Expand Down