AWS API Gateway
Error Messages
Request Validator
Detailed Errors
API Development

Get detailed error messages from AWS API Gateway Request Validator

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

AWS API Gateway request validation is useful because it rejects malformed requests before they hit your backend. The frustrating part is that the default 400 Bad Request response is often too generic, so clients and developers cannot immediately see which field or parameter failed validation.

What the Built-In Validator Actually Does

In a REST API, API Gateway can validate:

  • required query string parameters
  • required headers
  • path parameters
  • request bodies against a model

When validation fails, API Gateway stops the request before the integration runs. That behavior is good for protection and cost control, but it also means your Lambda or HTTP backend never gets a chance to build a nicer error response.

The fix is to customize the gateway response that API Gateway sends for validation failures.

Expose Validation Details with Gateway Responses

For REST APIs, the response types you usually care about are BAD_REQUEST_BODY and BAD_REQUEST_PARAMETERS. You can override them and include context variables such as messageString, validationErrorString, and requestId.

Here is an OpenAPI example using the API Gateway extension block:

yaml
1x-amazon-apigateway-gateway-responses:
2  BAD_REQUEST_BODY:
3    statusCode: 400
4    responseTemplates:
5      application/json: |
6        {
7          "message": $context.error.messageString,
8          "details": "$context.error.validationErrorString",
9          "requestId": "$context.requestId"
10        }
11  BAD_REQUEST_PARAMETERS:
12    statusCode: 400
13    responseTemplates:
14      application/json: |
15        {
16          "message": $context.error.messageString,
17          "details": "$context.error.validationErrorString",
18          "requestId": "$context.requestId"
19        }

After deployment, a request that fails schema validation can return a payload closer to:

json
1{
2  "message": "Invalid request body",
3  "details": "Invalid model schema specified: Validation Result: warnings : [], errors : [object has missing required properties ([email])]",
4  "requestId": "abc123"
5}

That is much easier to debug than a plain generic 400.

Add Logging for Faster Diagnosis

Even when you customize the client-facing payload, it is useful to include validation details in access logs. That lets you correlate the failing request with a request ID and inspect patterns in CloudWatch.

json
1{
2  "requestId":"$context.requestId",
3  "status":"$context.status",
4  "errorMessage":"$context.error.messageString",
5  "validation":"$context.error.validationErrorString"
6}

This log format belongs in the access log settings for the stage. It gives operators enough information to distinguish between missing parameters, invalid body shapes, and unrelated 400 responses.

When Built-In Validation Is Not Enough

The built-in validator is best for coarse request shape validation. If you need domain-specific feedback such as "end date must be after start date" or "username already exists", do that in your application code instead.

A Lambda handler can still perform additional validation and return a structured error body:

javascript
1export const handler = async (event) => {
2  const body = JSON.parse(event.body || "{}");
3
4  if (!body.email) {
5    return {
6      statusCode: 400,
7      body: JSON.stringify({
8        code: "EMAIL_REQUIRED",
9        message: "The email field is required."
10      })
11    };
12  }
13
14  return {
15    statusCode: 200,
16    body: JSON.stringify({ ok: true })
17  };
18};

Use API Gateway validation to reject obviously malformed input early, then use backend validation for rules that depend on business semantics.

One more practical note: this article is about REST APIs. HTTP APIs in API Gateway have a different feature set, so do not assume the same request validator and gateway response behavior applies unchanged.

Common Pitfalls

  • Expecting the backend to format validation errors from built-in request validation. The integration is never invoked on those failures.
  • Customizing only BAD_REQUEST_BODY and forgetting BAD_REQUEST_PARAMETERS. Missing query parameters then keep returning generic errors.
  • Returning too much internal detail. Validation messages should help clients, but they should not leak sensitive implementation details.
  • Mixing REST API guidance with HTTP API behavior. Similar names in the console do not guarantee identical features.

Summary

  • API Gateway request validation can reject bad requests before your backend runs.
  • Default validation errors are generic, but REST APIs let you customize gateway responses.
  • Use BAD_REQUEST_BODY and BAD_REQUEST_PARAMETERS to expose clearer details.
  • Include validation context in access logs so operators can debug failures quickly.
  • Keep business-rule validation in your backend even when request validation is enabled.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.