Loading...
Loading...
Guide for using Netlify AI Gateway to access AI models. Use when adding AI capabilities or selecting/changing AI models. Must be read before choosing a model. Covers supported providers (OpenAI, Anthropic, Google), SDK setup, environment variables, and the list of available models.
npx skill4agent add netlify/context-and-tools netlify-ai-gatewayIMPORTANT: Only use models listed in the "Available Models" section below. AI Gateway does not support every model a provider offers. Using an unsupported model will cause runtime errors.
OPENAI_BASE_URLnpm install openaiimport OpenAI from "openai";
const openai = new OpenAI();
// OPENAI_BASE_URL is auto-configured — no API key or base URL needed
const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Hello!" }],
});npm install @anthropic-ai/sdkimport Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
baseURL: Netlify.env.get("ANTHROPIC_BASE_URL"),
});
const message = await client.messages.create({
model: "claude-sonnet-4-5-20250929",
max_tokens: 1024,
messages: [{ role: "user", content: "Hello!" }],
});npm install @google/generative-aiimport { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI("placeholder");
// Configure base URL via environment variable
const model = genAI.getGenerativeModel({ model: "gemini-2.5-flash" });
const result = await model.generateContent("Hello!");import type { Config, Context } from "@netlify/functions";
import OpenAI from "openai";
export default async (req: Request, context: Context) => {
const { prompt } = await req.json();
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: prompt }],
});
return Response.json({
response: completion.choices[0].message.content,
});
};
export const config: Config = {
path: "/api/ai",
method: "POST",
};| Variable | Provider | Set by |
|---|---|---|
| OpenAI | Netlify (automatic) |
| Anthropic | Netlify (automatic) |
@netlify/vite-pluginnetlify dev