## TL;DR

Run with node --env-file=.env, or load your env loader before importing @clerk/fastify.

```text
import Fastify from 'fastify'
import { clerkClient, clerkPlugin, getAuth } from '@clerk/fastify'

const fastify = Fastify({ logger: true })
fastify.register(clerkPlugin)

fastify.get('/protected', async (request, reply) => {
  const { isAuthenticated, userId } = getAuth(request)
  if (!isAuthenticated) {
    return reply.code(401).send({ error: 'User not authenticated' })
  }
  const user = await clerkClient.users.getUser(userId)
  return reply.send({ message: 'ok', user })
})
```

## Steps

1. The Clerk Fastify guide assumes ECMAScript modules: `"type": "module"` in package.json. Mixing `require()` with the ESM-only imports is the first failure.

2. Env vars (`CLERK_PUBLISHABLE_KEY`, `CLERK_SECRET_KEY`) must be available before any Clerk module initializes. Run with `node --env-file=.env`, or load your env loader before importing `@clerk/fastify`. Same rule as Express: Clerk reads keys at import time.

3. - `fastify.register(clerkPlugin)` with no call parens: it is a plugin function, not middleware factory output.
- TypeScript: `getAuth(request)` is typed against the Fastify request; do not pass a raw Node req.
- As with Express, `clerkClient.users.getUser()` is a Backend API call per invocation; cache or avoid in hot paths.

## When to use

You are seeing this: The Clerk Fastify guide assumes ECMAScript modules: "type": "module" in package.json. Use this skill when you run into "Clerk + Fastify: clerkPlugin() registration, ESM, and getAuth(request)".

## When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

## Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

## Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
