> ## Documentation Index
> Fetch the complete documentation index at: https://docs.inputapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript SDK

> Configure a compatible base URL for Chat Completions and Responses.

Use the official OpenAI TypeScript SDK. Set the [environment variables](/en/guides/api-basics) first. Run these examples on a server or in a terminal.

## Install

Requires Node.js 22.18+ or Node.js 24 LTS to run `.mts` directly.

```bash theme={null}
npm install openai@7.33.0
```

These are the versions used to verify the examples. Upgrade according to your dependency policy and retest.

## Chat Completions

Save as `chat.mts`.

```typescript theme={null}
import OpenAI from 'openai';

const { API_BASE_URL, API_KEY, API_MODEL } = process.env;
if (!API_BASE_URL || !API_KEY || !API_MODEL) {
  throw new Error('Set API_BASE_URL, API_KEY, and API_MODEL first.');
}

const client = new OpenAI({
  baseURL: API_BASE_URL.replace(/\/+$/, ''),
  apiKey: API_KEY,
  timeout: 60_000,
  maxRetries: 0,
});

try {
  const result = await client.chat.completions.create({
    model: API_MODEL,
    messages: [{ role: 'user', content: 'Say hello in one short sentence.' }],
  });
  console.log(result.choices[0]?.message.content ?? '');
} catch (error) {
  console.error(error instanceof OpenAI.APIError && error.status
    ? `Request failed (HTTP ${error.status}). Check the request logs.`
    : 'Request failed. Check the API address and network.');
  process.exitCode = 1;
}
```

## Responses

Save as `responses.mts`. Select a Responses-compatible model first.

```typescript theme={null}
import OpenAI from 'openai';

const { API_BASE_URL, API_KEY, API_MODEL } = process.env;
if (!API_BASE_URL || !API_KEY || !API_MODEL) {
  throw new Error('Set API_BASE_URL, API_KEY, and API_MODEL first.');
}

const client = new OpenAI({
  baseURL: API_BASE_URL.replace(/\/+$/, ''),
  apiKey: API_KEY,
  timeout: 60_000,
  maxRetries: 0,
});

try {
  const result = await client.responses.create({
    model: API_MODEL,
    input: 'Say hello in one short sentence.',
  });
  if (result.status !== 'completed' || !result.output_text.trim()) {
    throw new Error('Response did not complete with usable text.');
  }
  for (const item of result.output) {
    if (item.type === 'reasoning') continue;
    if (item.type !== 'message' || item.status !== 'completed'
      || item.content.some((part) => part.type !== 'output_text')) {
      throw new Error('Response did not complete with usable text.');
    }
  }
  console.log(result.output_text);
} catch (error) {
  console.error(error instanceof OpenAI.APIError && error.status
    ? `Request failed (HTTP ${error.status}). Check the request logs.`
    : 'Response failed or did not complete. Check the request logs.');
  process.exitCode = 1;
}
```

## Run and handle results

```bash theme={null}
node chat.mts
node responses.mts
```

Each command sends a real request; run only the one you need. Examples use a 60-second timeout and disable SDK retries to avoid implicit repeated requests. Design timeout, retry, and concurrency policies for production.

Chat Completions text can be empty, for example during tool calls. The Responses example requires `completed` status. `output_text` is an SDK convenience property that joins text; handle tools and other non-text output separately.

[Streaming](/en/guides/streaming) · [Error handling](/en/guides/errors)

Reference: [Official OpenAI TypeScript SDK documentation](https://developers.openai.com/api/reference/typescript)。

The text-only Responses example rejects empty text, refusals, tool calls, and incomplete output. Continue with the full Responses examples in [conversations](/en/guides/conversations), [tool calling](/en/guides/tool-calling), and [structured output](/en/guides/structured-output).

[简体中文](/zh/guides/typescript)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.