Skip to content

Commit

Permalink
feat: tightly couple the api key to the proxy logic, so an api can't …
Browse files Browse the repository at this point in the history
…mistakenly be made public (#206)
  • Loading branch information
lukeocodes authored Nov 22, 2023
1 parent a75db1f commit 633e0f2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
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

0 comments on commit 633e0f2

Please sign in to comment.