# Hardening an API

An API online gets visitors you didn't invite. Decide which web pages may call it (CORS), add security headers and rate limits, keep secrets out of your code and your repository, and close the holes attackers try first.

Updated: November 19, 2026

## [Browsers and Traffic](https://www.ask-codey.com/courses/nodejs-in-depth/topics/node-security-browsers-and-traffic)

1. [CORS: Which Web Pages May Call Your API](https://www.ask-codey.com/lessons/node-security-cors) (Journeyman): A browser lets a web page read answers only from its own origin unless the other server allows it, and the cors package lets an Express API name the pages it allows, which protects people's browsers but never stops scripts and tools.
2. [Security Headers and Rate Limits](https://www.ask-codey.com/lessons/node-security-headers-and-rate-limits) (Journeyman): helmet adds a set of security headers to every answer of an Express API in one line, and express-rate-limit counts each address's requests to a route such as sign-in and answers 429 Too Many Requests when it goes over the limit.

## [Secrets and Attacks](https://www.ask-codey.com/courses/nodejs-in-depth/topics/node-security-secrets-and-attacks)

1. [Keeping Secrets Out of Your Code](https://www.ask-codey.com/lessons/node-security-secrets) (Journeyman): An API's secrets, such as the key that signs its tokens and its database password, live in environment variables loaded from a .env file that Git never sees, with a committed .env.example naming them, a check at startup that each one is set, and a new secret at once if one ever reaches GitHub.
2. [The Attacks to Expect: Injection and Mass Assignment](https://www.ask-codey.com/lessons/node-security-common-attacks) (Journeyman): The first things attackers try against an API are a MongoDB operator where text belongs, a body that sets fields it shouldn't, and errors that give details away, so an API checks that fields are text, copies only an allow-list of fields, answers errors in plain words and runs npm audit.

## [Exercise: Harden the Coffee Club API](https://www.ask-codey.com/courses/nodejs-in-depth/topics/exercise-node-harden-the-coffee-club-api)

1. [Exercise: Harden the Coffee Club API](https://www.ask-codey.com/lessons/exercise-node-harden-the-coffee-club-api) (Journeyman): Harden Bean There Coffee Club's working Express API before it goes online, with helmet's headers, CORS for the club's own pages, a rate limit on sign-in, an allow-list of fields and a check that refuses operator objects, and let Supertest's tests check every job.

## [Up Next: Real-Time Apps](https://www.ask-codey.com/courses/nodejs-in-depth/topics/node-up-next-real-time-apps)

1. [Up Next: Real-Time Apps](https://www.ask-codey.com/lessons/node-up-next-real-time-apps) (Journeyman): See what your APIs can do after Module 4, from hashed passwords and tokens to rules about who may do what and an API hardened for the open internet, meet the Club Events API project where you'll show it, and get a first look at Module 5, where the server speaks first.

Source: https://www.ask-codey.com/courses/nodejs-in-depth/chapters/node-hardening-an-api
