Skip to content

Variable: fastify

@providerprotocol/ai


@providerprotocol/ai / proxy / fastify

const fastify: object

Defined in: src/providers/proxy/server/fastify.ts:230

Fastify adapter utilities.

sendEmbeddingJSON: (result, reply) => FastifyReply

Send an EmbeddingResult as JSON response.

EmbeddingResult

The embedding result

FastifyReply

Fastify reply object

FastifyReply

sendError: (message, status, reply) => FastifyReply

Send an error response.

string

Error message

number

HTTP status code

FastifyReply

Fastify reply object

FastifyReply

sendImageJSON: (result, reply) => FastifyReply

Send an ImageResult as JSON response.

ImageResult

The image result

FastifyReply

Fastify reply object

FastifyReply

sendJSON: (turn, reply) => FastifyReply

Send a Turn as JSON response.

Turn

The completed inference turn

FastifyReply

Fastify reply object

FastifyReply

const turn = await instance.generate(messages);
return fastifyAdapter.sendJSON(turn, reply);

streamImageSSE: (stream, reply) => FastifyReply

Stream an ImageStreamResult as Server-Sent Events.

ImageStreamLike

The ImageStreamResult or ImageProviderStreamResult from image().stream()

FastifyReply

Fastify reply object

FastifyReply

streamSSE: (stream, reply) => FastifyReply

Stream a StreamResult as Server-Sent Events.

StreamResult

The StreamResult from instance.stream()

FastifyReply

Fastify reply object

FastifyReply

const stream = instance.stream(messages);
return fastifyAdapter.streamSSE(stream, reply);
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 them
const 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 platform
app.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);
});