Skip to content
Turnstile for NestJS v1.2.0

Stop Bots Before Your Handlers Run

nest-cloudflare-turnstile checks Cloudflare Turnstile tokens in a NestJS guard. Add one decorator and requests without a valid token never reach your code.

localhost:3000/login
auth.controller.tsapp.module.ts
1import { Controller, Post } from '@nestjs/common';2import { TurnstileCaptcha } from 'nest-cloudflare-turnstile';3 4@Controller('auth')5export class AuthController {6  @Post('login')7  @TurnstileCaptcha()8  login() {9    // Runs only after Cloudflare accepted the token.10  }11}
Terminal

[Nest] 4821 - LOG [NestApplication] Nest application successfully started

One Decorator

Put @TurnstileCaptcha() on a route or a controller. Or guard every route and mark the exceptions with @SkipTurnstile().

Fits Your Stack

Express and Fastify, GraphQL resolvers on Apollo or Mercurius, and WebSocket gateways, on NestJS 10, 11 and 12.

Checks Done Right

Verifies the hostname and action, retries a failed call once with an idempotency key, and keeps the secret key out of every error.

Protect a Route in Three Steps

Turnstile runs a challenge in the browser and hands out a token. Your server asks Cloudflare whether that token is valid. This package does the server half.

  1. 1

    Add the widget

    Turnstile runs in the browser and puts a token into your form.

    login.html
    <div class="cf-turnstile"
         data-sitekey="YOUR_SITE_KEY"></div>
  2. 2

    Register the module

    Import it once with the secret key from the Cloudflare dashboard.

    app.module.ts
    TurnstileModule.forRoot({
      secretKey: process.env.TURNSTILE_SECRET_KEY,
    })
  3. 3

    Protect the route

    Requests without a valid token get a 400 and never reach the handler.

    auth.controller.ts
    @Post('login')
    @TurnstileCaptcha()
    login() {}

Built for Production

Hooks for metrics, logs you can leave on, and checks that catch setup mistakes before your users do.

Works With Your Stack

One guard for every transport Nest supports, and examples for the frontend frameworks people use most.

Add Turnstile to Your NestJS App

Free and open source under the MIT License. The guide goes from install to your first protected route.