NanoLog Redact Go to NanoLog.dev
Home > Guides > Automatically Scrubbing PII from API Responses
Developer Security

Automatically Scrubbing PII from API Responses

Public-facing APIs are the primary gateway for applications to fetch data. Often, backend queries fetch whole user records from the database, and the entire row gets serialized into the JSON response payload. This frequently leaks sensitive database IDs, password hashes, and user contact details to the frontend browser client.

Writing API Data Sanitizers

To restrict sensitive data exposure, developers should write structured sanitization middleware:

  • Model-Level Exclusions: Configure ORM schemas (such as Prisma or Drizzle) to exclude sensitive fields (like password hashes or billing details) from query results by default.
  • JSON Payload Scrubbing: Implement recursive object scanners that traverse JSON response bodies, stripping key-value pairs that match blacklists of sensitive property names.
  • Boundary Gateways: Configure your API gateway to block responses that contain raw email, phone, or credit card formats.

Proactively sanitizing JSON endpoints ensures that even if a developer makes a coding error in a controller, the system gateway filters out sensitive data before it reaches the public internet.