Next.js Security Monitoring With SecureNow: Setup Guide
Add SecureNow security monitoring to a Next.js app through the instrumentation hook: CLI login, runtime config, Vercel deployment, body capture and verification.

Next.js Security Monitoring With SecureNow: Setup Guide
Next.js makes building full-stack web applications easy. But once your app is live — handling user logins, processing payments, serving API routes — you need to know what is actually happening inside it. Not just how fast a page renders, but who is requesting it, whether those requests look legitimate, and what they are trying to do.
SecureNow plugs directly into Next.js's built-in instrumentation hook to capture every server-side request as an OpenTelemetry trace. Those traces feed into a security-focused analysis engine that detects injection attacks, credential stuffing, API abuse, and traffic anomalies — all without a WAF, a proxy, or any changes to your route handlers.
This guide covers the full flow: installing the package, logging in through the CLI, creating an app on the free trial, enabling instrumentation, and confirming everything works.
Prerequisites
- Node.js 22.23.4+, 24.22.1+, or 26.12.1+
- A Next.js 15 or 16 project (App Router or Pages Router); Next.js 14 works from securenow 8.25.0 (see the note under next.config)
- A terminal and a browser
No account yet? The CLI handles signup for you.
Step 1: Install SecureNow
npm install securenow
The install itself writes no files: the Next.js wiring happens in Step 5 with npx securenow init.
Step 2: Log In via the CLI
Authenticate with SecureNow from the terminal:
npx securenow login
Your default browser opens the SecureNow login page at app.securenow.ai. Sign up or log in, then pick an application or create a new one. The CLI writes two gitignored files in your project: .securenow/runtime.json (the app key and runtime API key the SDK runs with) and .securenow/admin.json (the CLI's own authorization).
Headless / CI alternative: Generate a token from Dashboard → Settings → CLI Token, then:
npx securenow login --token YOUR_TOKEN
A token login only authorizes the CLI. Link the project to an app afterwards with npx securenow apps default <app-key> and npx securenow api-key create, which write .securenow/runtime.json.
Confirm you are authenticated:
npx securenow whoami
Step 3: Create an Application (Free Trial)
The browser step of npx securenow login already took care of this: the app you picked or created there is linked to this project, and its app key and runtime API key are saved in .securenow/runtime.json. There is nothing to copy into .env.local.
To create an app from the terminal instead:
npx securenow apps create my-nextjs-app
npx securenow apps default b9c8d7e6-f5a4-3210-9876-543210fedcba
npx securenow api-key create
apps default links the app to this project (it writes .securenow/runtime.json) and makes it the default app for CLI commands. api-key create saves the runtime API key that telemetry ingestion and firewall sync authenticate with.
Step 4: Check the Runtime Config
There is no .env.local to write: the app key, logging and body capture settings are not read from environment variables. The SDK reads them from .securenow/runtime.json, and every setting has a secure default, so the file works as login wrote it. The parts that matter here look like this:
{
"apiKey": "snk_live_...",
"app": { "key": "b9c8d7e6-f5a4-3210-9876-543210fedcba", "name": "my-nextjs-app" },
"config": {
"logging": { "enabled": true },
"capture": { "body": true }
}
}
| Setting | Purpose |
|---|---|
app.key | Your app's unique key. Written by login. |
apiKey | Runtime API key for telemetry ingestion and firewall sync. Secret: never commit it. |
config.logging.enabled | Forwards server-side console output as OTel logs alongside your traces (default true). |
config.capture.body | Attaches request bodies to trace spans, with automatic redaction of passwords, tokens, and card numbers (default true). |
The SDK reads only two environment variables: SECURENOW_CREDENTIALS, for hosts that cannot mount a file (see Vercel below), and SECURENOW_DISABLED=1, which turns it off (for example in tests). SECURENOW_APPID, SECURENOW_INSTANCE and the other old SECURENOW_* variables are ignored.
Step 5: Set Up Instrumentation
Pick one of these methods:
Option A: CLI Scaffold
npx securenow init
This creates instrumentation.ts (or .js if your project is JavaScript-only) and adds the next.config settings shown below, or creates next.config.mjs if you have none. If you already have a custom instrumentation file, it prints the exact block to merge instead of overwriting it.
Option B: Manual File
Create instrumentation.ts at the root of your project (same level as next.config.js):
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
try {
const securenowNext = await import('securenow/nextjs');
const registerSecureNow = securenowNext.registerSecureNow || securenowNext.default?.registerSecureNow;
registerSecureNow({ captureBody: true });
await import('securenow/nextjs-auto-capture');
} catch (error) {
console.error('[securenow] SecureNow did not start; the app is running without it:', error);
}
}
}
That is the entire file. Next.js calls the register() export automatically when the server starts. Keep the imports inside the nodejs block and without webpackIgnore comments: Next.js then leaves them out of the Edge build and includes SecureNow and its dependencies in output: 'standalone' builds.
next.config: Keep SecureNow External
Add this to your next.config.mjs (or merge it into your existing config):
const nextConfig = {
serverExternalPackages: ['securenow'],
outputFileTracingIncludes: {
'/*': ['./node_modules/securenow/**/*'],
},
};
export default nextConfig;
Next.js 15 and 16 enable the instrumentation hook by default, so this is the only config change. It works with next start and with output: 'standalone' (Docker).
On Next.js 14 (securenow 8.25.0+), the same settings go under experimental, with instrumentationHook: true and serverComponentsExternalPackages: ['securenow'] in place of serverExternalPackages; npx securenow init writes them for you. This is tested end to end on Next.js 14.2.35 with next start and standalone output.
Step 6: Run and Verify
Start the dev server:
npm run dev
Look for the confirmation line in your terminal output:
[securenow] OpenTelemetry started for Next.js -> https://ingest.securenow.ai/v1/traces
Navigate to a couple of pages in your browser to generate traces, then check from the terminal:
npx securenow status
Your app should show as protected. Browse traces directly:
npx securenow traces
Or open app.securenow.ai to explore the full dashboard — traces, logs, security issues, analytics, and more.
Bonus: Handy CLI Commands
| Command | What It Does |
|---|---|
securenow traces | List recent traces |
securenow traces analyze <traceId> | AI-powered trace breakdown |
securenow logs | List recent log entries |
securenow notifications | View detected security issues |
securenow analytics | Traffic and performance stats |
securenow ip <address> | Look up an IP address |
securenow blocklist add <ip> | Block a suspicious IP |
securenow alerts rules | Manage alert rules |
securenow forensics | Natural-language forensic queries |
Deploying to Production
Vercel
Vercel cannot mount a credentials file, so pass the same credentials as one secret environment variable:
- Run
npx securenow credentials runtime --env production --format env - Go to Project Settings → Environment Variables
- Add
SECURENOW_CREDENTIALSwith the printed value - Redeploy
SecureNow detects the Vercel runtime automatically and uses @vercel/otel for trace export. This path has not been tested end to end yet, and the firewall has not been verified on Vercel serverless functions.
Docker / Self-Hosted
FROM node:22.23.4-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# Mount the production credentials at /app/.securenow/credentials.json
# (generated by: npx securenow credentials runtime --env production)
ENV NODE_ENV=production
EXPOSE 3000
CMD ["npm", "start"]
With output: 'standalone', run node server.js instead and put .securenow/credentials.json next to server.js.
Or mount the production credentials file through docker-compose.yml:
services:
nextjs-app:
build: .
ports:
- "3000:3000"
volumes:
- ./.securenow/credentials.production.json:/app/.securenow/credentials.json:ro
PM2
// ecosystem.config.cjs
module.exports = {
apps: [{
name: 'my-nextjs-app',
script: 'node_modules/.bin/next',
args: 'start',
env: {
NODE_ENV: 'production',
}
}]
};
No SecureNow environment variables are needed: PM2 reads the app's .securenow/ folder, in fork or cluster mode. On the server, put the production credentials there as .securenow/credentials.json.
What Gets Monitored Automatically
With instrumentation enabled, SecureNow captures and analyzes:
- API Routes — every handler in
app/api/orpages/api/ - Server Components — data fetching, rendering, and streaming
- Server-Side Rendering —
getServerSideProps,generateMetadata, and dynamic renders - Middleware — middleware running in the Node.js runtime (the Edge runtime is not supported, so edge middleware is not instrumented)
- Database Queries — Prisma, Drizzle, pg, mysql2, mongoose, and more via auto-instrumentation
- External HTTP Calls — fetch, axios, and other outbound requests
- Authentication Events — login attempts, session checks, authorization failures
On the security side, SecureNow watches for:
- SQL injection and NoSQL injection patterns
- Cross-site scripting (XSS) payloads in parameters and bodies
- Credential stuffing and brute-force authentication attacks
- API abuse — scraping, enumeration, rate-limit evasion
- Anomalous traffic patterns flagged by AI analysis
- Unexpected outbound connections (supply-chain signals)
Advanced: Request Body Capture
Request bodies are captured at the HTTP layer by the instrumentation file above (registerSecureNow({ captureBody: true }) plus the securenow/nextjs-auto-capture import), so no middleware is needed. JSON, form and GraphQL bodies are attached to spans with sensitive fields like passwords, tokens, and credit card numbers redacted automatically; multipart uploads are recorded as metadata only. Add custom fields to redact in .securenow/runtime.json:
{
"config": {
"capture": { "sensitiveFields": ["internal_token", "session_key"] }
}
}
Set config.capture.body to false there if you want to turn body capture off.
Recap
| Step | Command / Action | Time |
|---|---|---|
| Install | npm install securenow | 15 s |
| Login | npx securenow login | 20 s |
| Create app | Pick or create it in the login browser step | 15 s |
| Configure | Nothing to edit: defaults live in .securenow/runtime.json | 0 s |
| Instrument | npx securenow init | 15 s |
| Verify | npm run dev → check for confirmation line | 15 s |
With npx securenow init, the whole process is under two minutes. Even the manual path takes less than five.
Next Steps
- Open the SecureNow dashboard to explore traces, logs, and detected issues
- Set up alert rules to get notified about security events
- Run
npx securenow forensicsto query your traffic in natural language - Read the full Next.js guide for body capture, custom logging, Vercel-specific options, and more
Your Next.js app is now observable, protected, and ready for production.
Frequently Asked Questions
Does SecureNow work with the Next.js App Router?
Yes. SecureNow uses the official Next.js instrumentation hook (instrumentation.ts) which is fully supported in both the App Router and Pages Router. Server Components and API routes are auto-instrumented. The Edge runtime is not supported, so middleware running on the edge is not instrumented.
Will this slow down my Next.js app?
No. OpenTelemetry instrumentation adds less than 1% overhead in most applications. SecureNow runs entirely server-side and does not inject any client-side JavaScript (unless you enable the optional free-trial banner).
Does SecureNow work on Vercel?
It is set up for it, but not yet tested end to end. On Vercel, SecureNow uses @vercel/otel for trace export; add one secret environment variable, SECURENOW_CREDENTIALS (printed by npx securenow credentials runtime --env production --format env), in the Vercel dashboard and deploy. The firewall has not been verified on Vercel serverless functions. next start and output: 'standalone' (Docker) are tested.
Does npm install set anything up automatically?
No. The postinstall step is intentionally quiet and writes no files. Run npx securenow init: it creates instrumentation.ts and adds serverExternalPackages and outputFileTracingIncludes to next.config. If init wrote your instrumentation file before securenow 8.24.0, run it again to replace that file.
Recommended reading
Block one IP, a CIDR range, or a single route and method in a Node.js app within seconds, with no code change: setup, scoping, CLI, MCP, verification and undo.
Oct 27Built your app with a coding agent? 11 warning signs it needs a security audit, each checkable in five minutes, and how to run the full audit yourself.
Oct 26Part 1 of the AI security audit series: set up an AI coding agent to audit your app locally, connect SecureNow, and run your first threat-model pass.
Jul 24