RECIPE

Fastify primer

Wire Meridian into a Fastify server in under five minutes. This primer walks through installing the SDK, registering a chat route, and streaming completions back to your client with the lowest possible overhead. Fastify pairs well with Meridian because both stacks prioritize predictable performance, schema-first contracts, and lean dependency footprints.

1. Install the SDK

Add the Meridian Node SDK alongside Fastify. The SDK ships with TypeScript types and a zero-config OpenAI-compatible client, so no extra adapters are needed.

npm install fastify @meridian/sdk
# or
pnpm add fastify @meridian/sdk

2. Register a chat route

Create a POST route that accepts a prompt and forwards it to Meridian. Keep your API key in an environment variable and pass the adaptive model router so Meridian picks the best backend per request.

import Fastify from 'fastify';
import { Meridian } from '@meridian/sdk';

const app = Fastify({ logger: true });
const client = new Meridian({ apiKey: process.env.MERIDIAN_KEY });

app.post('/chat', async (req, reply) => {
  const { prompt } = req.body as { prompt: string };
  const out = await client.chat.completions.create({
    model: 'azure/model-router',
    messages: [{ role: 'user', content: prompt }],
  });
  return { reply: out.choices[0].message.content };
});

app.listen({ port: 3000 });

3. Stream tokens to the client

For interactive UX, pipe the SSE stream directly through Fastify's raw reply. This keeps latency to first token under 300ms in production and avoids buffering the full completion server-side. Set the right headers, flush on each chunk, and end the stream when the SDK iterator completes.

app.post('/stream', async (req, reply) => {
  reply.raw.writeHead(200, {
    'Content-Type': 'text/event-stream',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive',
  });
  const stream = await client.chat.completions.create({
    model: 'azure/model-router',
    messages: req.body.messages,
    stream: true,
  });
  for await (const chunk of stream) {
    reply.raw.write(`data: ${JSON.stringify(chunk)}\n\n`);
  }
  reply.raw.end();
});

Next up: pair this server with the Meridian React hooks for a complete full-stack integration, or wire in usage logging via the cost cockpit.

All recipes