Configuring Hanami Providers
Set up providers for services, databases, and application components following Hanami conventions.
Quick Reference
- Location:
config/providers/<name>.rb - Structure:
Hanami.app.register_provider(:name) do ... end - Lifecycle:
prepare(lazy) →start(eager, on first use or boot) - Rule: Every external service gets a provider. Settings go through
target["settings"].
HARD-GATE
DO NOT hardcode credentials, URLs, or API keys in providers.
DO NOT call the container directly outside of providers.
DO use settings for all environment-dependent configuration.
Core Process
- Identify the dependency — external service, database, or application component.
- Define settings in
config/settings.rbif the service needs configuration:setting :redis_url, constructor: Types::String - Create the provider at
config/providers/<name>.rb:Hanami.app.register_provider(:redis) do prepare do require "redis" end start do url = target["settings"].redis_url client = Redis.new(url:) register("redis", client) end end - Use
preparefor requiring gems and lightweight setup. - Use
startfor instantiation and registration. Access other registered components viatarget[...]. - Register with a descriptive key —
"redis","http_client", not generic names. - Verify — ensure the provider boots without errors. If it needs external resources (database, API), handle connection failures gracefully.
Common Provider Patterns
Apply these patterns for frequently encountered service types:
- ROM/database connection — register the ROM container in
start; usetarget["settings"]for the database URL; depend on the:persistenceprovider in consumers. - HTTP client — require the HTTP library in
prepare; instantiate with base URL and headers from settings instart; register as"http_client"or a service-specific key. - Background job adapter — configure the adapter class and queue connection in
start; register as"jobs.adapter"so job classes can resolve it viaDeps. - Cache store — require the cache library in
prepare; build the store with TTL and connection settings fromtarget["settings"]instart.
For extended code examples, see PROVIDER_PATTERNS.md.
Output Style
- Provider file — the complete
config/providers/<name>.rbcontent. - Settings additions — any new settings needed in
config/settings.rb. - Registration key — the key used to register the component.
- Injection usage — how to use
include Deps["<key>"]in consumers. - Verification — how to confirm the provider boots correctly.
- English only unless user requests otherwise.
Integration
| Skill | When to chain |
|---|---|
| load-context | Always first — discover existing providers before adding new ones |
| implement-di | After provider setup, to configure injection in consumers |
| hanami-setup | Part of the project onboarding workflow |