# Integrate using Akamai EdgeWorkers

This guide describes how to use Akamai EdgeWorkers to integrate Agent Intelligence into your web app. For more information about Akamai EdgeWorkers, see [Akamai's documentation](https://techdocs.akamai.com/edgeworkers/docs).

## Prerequisites

- A Mosaic application with client credentials. Create an application in the [Admin Portal](https://portal.transmitsecurity.io/) if you do not already have one.
- Your web application must already be integrated with Akamai.
- You should be able to create and activate new Akamai EdgeWorkers.


## Step 1: Create new EdgeWorker ID

Start by setting up a new EdgeWorker that will execute the code that initializes the Platform SDK, collects browser and action signals, and feeds them to Mosaic.

1. Log in to your Akamai dashboard and navigate to EdgeWorkers.
2. Select **Create EdgeWorker ID** and provide details such as name, group, and resource tier.


## Step 2: Create and activate bundle

An EdgeWorker that you just created is still inactive and doesn't contain any code to execute. To enable the worker, create a new version for it, provide the code bundle, and then activate the version for your application environment. The bundle consists of `bundle.json` and `main.js`.

1. Go to the EdgeWorker ID details and select **Create version**.
2. Provide a version number and description in the `bundle.json`.
3. Add the sample code below to `main.js`. Replace `[CLIENT_ID]` with the client ID from the [Admin Portal](https://portal.transmitsecurity.io/) and `[ORIGIN_SERVER]` with your application domain so the EdgeWorker can fetch your origin response.
4. Select **Activate version** to enable the EdgeWorker.


Note
Set `agenticCollect: true` in the `drs` 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.

```js
import { ReadableStream, WritableStream } from 'streams';
import { httpRequest } from 'http-request';
import { createResponse } from 'create-response';
import { TextEncoderStream, TextDecoderStream } from 'text-encode-transform';

// Some headers aren't safe to forward from the origin response through an EdgeWorker on to the client.
// For more information, see the tech docs on create-response:
// https://techdocs.akamai.com/edgeworkers/docs/create-response
const UNSAFE_RESPONSE_HEADERS = ['content-length', 'transfer-encoding', 'connection', 'vary',
  'accept-encoding', 'content-encoding', 'keep-alive',
  'proxy-authenticate', 'proxy-authorization', 'te', 'trailers', 'upgrade'];

class HTMLStream {
  constructor () {
    let readController = null;
    const agentIntelligenceHandlerStr = `
          <script src="https://platform-websdk.transmitsecurity.io/platform-websdk/2.x/ts-platform-websdk.js" defer="true" id="ts-platform-script"></script>
          <script>
             document.getElementById("ts-platform-script").addEventListener("load", function() {
                 window.tsPlatform.initialize({
                    clientId: "[CLIENT_ID]",
                    drs: {
                      agenticCollect: true, // Enables Agent Intelligence detection
                      serverPath: "https://api.transmitsecurity.io/risk-collect/", // Set serverPath based on your region or custom domain
                    }
                 });
             });
          </script>
          </head>
    `;
    const headTag = '</head>';
    this.readable = new ReadableStream({
      start (controller) {
        readController = controller;
      }
    });

    async function handleTemplate (text) {
      const startIndex = text.indexOf(headTag);
      if (startIndex !== -1) {
        text = text.replace(headTag, agentIntelligenceHandlerStr);
      }
      readController.enqueue(text);
    }

    let completeProcessing = Promise.resolve();
    this.writable = new WritableStream({
      write (text) {
        completeProcessing = handleTemplate(text, 0);
      },
      close () {
        completeProcessing.then(() => readController.close());
      }
    });
  }
}

export function responseProvider () {
  return httpRequest("[ORIGIN_SERVER]").then(response => {
    return createResponse(
      response.status,
      getSafeResponseHeaders(response.getHeaders()),
      response.body
        .pipeThrough(new TextDecoderStream())
        .pipeThrough(new HTMLStream())
        .pipeThrough(new TextEncoderStream())
    );
  });
}

function getSafeResponseHeaders(headers) {
  for (let unsafeResponseHeader of UNSAFE_RESPONSE_HEADERS) {
    if (unsafeResponseHeader in headers) {
      delete headers[unsafeResponseHeader];
    }
  }
  return headers;
}
```

## Step 3: Validate in Mosaic

After you activate the EdgeWorker and drive traffic through the pages covered by your 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 EdgeWorker is active on the routes where you expect HTML responses
- 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](/guides/quick_start/enable_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.