fastify-x-ai
fastify-x-ai
A Fastify plugin wrapping the Vercel AI SDK for unified access to OpenAI, Anthropic, and Google AI providers. Exposes text generation, streaming, chat, embeddings, structured output, and tool calling through a single fastify.xAi decorator.
Installation
npm install @xenterprises/fastify-x-ai ai
# Install provider SDKs as needed
npm install @ai-sdk/openai # OpenAI / GPT models
npm install @ai-sdk/anthropic # Anthropic / Claude models
npm install @ai-sdk/google # Google / Gemini models
Quick Start
import Fastify from "fastify";
import xAI from "@xenterprises/fastify-x-ai";
const fastify = Fastify();
await fastify.register(xAI, {
defaultProvider: "openai",
providers: {
openai: { apiKey: process.env.OPENAI_API_KEY },
},
});
// Simple completion
const text = await fastify.xAi.complete("Write a haiku about coding");
// Chat endpoint
fastify.post("/chat", async (request, reply) => {
const result = await fastify.xAi.chat({ messages: request.body.messages });
return { text: result.text };
});
Options
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
active | boolean | true | No | Set false to disable the plugin entirely |
defaultProvider | string | "openai" | No | Default provider: openai, anthropic, or google |
defaultModel | string | Provider default | No | Default model name (falls back to per-provider defaults) |
defaultMaxTokens | number | 4096 | No | Default max tokens; must be a positive number |
defaultTemperature | number | 0.7 | No | Default temperature; must be 0–2 |
providers | object | {} | No | Per-provider config objects |
providers.openai.apiKey | string | — | If providers.openai is set | OpenAI API key |
providers.openai.baseURL | string | — | No | Custom OpenAI-compatible endpoint (passed to the SDK client) |
providers.anthropic.apiKey | string | — | If providers.anthropic is set | Anthropic API key |
providers.anthropic.baseURL | string | — | No | Custom Anthropic-compatible endpoint (passed to the SDK client) |
providers.google.apiKey | string | — | If providers.google is set | Google API key |
providers.google.baseURL | string | — | No | Custom Google-compatible endpoint (passed to the SDK client) |
The plugin never reads process.env — pass API keys in explicitly. A provider is
configured only when its providers.<name> entry is present, and each entry requires
an apiKey. If the matching provider SDK (@ai-sdk/openai, @ai-sdk/anthropic,
@ai-sdk/google) is not installed, registration logs a warning and that provider is
skipped.
Default Models
| Provider | Default Model |
|---|---|
openai | gpt-4o |
anthropic | claude-sonnet-4-20250514 |
google | gemini-2.0-flash |
Methods
All methods are available on fastify.xAi.
- generate — Full-control text generation with prompt, messages, tools, and per-call model overrides.
- stream — Streaming text generation; returns an async-iterable
textStream. - chat — Chat with conversation history; delegates to
generateorstream. - complete — Convenience wrapper that returns
result.textdirectly from a prompt string. - createEmbedding — Create single or batch embeddings; includes
similarity()helper. - generateStructured — Generate structured output validated against a Zod schema.
- getModel / raw — Get a raw model instance; access underlying AI SDK functions via
fastify.xAi.raw.
Error Reference
Registration fails fast with actionable Errors:
| Error | Cause |
|---|---|
xai: option `defaultProvider` must be one of "openai", "anthropic", "google", e.g. ... | Invalid defaultProvider |
xai: option `defaultModel` must be a string, e.g. ... | defaultModel is not a string |
xai: option `defaultMaxTokens` must be a positive number, e.g. ... | defaultMaxTokens < 1 or not a number |
xai: option `defaultTemperature` must be a number between 0 and 2, e.g. ... | defaultTemperature outside 0–2 or not a number |
xai: option `providers` must be an object, e.g. ... | providers is not an object |
xai: option `providers` has unknown provider "<name>", must be one of "openai", "anthropic", "google" | Unknown key in providers |
xai: missing required option `providers.<name>.apiKey` (string), e.g. ... | Provider entry present without a valid apiKey |
xai: option `providers.<name>.baseURL` must be a string, e.g. ... | baseURL present but not a string |
xai: 'ai' package is required. Install with: npm install ai | ai peer dependency not installed |
At call time:
| Error | Cause |
|---|---|
xAI: Provider '…' not configured. Available: … | Method called with a provider that is not configured |
xAI generate: Either 'prompt' or 'messages' is required | generate() called without input |
xAI stream: Either 'prompt' or 'messages' is required | stream() called without input |
xAI chat: 'messages' is required | chat() called without messages |
xAI complete: 'prompt' is required | complete() called with empty/missing prompt |
xAI createEmbedding: Either 'text' or 'texts' is required | createEmbedding() called without input |
xAI generateStructured: 'prompt' is required | generateStructured() called without prompt |
xAI generateStructured: 'schema' is required | generateStructured() called without schema |
Provider API errors (auth failures, rate limits, etc.) are AI SDK errors and propagate to the caller unchanged — use Fastify's error handling in routes.
Environment Variables
The plugin never reads process.env — the consumer owns env access and passes the
values into app.register(xAI, { ... }). These are the conventional variables a
consumer sets:
| Variable | Passed in as |
|---|---|
OPENAI_API_KEY | providers.openai.apiKey |
ANTHROPIC_API_KEY | providers.anthropic.apiKey |
GOOGLE_API_KEY | providers.google.apiKey |
await fastify.register(xAI, {
providers: {
openai: { apiKey: process.env.OPENAI_API_KEY }, // the consumer owns env access
},
});
A provider is configured only when its providers.<name> entry is present — there is
no env fallback or auto-detection.
How It Works
On registration the plugin validates options, dynamically imports the ai package, then attempts to load each configured provider SDK (@ai-sdk/openai, @ai-sdk/anthropic, @ai-sdk/google). A provider is initialized only when its providers.<name> entry is present with a valid apiKey — the plugin never reads environment variables; missing SDKs log a warning instead of failing. All public methods resolve the correct model via getModel, validate inputs, and delegate to the underlying AI SDK primitives (generateText, streamText, embed, embedMany). The decorated fastify.xAi object is available to every route handler and plugin in scope.
AI Context
package: "@xenterprises/fastify-x-ai"
type: fastify-plugin
use-when: Unified AI text generation, streaming, embeddings, and structured output via Vercel AI SDK — supports OpenAI, Anthropic, Google
decorator: fastify.xAi (generate, stream, chat, complete, createEmbedding, similarity, generateStructured, getModel, raw)
env: consumer-set OPENAI_API_KEY / ANTHROPIC_API_KEY / GOOGLE_API_KEY passed via providers.<name>.apiKey — plugin never reads process.env
defaults: openai/gpt-4o, anthropic/claude-sonnet-4-20250514, google/gemini-2.0-flash
peer-deps: ai, @ai-sdk/openai (and/or @ai-sdk/anthropic, @ai-sdk/google)
