Request Generator

The Request Generator creates request structs that define the structure and validation rules for incoming data in your Azu application. These align with the Azu::Request convention.

Deprecation Notice: The contract generator is deprecated. Use request instead. Running azu generate contract will automatically redirect to request.

Usage

azu generate request REQUEST_NAME [attr:type...] [OPTIONS]

Description

Requests in Azu applications provide a way to validate and structure incoming data from HTTP requests, API calls, or form submissions. They ensure data integrity and provide clear error messages when validation fails.

Options

  • REQUEST_NAME - Name of the request to generate (required)

  • attr:type - Field definitions (e.g., name:string email:string age:int32)

  • --force - Overwrite existing files

  • --help - Show help message

Examples

Generate a basic request

This creates:

  • src/requests/user/index_request.cr - The request struct

Generate request for specific action

Generate request with various field types

Field Types

The request generator supports these field types:

Type
Crystal Type
Description

string

String

Text strings

text

String

Long text

int32, integer

Int32

32-bit integers

int64

Int64

64-bit integers

float32

Float32

32-bit floats

float64, float

Float64

64-bit floats

bool, boolean

Bool

Boolean values

time, datetime

Time

Date and time

date

Date

Date only

json

JSON::Any

JSON data

reference, belongs_to

Int64

Foreign key reference

Generated File Structure

Generated Code Example

Request Struct (src/requests/user/create_request.cr)

Using Requests in Endpoints

Basic Usage

With Validation

Request Patterns

Basic Request Pattern

Request with Optional Fields

Request with Nested Data

Best Practices

1. Keep Requests Focused

Each request should validate a specific use case:

2. Use Type-Safe Fields

3. Provide Default Values

4. Document Expected Formats

Testing Requests

Unit Testing

  • azu generate endpoint - Generate API endpoints

  • azu generate model - Generate data models

  • azu generate service - Generate business logic services

  • azu generate scaffold - Generate complete CRUD resources

Migration from Contract

If you're migrating from the deprecated contract generator:

  1. Rename contract to request in your generator commands

  2. Update class inheritance from Azu::Contract to include Azu::Request

  3. Replace validation methods with the request pattern

Last updated