Turing API

Laravel bot detection and email validation

Route middleware that blocks datacenter traffic before it reaches your controllers, and a validation rule that rejects disposable signup addresses.

1. Install

composer require guzzlehttp/guzzle

2. Add the file

app/Http/Middleware/BlockBots.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class BlockBots
{
    public function handle(Request $request, Closure $next)
    {
        try {
            $response = Http::withToken(config('services.turing.key'))
                ->timeout(3)
                ->post('https://uat.turing-api.com/v1/bot-check', [
                    'ip' => $request->ip(),
                    'user_agent' => $request->userAgent() ?? '',
                ]);
        } catch (\Throwable $e) {
            // Fail open: an outage on our side must never take your site down.
            Log::warning('turing unreachable', ['error' => $e->getMessage()]);
            return $next($request);
        }

        if (! $response->successful()) {
            return $next($request);
        }

        if ($response->json('is_bot') === true) {
            return response()->json([
                'error' => 'Automated traffic is not permitted.',
                'reasons' => $response->json('reasons') ?? [],
            ], 403);
        }

        return $next($request);
    }
}

3. Wire it up

// bootstrap/app.php  (Laravel 11+)
->withMiddleware(function (Middleware $middleware) {
    $middleware->alias(['bot' => \App\Http\Middleware\BlockBots::class]);
})

// routes/web.php
Route::post('/signup', SignupController::class)->middleware('bot');

// config/services.php
'turing' => ['key' => env('TURING_API_KEY')],

4. Verify

Confirm the API answers before you debug your Laravel wiring. The sample address sits inside a published AWS range, so a correct setup returns is_bot: true.

Notes

Other frameworks