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

# Vercel AI SDK

> Generate and stream Phala responses in TypeScript applications.

Use the AI SDK's OpenAI-compatible provider with Phala.

```bash theme={"system"}
npm install ai @ai-sdk/openai-compatible
export PHALA_AI_API_KEY="YOUR_API_KEY"
```

## Configure the provider

```typescript theme={"system"}
import { createOpenAICompatible } from "@ai-sdk/openai-compatible";

const apiKey = process.env.PHALA_AI_API_KEY;
if (!apiKey) throw new Error("PHALA_AI_API_KEY is required");

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

Use `.chatModel()` with an exact model ID. The provider sends that ID without adding a prefix.

## Generate text

```typescript theme={"system"}
import { generateText } from "ai";

import { phala } from "./phala";

const { text } = await generateText({
  model: phala.chatModel("z-ai/glm-5.3"),
  prompt: "Explain attestation in one sentence.",
});
console.log(text);
```

## Stream from a route

```typescript app/api/generate/route.ts theme={"system"}
import { streamText } from "ai";

import { phala } from "../../../phala";

export async function POST(request: Request) {
  const body: unknown = await request.json().catch(() => null);
  if (
    typeof body !== "object" ||
    body === null ||
    !("prompt" in body) ||
    typeof body.prompt !== "string" ||
    body.prompt.trim().length === 0
  ) {
    return Response.json({ error: "A non-empty prompt is required" }, { status: 400 });
  }

  const result = streamText({
    model: phala.chatModel("z-ai/glm-5.3"),
    prompt: body.prompt.trim(),
  });
  return result.toTextStreamResponse();
}
```

Keep the Phala key on the server. Validate tool arguments and bound multi-step tool loops.

## Verification boundary

The base-URL configuration does not verify ACI locally. Node and Bun applications can pass
`connectAci().fetch` to `createOpenAICompatible`, then audit the retained receipt after generation.
See the [verified Node and Bun transport](https://docs.phala.com/phala-cloud/confidential-ai/confidential-model/integrations/openai-sdk.md#verified-node-and-bun-transport).

## Related

* [AI SDK OpenAI-compatible provider](https://ai-sdk.dev/providers/openai-compatible-providers)
* [AI SDK `streamText`](https://ai-sdk.dev/docs/reference/ai-sdk-core/stream-text)
* [AI SDK UI chat](https://ai-sdk.dev/docs/ai-sdk-ui/chatbot)
* [Phala function calling](https://docs.phala.com/phala-cloud/confidential-ai/confidential-model/tool-calling.md)


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