Fastify

01 Prep

Prepare a Fastify app for Anvia routes.

Use this path when Anvia runs inside a Fastify API or plugin-based Node service.

1. Create A Fastify Project

mkdir anvia-fastify
cd anvia-fastify
pnpm init
pnpm add fastify fastify-plugin zod
pnpm add -D tsx typescript @types/node

2. Install Anvia

pnpm add @anvia/core @anvia/openai

Install other provider packages when needed:

pnpm add @anvia/anthropic @anvia/gemini @anvia/mistral

3. Add Environment Variables

OPENAI_API_KEY=sk_...

Read the value in server code:

const apiKey = process.env.OPENAI_API_KEY;

if (!apiKey) {
  throw new Error("OPENAI_API_KEY is required");
}

4. Choose File Boundaries

FilePurpose
src/ai/support-agent.tsProvider client, model, tools, and reusable agent
src/routes/support.tsFastify plugin with prompt and stream routes
src/plugins/auth.tsAuth decoration and hooks
src/app.tsFastify instance and plugin registration

Next

Build the reusable agent in Setup Anvia. Read Runtime Boundaries for the SDK and application boundaries.