API Reference

Helmet

Express middleware that sets security-related HTTP headers such as CSP, HSTS, frame protection, and referrer policy with configurable defaults.

Official Documentation ↗
CompatibilityThis documentation covers version Helmet 7.x/8.x; header defaults change between major versions and should be reviewed on upgrade. Check the official docs for the latest API surface.

Installation

npmnpm install helmet
pnpmpnpm add helmet
yarnyarn add helmet

Methods

Helmet sets security-related response headers for Express. It is a collection of small middlewares, not an application firewall. Review each header, test it against the real frontend, and keep the policy as strict as the product allows.

Content Security Policy

CSP is the highest-value header and the easiest to break. Start in report-only mode, collect violations, then enforce. Prefer nonces or hashes for scripts, restrict frame-src to known embed providers, and avoid unsafe-inline for scripts.

Headers need context

HSTS requires HTTPS and a plan for every subdomain. frameguard and CSP frame-ancestors protect against clickjacking but can break legitimate embedding. Referrer policy and cross-origin policies affect third-party resources. Test in staging with the actual page, not only an API health check.

Security notes

Quoted from the official documentation (https://helmetjs.github.io/); this is not a complete threat model.

FAQ

Does Helmet make an application secure?
No. It sets useful response headers. Authentication, authorization, input validation, dependency updates, and secure cookies remain application responsibilities.
Why did enabling Helmet break my inline scripts?
The default Content-Security-Policy blocks inline scripts. Use nonces or hashes, move scripts to files, and scope the policy instead of adding unsafe-inline broadly.
Should I enable HSTS preload immediately?
No. Preload is difficult to reverse and requires every subdomain to be HTTPS-only. Start with a short max-age, verify, then increase and consider preload deliberately.