Skip to content
Source
Sign inSign up
TanStack/ai

packages/ai-cloudflare

Contents of packages/ai-cloudflare
NameLast commit messageLast commit date
..
src (directory)
tests (directory)
CHANGELOG.md
LICENSE
package.json
README.md
tsconfig.json
vite.config.ts

README.md

TanStack AI

@tanstack/ai-cloudflare

Cloudflare adapter for TanStack AI: Workers AI chat, summarization, embeddings, image generation, text-to-speech, and transcription over the env.AI binding or the REST API, with AI Gateway routing for any provider.

Installation

npm install @tanstack/ai @tanstack/ai-cloudflare

Usage

import { chat, toServerSentEventsResponse } from '@tanstack/ai'
import { createCloudflareText } from '@tanstack/ai-cloudflare'
import type { Ai } from '@cloudflare/workers-types'

export default {
  async fetch(request: Request, env: { AI: Ai }) {
    const { messages, threadId } = await request.json()
    const stream = chat({
      adapter: createCloudflareText('@cf/zai-org/glm-5.3-flash', {
        binding: env.AI,
      }),
      messages,
      threadId,
    })
    return toServerSentEventsResponse(stream)
  },
}

Outside a Worker, pass { accountId, apiKey } or set CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN and use cloudflareText(model).

See the docs for AI Gateway, media adapters, and model options.