StepflowStepflow

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-fastify

Usage

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:

OptionTypeDescription
stepflowStepflowRequired. The Stepflow engine instance
basePathstringPrefix for all routes (e.g., "/api/stepflow")
endpointsEndpointOptionPreset or fine-grained endpoint configuration
authFastifyAuthConfigAuthorization configuration
healthCheck() => Promise<boolean>Custom health check function
onAuthFailureFunctionCallback 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 };
};

On this page