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

# OpenAI SDK

> Use Phala through the OpenAI SDK, with optional local ACI verification on Node and Bun.

Phala supports the OpenAI SDK's Responses, Chat Completions, and Embeddings resources. Set the
base URL to `https://inference.phala.com/v1` and use an exact Phala model ID.

> [!WARNING]
>
> Changing only the base URL does not make the SDK a local ACI verifier.

```bash Python theme={"system"}
pip install openai
```

```bash TypeScript theme={"system"}
npm install openai
```

```bash theme={"system"}
export PHALA_AI_API_KEY="YOUR_API_KEY"
```

## Responses

```python Python theme={"system"}
import os

from openai import OpenAI

client = OpenAI(
    api_key=os.environ["PHALA_AI_API_KEY"],
    base_url="https://inference.phala.com/v1",
)
response = client.responses.create(
    model="z-ai/glm-5.3",
    input="Explain attestation in one sentence.",
)
print(response.output_text)
```

```typescript TypeScript theme={"system"}
import OpenAI from "openai";

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

const client = new OpenAI({
  apiKey,
  baseURL: "https://inference.phala.com/v1",
});
const response = await client.responses.create({
  model: "z-ai/glm-5.3",
  input: "Explain attestation in one sentence.",
});
console.log(response.output_text);
```

## Chat completions

Use Chat Completions when an existing application expects that API shape:

```typescript theme={"system"}
const response = await client.chat.completions.create({
  model: "z-ai/glm-5.3",
  messages: [{ role: "user", content: "Reply with: connected" }],
});
console.log(response.choices[0]?.message.content);
```

Pass `stream=True` in Python or `stream: true` in TypeScript for server-sent events. See
[Streaming](https://docs.phala.com/phala-cloud/confidential-ai/confidential-model/streaming.md) and [Function calling](https://docs.phala.com/phala-cloud/confidential-ai/confidential-model/tool-calling.md) for complete loops.

## Embeddings

Embedding models use a separate catalog:

```bash theme={"system"}
curl -s https://inference.phala.com/v1/embeddings/models \
  | jq -r '.data[] | [.id, .is_tee, .context_length] | @tsv'
```

```python theme={"system"}
response = client.embeddings.create(
    model="qwen/qwen3-embedding-8b",
    input="Receipts bind requests and responses to the serving path.",
)
embedding = response.data[0].embedding
```

## Verified Node and Bun transport

Node 20.18.1+ and Bun 1.4.0+ can inject the same ACI runtime `fetch`:

```bash theme={"system"}
npm install openai @phala/aci-verifier
```

```typescript theme={"system"}
import OpenAI from "openai";
import { connectAci } from "@phala/aci-verifier/runtime";

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

const aci = await connectAci({
  baseURL: "https://inference.phala.com/v1",
  serving: { requireVerified: true, requireReceipt: true },
});

try {
  if (!aci.identity.transcript.verdict.verified) {
    throw new Error(aci.identity.transcript.verdict.line);
  }

  const client = new OpenAI({ apiKey, baseURL: aci.baseURL, fetch: aci.fetch });
  const response = await client.chat.completions.create({
    model: "z-ai/glm-5.3",
    messages: [{ role: "user", content: "Explain attestation briefly." }],
  });

  const audit = await aci.verifyReceipt();
  if (!audit.transcript.verdict.verified) {
    throw new Error(audit.transcript.verdict.line);
  }
  console.log(response.choices[0]?.message.content);
} finally {
  await aci.close();
}
```

The connection verifies the TDX quote, measured compose, identity expiry, hostname, and attested TLS
SPKI before model traffic. `verifyReceipt()` checks the latest retained exchange, signed receipt,
wire hashes, and cited session.

See [Release policy](https://docs.phala.com/phala-cloud/confidential-ai/verify/verify-signature.md#release-policy) for compose allowlists and OS-image assurance.

## Production checks

This endpoint rejects non-TEE models and requires verified upstream serving. Set timeouts and bounded retries, and do not expose raw upstream errors to users.

## Related

* [OpenAI Python SDK](https://github.com/openai/openai-python)
* [OpenAI TypeScript SDK](https://github.com/openai/openai-node)
* [Verify a response](https://docs.phala.com/phala-cloud/confidential-ai/verify/typescript-sdk.md)
* [Phala Responses API](https://docs.phala.com/phala-cloud/confidential-ai/confidential-model/api-reference/responses.md)


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