Ruby SDK
Send email with Grape
Use Parcel Wing from Grape APIs with declared params and helper-backed SDK clients.
Install the SDK
The official Ruby SDK is published as parcelwing.
Terminal
gem install parcelwing
Send from an API resource
Declare request params, keep the API key server-side, and translate SDK errors into JSON API responses.
api.rb
require "grape"require "parcelwing"class API < Grape::APIformat :jsonhelpers dodef parcelwing@parcelwing ||= ParcelWing::Client.new(api_key: ENV.fetch("PARCELWING_API_KEY"))endendresource :emails doparams dorequires :email, type: Stringoptional :first_name, type: Stringendpost :welcome doemails = parcelwing.emails.send(from: ENV.fetch("PARCELWING_FROM_EMAIL"),to: params[:email],template_alias: "welcome",template_params: { first_name: params[:first_name] || "friend" }){ id: emails.first.fetch("id") }rescue ParcelWing::Error => errorerror!({ error: error.code, request_id: error.request_id }, error.status || 500)endendend
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.