> ## 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.

# Vercel AI SDK

> Using NanoGPT with the Vercel AI SDK through its OpenAI-compatible provider

> [!NOTE]
>
> These examples use the direct API host, `https://api.nano-gpt.com`. It has no IPv6 address, so clients with only IPv6 should use `https://nano-gpt.com` with the same paths. See [API Hosts](https://docs.nano-gpt.com/api-reference/miscellaneous/api-hosts.md).

# Using the Vercel AI SDK with NanoGPT

NanoGPT works with the [AI SDK](https://ai-sdk.dev) through the `@ai-sdk/openai-compatible` package. You only need a base URL and an API key; no NanoGPT-specific package is required.

## Setup

1. Get your API key from [nano-gpt.com/api](https://nano-gpt.com/api) and set it as a server-side environment variable:

   ```bash theme={null}
   export NANOGPT_API_KEY='your-api-key-here'
   ```

2. Install the AI SDK and its OpenAI-compatible provider:

   ```bash theme={null}
   npm install ai @ai-sdk/openai-compatible
   ```

## Create the provider

```ts theme={null}
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';

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

The `apiKey` setting sends your key as a Bearer token in the `Authorization` header.

## Generate text

```ts theme={null}
import { generateText } from 'ai';

const { text } = await generateText({
  model: nanogpt('openai/gpt-6.1-sol'),
  prompt: 'Explain what an API gateway does in one sentence.',
});

console.log(text);
```

## Stream text

```ts theme={null}
import { streamText } from 'ai';

const result = streamText({
  model: nanogpt('openai/gpt-6.1-sol'),
  prompt: 'Count from 1 to 5, separated by spaces.',
});

for await (const textPart of result.textStream) {
  process.stdout.write(textPart);
}
```

## Models

Pass any NanoGPT text model ID to the provider. [`GET /api/v1/models`](https://docs.nano-gpt.com/api-reference/endpoint/models) lists the current IDs, and `?detailed=true` adds each model's context length and capabilities. Tool calling, image input and reasoning depend on the model you select.

These examples were checked with `ai` 7.1.1 and `@ai-sdk/openai-compatible` 3.0.100. The SDK calls [`/api/v1/chat/completions`](https://docs.nano-gpt.com/api-reference/endpoint/chat-completion).


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