Skip to main content

Vercel AI SDK Setup

Connect an existing Vercel AI SDK app to GonkaGate.

Connect an existing Vercel AI SDK app to GonkaGate by creating one OpenAI-compatible provider, using a current model ID, and returning result.toTextStreamResponse() from your server route. Keep GONKAGATE_API_KEY in the same server runtime that handles the route.

Install the provider package

Installation
npm install @ai-sdk/openai-compatible

Create one shared provider

Create one provider and reuse it across your routes:

Create one shared provider
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";

const apiKey = process.env.GONKAGATE_API_KEY;

if (!apiKey) {
  throw new Error("Set GONKAGATE_API_KEY");
}

export const gonkagate = createOpenAICompatible({
  name: "gonkagate",
  apiKey,
  baseURL: "https://api.gonkagate.com/v1",
});

Use it in a server route

Use the provider in a server route that returns streamed text:

Use it in a server route
import { streamText } from "ai";

import { gonkagate } from "@/lib/gonkagate";

export async function POST(request: Request) {
  const { prompt } = (await request.json()) as { prompt: string };

  const result = streamText({
    model: gonkagate("<model-id-from-get-v1-models>"),
    prompt,
  });

  return result.toTextStreamResponse();
}

Replace <model-id-from-get-v1-models> with a current value from GET /v1/models.

Verify the stream

Send one short request through the route:

Request Example
curl http://localhost:3000/api/chat \
  -H "Content-Type: application/json" \
  -d '{"prompt":"Return exactly: AI SDK connected"}'

Expected result: the route returns streamed text that includes AI SDK connected.

Common failures

Response or symptomWhat it usually meansWhat to do
GONKAGATE_API_KEY is missingThe variable is not loaded in the server runtime that handles the routeLoad it in the same runtime where the route runs
401 invalid_api_keyThe key value or key state is wrongRecheck Authentication and API Keys
404 model_not_foundThe model ID is stale or unsupportedRefresh it from GET /v1/models
429 insufficient_quotaThe prepaid USD balance is too low for the requestTop up before retrying
429 rate_limit_exceededYou hit a runtime limitHonor Retry-After and add bounded backoff
The route responds but the client still handles the stream incorrectlyThe route and caller no longer agree on a text-stream responseKeep result.toTextStreamResponse() on the route and make sure the caller expects streamed text

See also

Was this page helpful?