Skip to main content

Creating Your First Flavor

This guide will walk you through creating a custom flavor from scratch. We’ll build a simple “Task” flavor that demonstrates all the core concepts using the actual patterns from the Tasteful codebase.

Planning Your Flavor

Before coding, consider:
  • Domain responsibility: What specific functionality will this flavor handle?
  • API endpoints: What HTTP routes do you need?
  • Services: What business logic components are required?
  • Dependencies: What other services or data stores do you need?
For our Task flavor, we’ll need:
  • CRUD operations for task items
  • Task service for business logic
  • Repository for data persistence
  • Configuration for database connection

Step 1: Create the Basic Structure

Create the flavor directory structure (following the same pattern as the built-in HealthFlavor):

Step 2: Create Configuration

Start with the configuration class in config.py. The BaseConfig class provides automatic environment variable loading and validation:
For comprehensive configuration management patterns, see the Configuration Management Guide.

Step 3: Create Repository

Create the repository for data access in repository.py:

Step 4: Implement the Service

Create the business logic in service.py:

Step 5: Create the Controller

Create the HTTP controller in controller.py:

Step 6: Create the Flavor

Now implement the main flavor in flavor.py:

Step 7: Export the Flavor

Make your flavor importable by updating __init__.py:

Step 8: Register with Your Application

Add the flavor to your main application:

Step 9: Test Your Flavor

Run your application and test the endpoints:
Visit http://localhost:8000/docs to see your API documentation automatically generated by FastAPI.

Advanced Features

Complex Dependency Injection

You can create more complex dependency graphs like in the main.py example:

Environment-Specific Configuration

Use environment variables and validation in your config:
Create a .env file for local development:
For advanced configuration patterns including nested configuration, validation, and environment-specific setups, see the Configuration Management Guide.

Best Practices

1. Separation of Concerns

  • Config: Configuration and settings
  • Services: Business logic and orchestration
  • Repositories: Data access and persistence
  • Controllers: HTTP layer and request/response handling
  • Flavors: Component composition and dependency wiring

2. Error Handling

Use FastAPI’s HTTPException in controllers for consistent error responses:

3. Async Operations

Use async/await for I/O operations in controllers:

4. Documentation

Add comprehensive docstrings to controllers and services:

Next Steps

Testing Your Flavor

Learn how to write comprehensive tests for your flavors

Adding Services

Explore advanced service patterns and dependency injection

Database Integration

Connect your flavors to databases with repositories

Authentication

Secure your flavors with authentication and authorization