Q32: How do you disable the default body parser in Pages Router API routes?

Export a config object from the API route file with api: { bodyParser: false }. Disabling body parsing is necessary when processing raw file streams, file uploads (e.g. using multer or formidable), or verifying raw webhooks (like Stripe).

Disabling Body Parser for Webhooks
// pages/api/webhook.js
export const config = {
  api: {
    bodyParser: false, // Disables default JSON body parsing
  },
};

export default async function handler(req, res) {
  // Process raw stream (e.g., Stripe signature verification)
}
💡 Disabling bodyParser grants direct access to the unparsed HTTP request stream.
Key Takeaways
  • Export export const config = { api: { bodyParser: false } }.
  • Required for raw request body stream processing.
Knowledge Check

Why must you set bodyParser: false when receiving raw Stripe webhook events in an API route?