# Vercel AI SDK & Next.js — API Store Hub

> VI: Kết nối `@ai-sdk/openai` và `streamText` / `generateObject` vào https://apistorehub.com/v1 để truyền dữ liệu SSE mượt trên trình duyệt với chi phí ST minh bạch.
> EN: Connect `@ai-sdk/openai` with `streamText` and `generateObject` to https://apistorehub.com/v1 for low-latency SSE streaming and typed structured outputs.

- Canonical VI: https://apistorehub.com/vi/tich-hop/vercel-ai-sdk-nextjs
- Canonical EN: https://apistorehub.com/en/tich-hop/vercel-ai-sdk-nextjs
- Language: TypeScript
- Runtime: Node.js 20+, Edge Runtime, Vercel AI SDK v4+
- Protocol: OpenAI Compatible (/v1/chat/completions)
- Install: `npm install ai @ai-sdk/openai zod`
- Auth Env Var: `API_KEY` (prefix `sk-ash-`)

## Step-by-Step Setup
1. Install `ai`, `@ai-sdk/openai`, and `zod` in your TypeScript project.
2. Configure `createOpenAI({ baseURL: "https://apistorehub.com/v1", apiKey: process.env.API_KEY, compatibility: "compatible" })`.
3. Use `gateway.chat("claude-sonnet-5-5")` inside `streamText` or `generateObject` for strict chat completions routing.
4. Keep API keys strictly server-side and never expose them in browser bundles.

## Production Code Sample
```
import { createOpenAI } from "@ai-sdk/openai";
import { streamText } from "ai";

const gateway = createOpenAI({
  baseURL: "https://apistorehub.com/v1",
  apiKey: process.env.API_KEY,
  compatibility: "compatible",
});

export async function POST(req: Request) {
  const { messages } = await req.json();
  const result = streamText({
    model: gateway.chat("claude-sonnet-5-5"),
    messages,
    maxTokens: 2048,
  });
  return result.toDataStreamResponse();
}
```

## Quick cURL Check
```bash
curl -N https://apistorehub.com/v1/chat/completions \
  -H "Authorization: Bearer sk-ash-your_key" \
  -H "Content-Type: application/json" \
  -d '{"model":"claude-sonnet-5-5","stream":true,"messages":[{"role":"user","content":"Draft a TypeScript interface"}]}'
```

## Recommended Models (Live ST Pricing)
| Model | Live Price | Context | Datasheet |
|---|---|---|---|
| `claude-sonnet-5-5` | 7,700 ST / 1M tokens | 1M | https://apistorehub.com/llms/models/claude-sonnet-5-5.md |
| `gpt-6.1-terra` | 3,400 ST / 1M tokens | 1.1M | https://apistorehub.com/llms/models/gpt-6-1-terra.md |
| `gemini-4-neon` | 20,400 ST / 1M tokens | 2M | https://apistorehub.com/llms/models/gemini-4-neon.md |

## FAQ
- **Does `generateObject` with Zod work reliably on API Store Hub?**: Yes. Combining `gateway.chat("claude-sonnet-5-5")` with Zod schemas returns validated typed objects.
- **Can one handler switch between Claude, GPT, and DeepSeek dynamically?**: Yes. Pass any active catalog model ID to `gateway.chat(modelId)` using the same gateway instance.
- **How do I avoid reverse proxy buffering on SSE streams?**: Return `result.toDataStreamResponse()` and configure upstream proxies with `X-Accel-Buffering: no`.
