@steadphast/connector-nextjs
The Next.js connector for Steadphast. It runs in your Next.js Middleware
(Next.js 15) or Proxy (Next.js 16), captures request metadata before the request reaches your
application, and sends a bounded, sanitized record of each request to Steadphast in the background.
Steadphast classifies the traffic server-side: the connector never reads a request body, never runs
any classification, and never changes the response your visitor receives.
The package is one connector with two modes: Next.js on Vercel (kind: "vercel") and
Next.js self-hosted (kind: "self-hosted").
The optional App Router DiscoverCollector component has independent T0 and Family C controls.
Family C requires API enablement, workspace policy and an exact nonsensitive route. Own mode
is not visitor permission: affirmative consent is the default. A no-prompt notice basis
applies only where Steadphast has recorded it for the workspace and behavioral.consentBasis
is also "notice". Timing defaults false and needs both the site and the workspace switch.
See Family C own collection.
Visitor / bot Your Next.js deployment Steadphast
───────────── ─────────────────────── ──────────
│ request │ │
├───────────────────────────────►│ middleware.ts / proxy.ts │
│ │ 1. skip static assets │
│ │ 2. read method, URL, headers only │
│ │ 3. drop recognized credential headers │
│ │ + your excludedHeaders │
│ │ 4. build one bounded record │
│ │ 5. waitUntil(POST record) ───────────►│ classify
│ │ 6. run your handler, then the app │ + store
│◄───────────────────────────────┤ response (unchanged by the connector) │
│ │ │
Event appears in Explore ◄┘
Licensed under Apache-2.0. The LICENSE and NOTICE files shipped with the package are the
authoritative terms.
Requirements
- Next.js 15 or 16 (
^15.0.0 || ^16.0.0) and React 19 (^19.0.0) are peer dependencies.
The exact-runtime fixtures pin Next.js 15.5.28 (middleware.ts) and 16.3.2 (proxy.ts);
other releases on those two lines are expected to work. Next.js 14 and earlier are unsupported.
- A Steadphast API key, created in the dashboard under Settings → API keys with Used with
set to the mode you deploy: Next.js on Vercel or Next.js self-hosted. A key is issued for
one deployment mode and is rejected by the other.
Install
npm install @steadphast/connector-nextjs
Or, with pnpm:
pnpm add @steadphast/connector-nextjs
The package has no runtime dependencies; install no other Steadphast package. On Next.js 14 or
earlier, npm install stops with ERESOLVE and pnpm add warns about an unmet peer: upgrade
Next.js rather than forcing the install.
Let a coding agent do the setup
If a coding agent (Claude Code, Codex, Cursor, or similar) has access to your application's
repository, paste the prompt for your deployment mode into it. The prompt sends the agent to this
README, has it install the package, write the entry file, propose the routes to exclude from
capture, and run the build, and it stops before anything that needs your account: it never creates,
asks for, or writes an API key, and it does not deploy. Review the routes it excluded before you
deploy. In the self-hosted prompt, replace https://www.example.com and www.example.com with
your public origin and host first.
Prompt: Next.js on Vercel
Integrate the Steadphast Next.js connector into this application. The site is deployed on Vercel.
The integration guide is the package README. Follow it exactly and do not invent configuration options it does not document.
Steps:
1. Before installing anything, check the Next.js version in package.json. The connector supports Next.js 15 (entry file middleware.ts) and Next.js 16 (entry file proxy.ts). If the app is on an older version, stop and tell me; do not upgrade Next.js.
2. Install @steadphast/connector-nextjs with the package manager this repo already uses, then read node_modules/@steadphast/connector-nextjs/README.md in full before changing anything. If your package manager does not create node_modules, print the README with: npm view @steadphast/connector-nextjs readme
3. Create the entry file for our Next.js version beside the app or pages directory (or under src/ if the app lives there). Default-export withDiscover with kind: "vercel", apiUrl: "https://api.steadphast.ai", excludedHeaders: ["cookie"], and a recordIdPrefix that names this deployment. Export a config with a matcher that excludes _next/static, _next/image, favicon.ico, and every route that handles form submissions, authentication, payments, or other sensitive actions in this app. List the routes you excluded and why so I can review them.
4. Run the production build and fix any errors. Do not start a dev server.
5. Do not deploy. Do not create, request, or write an API key anywhere; never put a key in a file or in a NEXT_PUBLIC_ variable. I will generate the key in Steadphast under Settings, API keys, with "Used with" set to "Next.js on Vercel", and set STEADPHAST_API_KEY in the Vercel project's environment myself.
Finish with a short handoff that tells me: the files you changed, the routes excluded from capture, the exact remaining manual steps (set the variable, redeploy), and how to verify: request the production domain with an AI crawler user agent, for example curl -A GPTBot, then check Explore in the Steadphast dashboard, which shows AI traffic by default.
Prompt: Next.js self-hosted
Integrate the Steadphast Next.js connector into this application in self-hosted mode. The site runs with next start on our own server behind nginx, and visitors reach it at https://www.example.com.
The integration guide is the package README. Follow it exactly and do not invent configuration options it does not document.
Steps:
1. Before installing anything, check the Next.js version in package.json. The connector supports Next.js 15 (entry file middleware.ts, which must also export runtime: "nodejs" in its config) and Next.js 16 (entry file proxy.ts, which takes no runtime option). If the app is on an older version, stop and tell me; do not upgrade Next.js.
2. Install @steadphast/connector-nextjs with the package manager this repo already uses, then read node_modules/@steadphast/connector-nextjs/README.md in full before changing anything. If your package manager does not create node_modules, print the README with: npm view @steadphast/connector-nextjs readme
3. Create the entry file for our Next.js version beside the app or pages directory (or under src/ if the app lives there). Default-export withDiscover with kind: "self-hosted", publicOrigin: "https://www.example.com", apiUrl: "https://api.steadphast.ai", excludedHeaders: ["cookie"], and a recordIdPrefix that names this deployment. Export a config with a matcher that excludes _next/static, _next/image, favicon.ico, and every route that handles form submissions, authentication, payments, or other sensitive actions in this app. List the routes you excluded and why so I can review them.
4. Run the production build and fix any errors. Do not start a dev server. On Next.js 15 with runtime: "nodejs", the build output shows no Middleware line and .next/server/middleware-manifest.json stays empty; that is expected. The middleware is registered in .next/server/functions-config-manifest.json instead. Keep runtime: "nodejs".
5. Copy the reference nginx configuration from the README's Next.js self-hosted section into deploy/nginx/nginx.conf and change only the values its table lists: the public host and port (www.example.com on 443, in every occurrence, keeping the [.] spelling inside the map regular expression), the certificate paths, and the proxy_pass listener (127.0.0.1:39515). Keep every other directive exactly as written; it is what sets X-Steadphast-Client-Ip from $remote_addr and X-Steadphast-Request-Id from $request_id on every proxied request and answers 421 to any other host. Do not run nginx, systemctl, certbot, or sudo, and do not connect to any server; I will apply this on the host.
6. Do not deploy. Do not create, request, or write an API key anywhere; never put a key in a file or in a NEXT_PUBLIC_ variable. I will generate the key in Steadphast under Settings, API keys, with "Used with" set to "Next.js self-hosted", and set STEADPHAST_API_KEY in the environment of the next start process myself.
Finish with a short handoff that tells me: the files you changed, the routes excluded from capture, the exact remaining manual steps on the host (install the nginx config, the key, restart), and how to verify: request the site with an AI crawler user agent, for example curl -A GPTBot, then check Explore in the Steadphast dashboard, which shows AI traffic by default.
Quick start: Next.js on Vercel
Next.js 15: middleware.ts
Place middleware.ts beside the root app or pages directory, or at src/middleware.ts when the
application lives under src/. Vercel mode runs on the Edge or Node.js runtime.
import { withDiscover } from "@steadphast/connector-nextjs";
export default withDiscover({
kind: "vercel",
apiUrl: "https://api.steadphast.ai",
excludedHeaders: ["cookie"],
recordIdPrefix: "vercel-production",
});
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};
Next.js 16: proxy.ts
Place proxy.ts beside the root app or pages directory, or at src/proxy.ts when the
application lives under src/. Proxy runs on Node.js.
import { withDiscover } from "@steadphast/connector-nextjs";
export default withDiscover({
kind: "vercel",
apiUrl: "https://api.steadphast.ai",
excludedHeaders: ["cookie"],
recordIdPrefix: "vercel-production",
});
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};
Set the API key
Add STEADPHAST_API_KEY to every Vercel environment where capture should be enabled. It is a
server-only secret: never give it a NEXT_PUBLIC_* name. When no key is set, the connector ships
nothing and the visitor response is unaffected; it logs one warning per running instance, on the
first matched request that is not a skipped static asset, that begins
Steadphast connector skipped event.
Look for it in the deployment's runtime logs. Adding the variable takes effect on the next
deployment, so redeploy after you set it.
Deploy, make a request to a matched path on your production domain (deployment-specific
*.vercel.app URLs are protected by Vercel Deployment Protection by default), and open Explore
in the Steadphast dashboard. The event normally appears within about 60 seconds. Explore opens on AI
traffic only, so a request from your own browser is hidden until you switch the traffic filter to
all traffic; a request sent with an AI crawler's user agent, such as curl -A GPTBot, appears in the
default view.
Keep your own Middleware or Proxy logic
withDiscover(config, handler?) composes one application handler. The wrapper schedules capture and
delivery, then calls your handler once and preserves its response, rewrite, redirect, undefined
result, or thrown error:
import { NextResponse } from "next/server";
import { withDiscover } from "@steadphast/connector-nextjs";
export default withDiscover(
{ kind: "vercel", apiUrl: "https://api.steadphast.ai", excludedHeaders: ["cookie"] },
(request) => {
if (request.nextUrl.pathname === "/old") {
return NextResponse.redirect(new URL("/new", request.url));
}
},
);
Keep the wrapper as the file's default export so Next.js calls it with both the request and the
NextFetchEvent; delivery is scheduled through event.waitUntil. Do not call the wrapper from a
route handler, and do not drop the event argument.
Next.js self-hosted
Self-hosted mode is for a Next.js application started with next start on a server, virtual
machine, or container you operate, with nginx terminating client TLS directly in front of it. It
is the same package configured with kind: "self-hosted". Use it in either entry file; for
Next.js 15 middleware.ts also add runtime: "nodejs" to the exported config (Next.js 16
proxy.ts is already Node-only and does not accept the runtime option). The full setup guide is in the
dashboard at https://app.steadphast.ai/docs/integrations/nextjs-self-hosted; the reference nginx
configuration it uses is reproduced below.
import { withDiscover } from "@steadphast/connector-nextjs";
export default withDiscover({
kind: "self-hosted",
publicOrigin: "https://www.example.com",
apiUrl: "https://api.steadphast.ai",
excludedHeaders: ["cookie"],
recordIdPrefix: "nextjs-self-hosted-production",
});
export const config = {
// Next.js 15 middleware.ts only; remove this line in Next.js 16 proxy.ts.
runtime: "nodejs",
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};
publicOrigin is the public scheme, host, and port your visitors use, and it is the only source
of the captured request URL's origin. It must be one HTTPS origin with no credentials, path, query,
or fragment, and it must match the public authority nginx serves exactly. It is separate from
apiUrl. The API key comes from the STEADPHAST_API_KEY environment variable of the next start
process, issued with Used with set to Next.js self-hosted.
The nginx trust boundary
In this mode the connector trusts exactly two request headers, X-Steadphast-Client-Ip and
X-Steadphast-Request-Id, and only because nginx overwrites both on every proxied request from
facts nginx itself established. The connector never falls back to X-Forwarded-For, X-Real-IP,
the public x-request-id, any x-vercel-* header, or the socket address. A Next.js listener that
is reachable without nginx, or a proxy that passes client headers through, lets any client forge
the IP recorded for its requests.
The supported topology is exactly one nginx directly in front of the application, and the
supported configuration is the reference nginx configuration
below. Start from that file and change only the values it lists (public host and port, certificate
paths, and the loopback proxy_pass address). It:
- terminates client TLS directly on TLS 1.2 or 1.3 and refuses a handshake without the configured
server name;
- accepts exactly one public host and port and answers
421 to every other Host or absolute-form
request target;
- overwrites
Host, X-Forwarded-Host, and X-Forwarded-Proto from validated routing and
transport facts;
- overwrites
X-Steadphast-Client-Ip with the direct TCP peer ($remote_addr) and
X-Steadphast-Request-Id with nginx's own $request_id, replacing every client-supplied copy;
- appends its peer to
X-Forwarded-For, which the connector keeps only as untrusted evidence.
Bind the application to loopback (for example next start -H 127.0.0.1 -p 3000) and confirm from
outside the host that the Next.js port is not reachable. A CDN, load balancer, or second proxy in
front of nginx, a public direct Next.js port, and pass-through internal headers are unsupported:
the connector cannot detect them, and a CDN in front makes the recorded client IP that CDN's
address. No TLS fingerprint is available in this mode.
Reference nginx configuration