Variable: fastify
@providerprotocol/ai / proxy / fastify
Variable: fastify
Section titled “Variable: fastify”
constfastify:object
Defined in: src/providers/proxy/server/fastify.ts:230
Fastify adapter utilities.
Type Declaration
Section titled “Type Declaration”sendEmbeddingJSON()
Section titled “sendEmbeddingJSON()”sendEmbeddingJSON: (
result,reply) =>FastifyReply
Send an EmbeddingResult as JSON response.
Parameters
Section titled “Parameters”result
Section titled “result”The embedding result
FastifyReply
Fastify reply object
Returns
Section titled “Returns”FastifyReply
sendError()
Section titled “sendError()”sendError: (
message,status,reply) =>FastifyReply
Send an error response.
Parameters
Section titled “Parameters”message
Section titled “message”string
Error message
status
Section titled “status”number
HTTP status code
FastifyReply
Fastify reply object
Returns
Section titled “Returns”FastifyReply
sendImageJSON()
Section titled “sendImageJSON()”sendImageJSON: (
result,reply) =>FastifyReply
Send an ImageResult as JSON response.
Parameters
Section titled “Parameters”result
Section titled “result”The image result
FastifyReply
Fastify reply object
Returns
Section titled “Returns”FastifyReply
sendJSON()
Section titled “sendJSON()”sendJSON: (
turn,reply) =>FastifyReply
Send a Turn as JSON response.
Parameters
Section titled “Parameters”The completed inference turn
FastifyReply
Fastify reply object
Returns
Section titled “Returns”FastifyReply
Example
Section titled “Example”const turn = await instance.generate(messages);return fastifyAdapter.sendJSON(turn, reply);streamImageSSE()
Section titled “streamImageSSE()”streamImageSSE: (
stream,reply) =>FastifyReply
Stream an ImageStreamResult as Server-Sent Events.
Parameters
Section titled “Parameters”stream
Section titled “stream”ImageStreamLike
The ImageStreamResult or ImageProviderStreamResult from image().stream()
FastifyReply
Fastify reply object
Returns
Section titled “Returns”FastifyReply
streamSSE()
Section titled “streamSSE()”streamSSE: (
stream,reply) =>FastifyReply
Stream a StreamResult as Server-Sent Events.
Parameters
Section titled “Parameters”stream
Section titled “stream”The StreamResult from instance.stream()
FastifyReply
Fastify reply object
Returns
Section titled “Returns”FastifyReply
Example
Section titled “Example”const stream = instance.stream(messages);return fastifyAdapter.streamSSE(stream, reply);Examples
Section titled “Examples”import Fastify from 'fastify';import { llm } from '@providerprotocol/ai';import { anthropic } from '@providerprotocol/ai/anthropic';import { parseBody } from '@providerprotocol/ai/proxy';import { fastify as fastifyAdapter } from '@providerprotocol/ai/proxy/server';
const app = Fastify();
app.post('/api/ai', async (request, reply) => { const { messages, system, params } = parseBody(request.body); const instance = llm({ model: anthropic('claude-sonnet-4-20250514'), system });
if (request.headers.accept?.includes('text/event-stream')) { return fastifyAdapter.streamSSE(instance.stream(messages), reply); } else { const turn = await instance.generate(messages); return fastifyAdapter.sendJSON(turn, reply); }});import Fastify from 'fastify';import { llm, exponentialBackoff, roundRobinKeys } from '@providerprotocol/ai';import { anthropic } from '@providerprotocol/ai/anthropic';import { parseBody } from '@providerprotocol/ai/proxy';import { fastify as fastifyAdapter } from '@providerprotocol/ai/proxy/server';
const app = Fastify();
// Server manages AI provider keys - users never see themconst claude = llm({ model: anthropic('claude-sonnet-4-20250514'), config: { apiKey: roundRobinKeys([process.env.ANTHROPIC_KEY_1!, process.env.ANTHROPIC_KEY_2!]), retryStrategy: exponentialBackoff({ maxAttempts: 3 }), },});
// Auth hook for your platformapp.addHook('preHandler', async (request, reply) => { const token = request.headers.authorization?.replace('Bearer ', ''); const user = await validatePlatformToken(token); if (!user) { reply.status(401).send({ error: 'Unauthorized' }); return; } request.user = user;});
app.post('/api/ai', async (request, reply) => { // Track usage per user // await trackUsage(request.user.id);
const { messages, system, params } = parseBody(request.body);
if (params?.stream) { return fastifyAdapter.streamSSE(claude.stream(messages, { system }), reply); } const turn = await claude.generate(messages, { system }); return fastifyAdapter.sendJSON(turn, reply);});