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_manager2. 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:
Configuration → - Learn about advanced configuration options
Core Concepts → - Deep dive into endpoints, requests, and responses
Real-Time Features → - Master WebSocket channels and live components
Templates → - Learn about template rendering and markup DSL
Testing → - 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?
