Skip to main content

How to fix: Client Script Hardcodes a Localhost API URL

A client-side script assigns an API base URL / endpoint variable to a literal http://localhost or 127.0.0.1 address.

Why it matters

In production, every API call built from this base URL fails outright (or, worse, silently targets whatever happens to be listening on the visitor's own machine at that port), and it signals the deploy pipeline never swapped in environment-specific configuration.

This is a common artifact of AI-scaffolded or quickly-prototyped apps: the assistant generates a working local dev config, and it ships unchanged because there was no environment-variable indirection for the API base URL.

How to fix it

  1. 1Read the API base URL from a build-time environment variable, never a literal string.
  2. 2Fail the production build if the environment variable resolves to localhost/127.0.0.1.
  3. 3Add an end-to-end smoke test against the deployed environment that would catch a broken API base URL.

Code examples

Use an environment variable instead

TypeScript
const apiUrl = process.env.NEXT_PUBLIC_API_URL;
if (!apiUrl) throw new Error('NEXT_PUBLIC_API_URL is not set');

References

Detected automatically by VulnRadar. The check id is cs-hardcoded-localhost-api-url, stable across every scan so you can filter or diff on it.

See every client-side check

Questions

How severe is Client Script Hardcodes a Localhost API URL?
VulnRadar rates this medium severity in the client-side category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
Does VulnRadar check for this automatically?
Yes. It runs as part of every scan under the check id cs-hardcoded-localhost-api-url, which stays the same between runs so you can filter, diff, or gate CI on it.

Missing a check?

The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.

Propose a checkHow a check is built

Scan your own site for client script hardcodes a localhost api url

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

Client Script Hardcodes a Localhost API URL fix | VulnRadar