Next.js AI agent detection: install and verify Marshy
Install Marshy in a Next.js App Router app, verify a complete journey, identify authenticated accounts, and troubleshoot missing verdicts.
Obaid Ahmed4 min read
Install Marshy AI agent detection in a Next.js App Router application by copying your project’s integration snippet into the root layout with next/script, preserving its browser-safe configuration. Verify a complete route-changing journey in Marshy before using the verdict in reporting or an experience change.
This tutorial covers loading, authenticated account association, and common missing-result checks. Keep management credentials and identity-signing secrets on the server.
1. Copy the project-specific snippet
Open your project in Marshy, then open Settings and copy its integration snippet. Retain its script URL, publishable key, endpoint, and any configuration attributes. Use separate projects for environments when you need to keep test traffic out of production reporting.
The publishable browser key belongs in the installed snippet. Management API credentials and identity-signing secrets belong on the server. Do not replace the supplied script URL with a guessed CDN address or install an unrelated package because its name sounds similar.
2. Load the integration in the root layout
For a Next.js App Router project, adapt the tag using next/script in your root layout. Next documents beforeInteractive for scripts that need to load before hydration, including bot detectors. It also forwards custom attributes to the resulting script element. See the Next.js Script reference.
This example deliberately uses configuration names rather than an invented SDK URL. Set each value from your actual Marshy snippet, and preserve any additional attributes it includes:
// app/layout.tsx — merge into your existing root layout
import Script from 'next/script';
import type { ReactNode } from 'react';
export default function RootLayout({ children }: { children: ReactNode }) {
return (
<html lang="en">
<head>
<Script
id="marshy-agent-detection"
src={process.env.NEXT_PUBLIC_MARSHY_SDK_URL}
strategy="beforeInteractive"
data-resemble-key={process.env.NEXT_PUBLIC_MARSHY_KEY}
data-resemble-endpoint={process.env.NEXT_PUBLIC_MARSHY_ENDPOINT}
/>
</head>
<body>{children}</body>
</html>
);
}
The underlying SDK retains the data-resemble-* attribute names. Those are technical identifiers; the integration you are setting up is Marshy. Public environment variables are visible in browser code, so place only browser-safe configuration in them. Rebuild after changing build-time public configuration.
If your application uses a consent manager, coordinate initialization with that application's approved collection behavior. Record which visits were eligible for instrumentation rather than silently treating uninstrumented sessions as human.
3. Verify a complete journey
Load a page that contains the root layout, inspect the script request, and check for a successful response and any Content Security Policy errors. Allow only the actual origins your supplied integration requires.
In Marshy settings, check the installation status. Then complete a harmless task and find its session in Sessions. Navigate between routes and repeat. Look for missing pages, duplicate initialization, or a session that ends before classification.
The absence of a verdict is a result to investigate. It does not establish a human visitor. Check short visits, blocked scripts, consent conditions, and configuration before changing detection policy.
4. Attribute authenticated users carefully
After authentication, identify the account using the integration's documented queue:
window.ResembleAgentDetection =
window.ResembleAgentDetection || [];
window.ResembleAgentDetection.push([
'identify',
accountId,
{ plan: accountPlan },
{ signature: serverGeneratedSignature },
]);
Generate the signature with your integration's identity-signing secret on the server, over the documented account identifier. Return it only for the authenticated account. Use the reset operation when the user signs out or switches accounts. Refer to the integration instructions for the exact signing contract.
A signed account association answers whose account is involved. It does not prove that a person is operating the browser, or grant the session permissions it did not already have.
5. Add reporting before restrictions
Keep human, agent, review, and missing-verdict sessions visible. Link classification to server-confirmed outcomes so a click is not mistaken for a completed operation. The next tutorial shows how to send Marshy verdicts to PostHog.
Before launch, test a keyboard journey, an authenticated account switch, a short visit, and a blocked-script case. Use the evaluation worksheet to record what happened. Create your Marshy integration and start with a session you can trace end to end.
Checked October 8, 2026 against the local Marshy settings interface and Next.js documentation. The code is an adaptation pattern; supply your integration's real configuration.