First App

Build a complete, working Azu application from scratch. This guide will walk you through creating a user management API with type-safe endpoints, validation, and real-time features.

What You'll Build

By the end of this guide, you'll have a fully functional application with:

  • Type-safe API endpoints for user management

  • Request validation with detailed error messages

  • Real-time notifications via WebSocket

  • Template rendering with hot reloading

  • Comprehensive error handling

Project Setup

1. Create Project Structure

# Create project directory
mkdir user-manager
cd user-manager

# Initialize Crystal project
crystal init app user_manager
cd user_manager

2. Add Dependencies

Edit shard.yml:

Install dependencies:

3. Create Project Structure

Building the Application

Step 1: Define the User Model

Create src/models/user.cr:

Production Note: This tutorial uses in-memory storage for simplicity. For production applications, use CQL for database persistence.

Step 2: Create Request Contracts

Create src/requests/create_user_request.cr:

Create src/requests/update_user_request.cr:

Step 3: Create Response Objects

Create src/responses/user_response.cr:

Create src/responses/users_list_response.cr:

Step 4: Create Endpoints

Create src/endpoints/create_user_endpoint.cr:

Create src/endpoints/list_users_endpoint.cr:

Create src/endpoints/show_user_endpoint.cr:

Create src/endpoints/update_user_endpoint.cr:

Create src/endpoints/delete_user_endpoint.cr:

Step 5: Create WebSocket Channel

Create src/channels/user_notification_channel.cr:

Step 6: Create Templates

Create templates/users/index.html:

Step 7: Create Main Application

Create src/user_manager.cr:

Running Your Application

1. Start the Server

You should see:

2. Test the API

Create a User

Response:

List Users

Response:

Get a Specific User

Update a User

Delete a User

3. Test the Web Interface

Open your browser and navigate to http://localhost:4000. You'll see:

  • A form to create new users

  • A list of all users

  • Real-time notifications when users are created, updated, or deleted

4. Test WebSocket Notifications

Open the browser console and watch for real-time notifications when you:

  • Create a new user via the form

  • Delete a user via the API

  • Update a user via the API

Testing Error Handling

Test Validation Errors

Response:

Test Duplicate Email

Test Not Found

Project Structure

Your completed project should look like:

Next Steps

Congratulations! You've built a complete Azu application. Here's what to explore next:

  1. Configuration → - Learn about advanced configuration options

  2. Core Concepts →arrow-up-right - Deep dive into endpoints, requests, and responses

  3. Real-Time Features →arrow-up-right - Master WebSocket channels and live components

  4. Templates →arrow-up-right - Learn about template rendering and markup DSL

  5. Testing →arrow-up-right - Write comprehensive tests for your application

Extending Your Application

Consider adding these features:

  • Database integration with PostgreSQL or MySQL

  • Authentication and authorization

  • File uploads for user avatars

  • Pagination for the users list

  • Search and filtering capabilities

  • Email notifications when users are created

  • API rate limiting

  • Request logging and monitoring


Your first Azu application is complete! You now have a solid foundation for building more complex applications with type safety, real-time features, and excellent developer experience.

Last updated

Was this helpful?