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

# TypeScript SDK

> The official NanoGPT client for TypeScript, @nanogpt/sdk

# TypeScript SDK

`@nanogpt/sdk` is NanoGPT's official client. It has no runtime dependencies, is ESM-only and needs Node.js 22 or later, or another runtime with Fetch and Web Streams.

```bash theme={null}
npm install @nanogpt/sdk
```

```typescript theme={null}
import { NanoGPT } from '@nanogpt/sdk';

const client = new NanoGPT({ apiKey: process.env.NANOGPT_API_KEY! });

const completion = await client.chat.completions.create({
  model: 'openai/gpt-6.1-sol',
  messages: [{ role: 'user', content: 'Say hello in one sentence.' }],
});

console.log(completion.choices[0]?.message?.content);
```

## What it covers

| Area | Methods |
| - | - |
| Chat Completions | `chat.completions.create`, `chat.completions.stream` |
| Responses | `responses.create`, `responses.stream`, `responses.retrieve`, `responses.delete` |
| Anthropic Messages | `messages.create`, `messages.stream`, `messages.countTokens` |
| Embeddings | `embeddings.create` |
| Models | `models.list` |
| Account | `account.currentApiKey`, `account.usage`, `account.requestBilling`, `account.checkBalance`, `account.createDeposit`, `account.getDepositStatus` |

For anything else, call the HTTP API directly. See the [API reference](https://docs.nano-gpt.com/api-reference/introduction).

## Streaming and errors

Use the `stream` helpers. The `create` helpers return one JSON response and reject `stream: true`.

```typescript theme={null}
import { NanoGPT, NanoGPTError } from '@nanogpt/sdk';

const client = new NanoGPT({ apiKey: process.env.NANOGPT_API_KEY! });

try {
  for await (const chunk of client.chat.completions.stream({
    model: 'openai/gpt-6.1-sol',
    messages: [{ role: 'user', content: 'Count to three.' }],
  })) {
    console.log(chunk);
  }
} catch (error) {
  if (error instanceof NanoGPTError) {
    console.error(error.status, error.message, error.headers.get('retry-after'));
  } else {
    throw error;
  }
}
```

A failure after a stream has started arrives as an event on the HTTP 200 response. The SDK throws it as a `NanoGPTError` with the status from that event, or 500 if the event has none, so output you have already received may be partial.

To cancel a request or set a timeout, pass an `AbortSignal` as `signal`. To read headers such as `X-Request-ID` before the body, pass `onResponse`.

## API host

The client defaults to `https://api.nano-gpt.com`, the [direct API host](https://docs.nano-gpt.com/api-reference/miscellaneous/api-hosts.md). Set `baseURL` to use another host.

## Using the OpenAI SDK instead

NanoGPT is OpenAI-compatible, so the official OpenAI SDK also works:

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

const client = new OpenAI({
  apiKey: process.env.NANOGPT_API_KEY,
  baseURL: 'https://api.nano-gpt.com/api/v1',
});
```

## Other packages

`nanogptjs`, `nanogpt-client` and the npm package named `nanogpt` aren't published or reviewed by NanoGPT. They would receive your API key, so check them yourself before using them.


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