Protected by MagenTrust
    MAGEN
    MAGEN Docs
    Integration Guide

    Edge Gateway Integration

    Deploy MAGEN trust verification at the edge for sub-millisecond latency protection across all your traffic.

    Architecture Overview

    ┌─────────────────────────────────────────────────────────────┐
    │                        USER REQUEST                          │
    └─────────────────────────────────────────────────────────────┘
                                  │
                                  ▼
    ┌─────────────────────────────────────────────────────────────┐
    │                      EDGE GATEWAY                            │
    │  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐       │
    │  │  Cloudflare  │  │    AWS       │  │   Vercel     │       │
    │  │   Workers    │  │  Lambda@Edge │  │  Middleware  │       │
    │  └──────┬───────┘  └──────┬───────┘  └──────┬───────┘       │
    │         └─────────────────┼─────────────────┘               │
    │                           ▼                                  │
    │              ┌────────────────────────┐                      │
    │              │   MagenTrust Layer    │                      │
    │              │   /v1/verify endpoint  │                      │
    │              └────────────┬───────────┘                      │
    │                           │                                  │
    │         ┌─────────────────┼─────────────────┐               │
    │         ▼                 ▼                 ▼               │
    │    ┌─────────┐      ┌──────────┐      ┌──────────┐         │
    │    │  ALLOW  │      │  REVIEW  │      │  BLOCK   │         │
    │    │ (Human) │      │(Challenge)│     │  (Bot)   │         │
    │    └────┬────┘      └─────┬────┘      └────┬─────┘         │
    └─────────┼─────────────────┼────────────────┼────────────────┘
              │                 │                │
              ▼                 ▼                ▼
        ┌──────────┐      ┌──────────┐     ┌──────────┐
        │  Origin  │      │ Challenge│     │  Block   │
        │  Server  │      │   Page   │     │ Response │
        └──────────┘      └──────────┘     └──────────┘

    Deployment Patterns

    Signal Provider

    Feed MAGEN scores into existing WAFs (Cloudflare, Akamai, AWS WAF) as custom signals for rule evaluation.

    Pre-Firewall

    Intercept and verify requests before they reach your firewall. Block bots at the network edge.

    AI Firewall

    Protect against autonomous agents and synthetic actors with continuous behavioral analysis.

    Platform Integration

    Deploy as a Cloudflare Worker to verify requests at 300+ edge locations globally.

    // Cloudflare Worker - MagenTrust Verification
    export default {
      async fetch(request, env, ctx) {
        const url = new URL(request.url);
        
        // Skip for static assets
        if (url.pathname.startsWith('/static/')) {
          return fetch(request);
        }
    
        // Extract session token from cookie or header
        const sessionToken = request.headers.get('x-magen-session') 
          || getCookie(request, 'magen_session');
    
        if (!sessionToken) {
          // No session - allow but flag for client-side init
          return addMagenHeaders(await fetch(request), { status: 'no_session' });
        }
    
        // Verify with MAGEN API
        const verification = await verifySession(sessionToken, env);
    
        switch (verification.verdict) {
          case 'verified':
            // Human verified - proceed normally
            return addMagenHeaders(await fetch(request), verification);
    
          case 'review':
            // Suspicious - add challenge header for client
            const response = await fetch(request);
            return addMagenHeaders(response, verification, {
              'x-magen-challenge': 'required'
            });
    
          case 'unverified':
            // Bot detected - block or redirect
            return new Response(JSON.stringify({
              error: 'Access denied',
              reason: 'automated_behavior_detected'
            }), {
              status: 403,
              headers: { 'Content-Type': 'application/json' }
            });
        }
      }
    };
    
    async function verifySession(token, env) {
      const response = await fetch('https://api.magentrust.ai/v1/verify', {
        method: 'POST',
        headers: {
          'x-magen-key': env.MAGEN_API_KEY,
          'x-magen-site': env.MAGEN_SITE_ID,
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({ session_token: token })
      });
      return response.json();
    }
    
    function addMagenHeaders(response, verification, extra = {}) {
      const headers = new Headers(response.headers);
      headers.set('x-magen-verified', verification.verdict || 'unknown');
      headers.set('x-magen-score', String(verification.score || 0));
      Object.entries(extra).forEach(([k, v]) => headers.set(k, v));
      return new Response(response.body, { ...response, headers });
    }
    
    function getCookie(request, name) {
      const cookies = request.headers.get('Cookie') || '';
      const match = cookies.match(new RegExp(`${name}=([^;]+)`));
      return match ? match[1] : null;
    }

    Configuration (wrangler.toml)

    # wrangler.toml - Cloudflare Worker Configuration
    name = "magen-trust-gateway"
    main = "src/worker.js"
    compatibility_date = "2024-01-01"
    
    [vars]
    MAGEN_SITE_ID = "site_abc123"
    
    # Protected routes
    [[routes]]
    pattern = "example.com/api/*"
    zone_name = "example.com"
    
    [[routes]]
    pattern = "example.com/checkout/*"
    zone_name = "example.com"
    
    # Rate limiting rule (optional - MAGEN handles this too)
    [[rules]]
    action = "block"
    expression = "(cf.threat_score > 30)"
    description = "Block high threat scores"

    Response Headers

    HeaderValuesDescription
    x-magen-verifiedverified | review | unverifiedTrust verdict from MAGEN
    x-magen-score0.0 - 1.0Confidence score (higher = more human)
    x-magen-is-humantrue | falseBinary human classification
    x-magen-challengerequired | optionalChallenge recommendation
    x-magen-sessionsession_idSession identifier for tracking

    Best Practices

    • Fail open: If MAGEN API is unreachable, allow requests but log for investigation
    • Cache verdicts: Cache verification results for 30-60 seconds to reduce API calls
    • Progressive enforcement: Start with logging only, then enable blocking after tuning
    • Route-specific rules: Apply stricter verification for /checkout, /api, /admin paths
    • Monitor latency: Edge verification should add <50ms; alert if higher
    Edge Gateway Integration v1.0 | Last updated Feb 2026