Ruby SDK
Send email with Hanami
Register the Parcel Wing Ruby SDK as a Hanami provider and inject it into actions.
Install the SDK
The official Ruby SDK is published as parcelwing.
Terminal
gem install parcelwing
Register a provider
Register the Parcel Wing client in Hanami's container so actions can request it as a dependency.
config/providers/parcelwing.rb
# config/providers/parcelwing.rbHanami.app.register_provider :parcelwing doprepare dorequire "parcelwing"endstart doregister "parcelwing.client", ParcelWing::Client.new(api_key: target["settings"].parcelwing_api_key)endend
Send from an action
Inject the client into an action, validate params, and send a saved template.
app/actions/send_welcome.rb
# app/actions/send_welcome.rbmodule Acmemodule Actionsclass SendWelcome < Acme::Actioninclude Deps[parcelwing: "parcelwing.client"]params dorequired(:email).filled(:string)optional(:first_name).maybe(:string)enddef handle(request, response)halt 422, request.params.errors.to_h.to_json unless request.params.valid?emails = parcelwing.emails.send(from: ENV.fetch("PARCELWING_FROM_EMAIL"),to: request.params[:email],template_alias: "welcome",template_params: { first_name: request.params[:first_name] || "friend" })response.format = :jsonresponse.body = { id: emails.first.fetch("id") }.to_jsonendendendend
Production notes
- Keep
PARCELWING_API_KEYin server-side environment variables. - Use saved templates for onboarding, invites, receipts, and account notifications.
- Log
request_idfromParcelWing::Errorfor support and debugging.