> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/vercel/workflow/llms.txt
> Use this file to discover all available pages before exploring further.

# Fastify

> Install and configure Workflow DevKit in your Fastify application

This guide shows you how to install Workflow DevKit in a Fastify application using Nitro as the build system.

<Steps>
  <Step title="Install dependencies">
    Install the required packages:

    <CodeGroup>
      ```bash npm theme={null}
      npm install workflow fastify nitro rollup
      ```

      ```bash yarn theme={null}
      yarn add workflow fastify nitro rollup
      ```

      ```bash pnpm theme={null}
      pnpm add workflow fastify nitro rollup
      ```
    </CodeGroup>

    <Note>
      Nitro provides the build system needed to compile workflows. Learn more at [nitro.build](https://nitro.build).
    </Note>
  </Step>

  <Step title="Configure Nitro">
    Create a `nitro.config.ts` file with the Workflow module:

    ```typescript nitro.config.ts theme={null}
    import { defineNitroConfig } from 'nitro/config';

    export default defineNitroConfig({
      modules: ['workflow/nitro'],
      vercel: { entryFormat: 'node' },
      routes: {
        '/**': { handler: './src/index.ts', format: 'node' },
      },
    });
    ```

    This configuration:

    * Enables the Workflow Nitro module
    * Routes all requests to your Fastify app
    * Uses Node.js format for compatibility with Fastify
  </Step>

  <Step title="Create your Fastify app">
    Create `src/index.ts` with your Fastify application:

    ```typescript src/index.ts theme={null}
    import Fastify from 'fastify';
    import { start } from 'workflow/api';
    import { handleUserSignup } from '../workflows/user-signup.js';

    const server = Fastify({
      logger: true,
    });

    // Configure JSON parsing for empty requests
    server.addContentTypeParser(
      'application/json',
      { parseAs: 'string' },
      (req, body, done) => {
        const text = typeof body === 'string' ? body : body.toString();
        if (!text) return done(null, {});
        try {
          done(null, JSON.parse(text));
        } catch (error) {
          done(error as Error);
        }
      }
    );

    server.post('/api/signup', async (req, reply) => {
      const { email } = req.body as { email: string };
      await start(handleUserSignup, [email]);
      return reply.send({ message: "User signup workflow started" });
    });

    await server.ready();

    export default (req: any, res: any) => {
      server.server.emit('request', req, res);
    };
    ```
  </Step>

  <Step title="Update package.json scripts">
    Add development and build scripts:

    ```json package.json theme={null}
    {
      "scripts": {
        "dev": "nitro dev",
        "build": "nitro build",
        "start": "node .output/server/index.mjs"
      }
    }
    ```
  </Step>

  <Step title="Enable TypeScript support (Optional)">
    Add the Workflow TypeScript plugin to your `tsconfig.json` for IntelliSense:

    ```json tsconfig.json theme={null}
    {
      "compilerOptions": {
        "plugins": [
          {
            "name": "workflow"
          }
        ]
      }
    }
    ```
  </Step>
</Steps>

## How it works

The Fastify + Nitro integration:

* Uses Nitro's build system to compile workflows
* Generates workflow route handlers at `.well-known/workflow/v1/*`
* Supports both development (with HMR) and production builds
* Routes all non-workflow requests to your Fastify app
* Automatically handles workflow execution and resumption

## Example workflow

```typescript workflows/user-signup.ts theme={null}
import { sleep } from "workflow";

export async function handleUserSignup(email: string) {
  "use workflow";

  const user = await createUser(email);
  await sendWelcomeEmail(user);
  await sleep("1 day");
  await sendOnboardingEmail(user);

  return { userId: user.id, status: "onboarded" };
}

async function createUser(email: string) {
  "use step";
  console.log(`Creating user with email: ${email}`);
  return { id: crypto.randomUUID(), email };
}

async function sendWelcomeEmail(user: { id: string; email: string }) {
  "use step";
  console.log(`Sending welcome email to user: ${user.id}`);
}

async function sendOnboardingEmail(user: { id: string; email: string }) {
  "use step";
  console.log(`Sending onboarding email to user: ${user.id}`);
}
```

## Development

Run the development server:

```bash theme={null}
npm run dev
```

Trigger a workflow:

```bash theme={null}
curl -X POST --json '{"email":"hello@example.com"}' http://localhost:3000/api/signup
```

## Production

Build and start the production server:

```bash theme={null}
npm run build
npm start
```

## Next steps

<CardGroup cols={2}>
  <Card title="Core Concepts" href="/concepts/workflows-and-steps">
    Learn about workflows and steps
  </Card>

  <Card title="Nitro Integration" href="/installation/nitro">
    Learn more about the Nitro integration
  </Card>
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.