Build a REST API Endpoint with Input Validation and Error Handling

Generate a production-ready REST API endpoint with robust input validation, error handling, and security best practices.

๐Ÿ“ The Prompt

Create a production-ready REST API endpoint for [RESOURCE_NAME] using [FRAMEWORK] (e.g., Express.js, FastAPI, Spring Boot) with comprehensive input validation and structured error handling. Endpoint Details: - HTTP Method: [HTTP_METHOD] - Route: /api/[VERSION]/[RESOURCE_PATH] - Purpose: [ENDPOINT_PURPOSE] Request body/query parameters to validate: [LIST_OF_FIELDS_WITH_TYPES] Please implement the following: 1. **Route Definition**: Set up the endpoint with proper HTTP method and path parameters. 2. **Input Validation Layer**: Implement validation for each field including: - Type checking and coercion - Required vs. optional fields - String length limits, regex patterns where appropriate - Numeric range validation - Custom business rule validation (e.g., [CUSTOM_RULE]) 3. **Validation Middleware/Decorator**: Use [VALIDATION_LIBRARY] (e.g., Joi, Zod, Pydantic, class-validator) to define a reusable schema. 4. **Error Response Format**: Return standardized error responses following this structure: - HTTP status code (400 for validation, 404 for not found, 500 for server errors) - Error code identifier - Human-readable message - Field-level error details array 5. **Controller Logic**: Implement the core business logic that [BUSINESS_LOGIC_DESCRIPTION], including database interaction placeholders. 6. **Response Format**: Return a consistent success response with status, data, and metadata (pagination if applicable). 7. **Security Considerations**: Add rate limiting headers, input sanitization against [SECURITY_CONCERN] (e.g., SQL injection, XSS), and authentication middleware placeholder. Include inline comments explaining design decisions and add unit test examples for at least 3 validation scenarios (valid input, missing required field, invalid format).

๐Ÿ’ก Tips for Better Results

Specify your exact field names and types in [LIST_OF_FIELDS_WITH_TYPES] for tailored validation rules. Always mention your preferred validation library to get idiomatic code. Include a real business rule example in [CUSTOM_RULE] to get meaningful custom validators beyond basic type checks.

๐ŸŽฏ Use Cases

Backend developers use this when building new API endpoints that need reliable input validation and consistent error responses. Ideal during sprint development when scaffolding CRUD operations for new features.

๐Ÿ”— Related Prompts

๐Ÿ’ป Coding beginner

Explain Code Like Im a Beginner

Get any code explained in plain English with line-by-line breakdowns, analogies, and learning suggestions.

๐Ÿ’ป Coding beginner

Debug My Code and Explain the Fix

Get your code debugged with clear explanations of what went wrong and why, plus the corrected version.

๐Ÿ’ป Coding intermediate

Write Unit Tests for My Code

Generate thorough unit tests covering edge cases, error handling, and both positive and negative scenarios.

๐Ÿ’ป Coding intermediate

Convert Code Between Languages

Convert code between any programming languages while maintaining idiomatic patterns and best practices.

๐Ÿ’ป Coding intermediate

Write a REST API Endpoint

Generate production-ready REST API endpoints with validation, error handling, and documentation.

๐Ÿ’ป Coding advanced

Create a GitHub Actions CI/CD Workflow for Automated Testing and Deployment

Generate a complete GitHub Actions CI/CD workflow with build, test, deploy, and notification jobs for your project.