Quickstarts
Send with Sinatra
Send email from a Sinatra app with the usend gem, and receive webhooks.
You need a verified domain and an API key.
gem install usend sinatra
export USEND_API_KEY="us_123456789"Send an email
require "sinatra"
require "usend"
USEND = Usend::Client.new(api_key: ENV.fetch("USEND_API_KEY"))
post "/signup" do
USEND.emails.send_email(
{
from: "Acme <[email protected]>",
to: [params[:email]],
subject: "Welcome",
html: "<p>Thanks for signing up!</p>"
},
idempotency_key: "signup-#{params[:email]}"
)
"ok"
rescue Usend::Error => e
halt 502, "#{e.name}: #{e.message}"
endRate limits and server errors are retried for you, and the idempotency key makes a retry safe. See Idempotency.
Receive webhooks
post "/usend/webhook" do
event = Usend::Webhook.verify(
payload: request.body.read, # the raw body
headers: request.env, # Rack's HTTP_WEBHOOK_* keys work
secret: ENV.fetch("USEND_WEBHOOK_SECRET")
)
puts event["type"]
status 204
rescue Usend::Error
status 400
endSee Verify signatures.
Notes
- A
422means the request is invalid (for example thefromdomain is not verified); fix it rather than retrying. See Errors. - While your organization is in the sandbox, send only to verified members.
Without the gem
Net::HTTP works too: POST https://api.usend.et/emails with an Authorization: Bearer header and a JSON body. Or send over SMTP with the mail gem: host smtp.usend.et, port 465 or 587, username usend, your API key as the password.
More fields: Send emails.