Sign inGet Started

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.rb
Hanami.app.register_provider :parcelwing do
prepare do
require "parcelwing"
end
 
start do
register "parcelwing.client", ParcelWing::Client.new(
api_key: target["settings"].parcelwing_api_key
)
end
end

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.rb
module Acme
module Actions
class SendWelcome < Acme::Action
include Deps[parcelwing: "parcelwing.client"]
 
params do
required(:email).filled(:string)
optional(:first_name).maybe(:string)
end
 
def 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 = :json
response.body = { id: emails.first.fetch("id") }.to_json
end
end
end
end

Production notes

  • Keep PARCELWING_API_KEY in server-side environment variables.
  • Use saved templates for onboarding, invites, receipts, and account notifications.
  • Log request_id from ParcelWing::Error for support and debugging.