You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The OpenAI Node library provides convenient access to the OpenAI REST API from applications written in server-side JavaScript.
6
-
It includes TypeScript definitions for all request params and response fields.
5
+
This library provides convenient access to the OpenAI REST API from TypeScript or JavaScript.
7
6
8
-
> ⚠️ **Important note: this library is meant for server-side usage only, as using it in client-side browser code will expose your secret API key. [See here](https://platform.openai.com/docs/api-reference/authentication) for more details.**
9
-
10
-
## Documentation
7
+
It is generated from our [OpenAPI specification](https://github.com/openai/openai-openapi) with [Stainless](https://stainlessapi.com/).
11
8
12
9
To learn how to use the OpenAI API, check out our [API Reference](https://platform.openai.com/docs/api-reference) and [Documentation](https://platform.openai.com/docs).
13
10
@@ -21,6 +18,9 @@ yarn add openai
21
18
22
19
## Usage
23
20
21
+
> [!IMPORTANT]
22
+
> Previous versions of this SDK used a `Configuration` class. See the [v3 to v4 migration guide](https://github.com/openai/openai-node/discussions/217).
23
+
24
24
```js
25
25
importOpenAIfrom'openai';
26
26
@@ -66,10 +66,9 @@ main();
66
66
If you need to cancel a stream, you can `break` from the loop
67
67
or call `stream.controller.abort()`.
68
68
69
-
### Usage with TypeScript
69
+
### Request & Response types
70
70
71
-
Importing, instantiating, and interacting with the library are the same as above.
72
-
If you like, you may reference our types directly:
71
+
This library includes TypeScript definitions for all request params and response fields. You may import and use them like so:
73
72
74
73
```ts
75
74
importOpenAIfrom'openai';
@@ -207,6 +206,30 @@ On timeout, an `APIConnectionTimeoutError` is thrown.
207
206
208
207
Note that requests which time out will be [retried twice by default](#retries).
209
208
209
+
## Advanced Usage
210
+
211
+
### Accessing raw Response data (e.g., headers)
212
+
213
+
The "raw" `Response` returned by `fetch()` can be accessed through the `.asResponse()` method on the `APIPromise` type that all methods return.
214
+
215
+
You can also use the `.withResponse()` method to get the raw `Response` along with the parsed data.
216
+
217
+
```ts
218
+
const openai =newOpenAI();
219
+
220
+
const response =awaitopenai.chat.completions
221
+
.create({ messages: [{ role: 'user', content: 'Say this is a test' }], model: 'gpt-3.5-turbo' })
222
+
.asResponse();
223
+
console.log(response.headers.get('X-My-Header'));
224
+
console.log(response.statusText); // access the underlying Response object
225
+
226
+
const { data: completions, response: raw } =awaitopenai.chat.completions
227
+
.create({ messages: [{ role: 'user', content: 'Say this is a test' }], model: 'gpt-3.5-turbo' })
228
+
.withResponse();
229
+
console.log(raw.headers.get('X-My-Header'));
230
+
console.log(completions.choices);
231
+
```
232
+
210
233
## Configuring an HTTP(S) Agent (e.g., for proxies)
211
234
212
235
By default, this library uses a stable agent for all http/https requests to reuse TCP connections, eliminating many TCP & TLS handshakes and shaving around 100ms off most requests.
@@ -247,7 +270,9 @@ We are keen for your feedback; please open an [issue](https://www.github.com/ope
247
270
The following runtimes are supported:
248
271
249
272
- Node.js 16 LTS or later ([non-EOL](https://endoflife.date/nodejs)) versions.
250
-
- Deno v1.28.0 or higher (experimental).
251
-
Use `import OpenAI from "npm:openai"`.
273
+
- Deno v1.28.0 or higher, using `import OpenAI from "npm:openai"`.
274
+
Deno Deploy is not yet supported.
275
+
- Cloudflare Workers.
276
+
- Vercel Edge Runtime.
252
277
253
278
If you are interested in other runtime environments, please open or upvote an issue on GitHub.
0 commit comments