Fastify Adapter
Use Stepflow with Fastify
The Fastify adapter allows you to integrate Stepflow into your Fastify applications using a plugin.
Installation
pnpm add @stepflowjs/adapter-fastifyUsage
Register the stepflowPlugin with your Fastify instance.
import Fastify from "fastify";
import { Stepflow } from "@stepflowjs/core";
import { stepflowPlugin } from "@stepflowjs/adapter-fastify";
const stepflow = new Stepflow({
/* config */
});
const fastify = Fastify();
// Register Stepflow plugin
fastify.register(stepflowPlugin, {
stepflow,
basePath: "/api/stepflow",
});
fastify.listen({ port: 3000 });Configuration
The stepflowPlugin accepts the following options:
| Option | Type | Description |
|---|---|---|
stepflow | Stepflow | Required. The Stepflow engine instance |
basePath | string | Prefix for all routes (e.g., "/api/stepflow") |
endpoints | EndpointOption | Preset or fine-grained endpoint configuration |
auth | FastifyAuthConfig | Authorization configuration |
healthCheck | () => Promise<boolean> | Custom health check function |
onAuthFailure | Function | Callback when authorization fails |
Authorization Example
import { stepflowPlugin, createApiKeyAuth } from "@stepflowjs/adapter-fastify";
fastify.register(stepflowPlugin, {
stepflow,
auth: {
global: createApiKeyAuth(process.env.STEPFLOW_API_KEY),
health: () => ({ ok: true }), // Public health check
},
});Fastify-specific Context
When writing custom auth handlers for Fastify, the extra property in the AuthContext contains the Fastify Request object.
const customAuth = (ctx) => {
const request = ctx.extra; // Fastify Request
const user = request.user;
if (!user) return { ok: false, message: "User not authenticated" };
return { ok: true };
};