Skip to content

Integrate using Cloudflare Workers

This guide describes how to use Cloudflare Workers to integrate Agent Intelligence into your web app. For more information about Cloudflare Workers, see Cloudflare's documentation.

Prerequisites

  • A Mosaic application with client credentials. Create an application in the Admin Portal if you do not already have one.
  • Your web application must already be integrated with Cloudflare Workers.

Step 1: Load SDK via Cloudflare Worker

Load and initialize the Platform SDK using Cloudflare Workers:

  1. Log in to your Cloudflare dashboard and go to Workers.
  2. Create or update an existing Worker, and make sure the Worker's routes are defined for all relevant application pages. See Routes for information about adding routes to Cloudflare Workers.
  3. On the Worker page, click Quick edit and add the code below to the JavaScript code pane on the left. Replace <CLIENT_ID> with the client ID of your app (can be found in Admin Portal) and <SERVER_PATH> with the endpoint for your region or custom domain.
Note

Set agenticCollect: true in the SDK configuration to enable Agent Intelligence detection.

If you already use Mosaic Fraud Prevention, make sure the clientId and the region in serverPath match the ones configured for your existing integration.

const AGENTIC_CLIENT_ID = '<CLIENT_ID>'; // Client ID of your app
const AGENTIC_SERVER_PATH = '<SERVER_PATH>'; // e.g. Set to https://api.transmitsecurity.io/risk-collect/ or other depending on your region or custom domain
const PLATFORM_SDK_URL = 'https://platform-websdk.transmitsecurity.io/platform-websdk/2.x/ts-platform-websdk.js';

const AGENTIC_SNIPPET = `
<script src="${PLATFORM_SDK_URL}" id="ts-agentic-loader" data-ts-agentic-self></script>
<script data-ts-agentic-self>
  (function () {
    if (window.__agenticSdkInit) return;
    window.__agenticSdkInit = true;
    var s = document.getElementById('ts-agentic-loader');
    if (!s) return;
    function go() {
      if (window.__agenticSdkStarted) return;
      if (!window.tsPlatform || typeof window.tsPlatform.initialize !== 'function') return;
      window.__agenticSdkStarted = true;
      try {
        window.tsPlatform.initialize({
          clientId: '${AGENTIC_CLIENT_ID}',
          drs: {
            serverPath: '${AGENTIC_SERVER_PATH}',
            agenticCollect: true
          }
        });
      } catch (e) { /* never break the page */ }
    }
    if (window.tsPlatform && window.tsPlatform.initialize) go();
    else s.addEventListener('load', go);
  })();
</script>
`;

export default {
  async fetch(request) {
    const response = await fetch(request);

    const contentType = response.headers.get('content-type') || '';
    if (!contentType.includes('text/html')) {
      return response; // pass through anything non HTML
    }

    return new HTMLRewriter().on('body', new AgenticInjector()).transform(response);
  },
};

class AgenticInjector {
  element(element) {
    element.onEndTag((endTag) => {
      endTag.before(AGENTIC_SNIPPET, { html: true });
    });
  }
}

Step 2: Validate in Mosaic

After you deploy the Worker and drive traffic through the covered routes, open Agent Intelligence in Mosaic and start with Sessions.

  • Confirm that session records begin appearing for the applications and routes covered by your integration.
  • Review fields such as Activity Surface, Start Time, Agent Platform, and Origin Type to verify that the expected traffic is being collected.
  • Open individual sessions to inspect additional details such as IP Address, Country, OS, Browser, and path data when available.

If data doesn't appear, verify the following:

  • The Worker routes include the relevant HTML pages
  • The Platform SDK script is being injected into the page source
  • serverPath matches the correct region or your custom domain
  • Your CSP allows the Platform SDK script and risk-collect endpoint. See Enable API communication
Note

First agent platforms typically surface within hours of activation. Agentic traffic volume builds gradually over the first several weeks—check trends across multiple weeks rather than a single day.