docs
SDKs

Ruby and Rails

The gem 1billion sends from ActionMailer with one line, or from plain Ruby with OneBillion::Emails.send, the same call as Resend.

bundle add 1billion

Ruby 3.1 or newer. The gem is named 1billion, and its code is under OneBillion: a Ruby constant can not start with a digit.

Put the API key in the environment as ONEBILLION_API_KEY. The gem reads it from there.

Rails (ActionMailer)

# config/environments/production.rb
config.action_mailer.delivery_method = :onebillion

That is all. Your mailers and views do not change:

UserMailer.login_code(user, "482913").deliver_later

The gem sends the finished MIME message to every recipient (to, cc and bcc), with the Message-ID as the Idempotency-Key. Send with deliver_later, so a slow network never holds a web request.

To set the key in code, use config.action_mailer.onebillion_settings = { api_key: ..., url: ... }. The settings win over the environment.

Send a mail (the same call as Resend)

answer = OneBillion::Emails.send(
  {
    from: "Acme <hi@acme.com>",
    to: "jane@example.com",
    subject: "Your code",
    html: "<p>Your code is <b>482 913</b></p>"
  },
  { idempotency_key: "login-jane-482913" }
)

answer["id"] # the id of the first message; answer["messages"] has one per recipient

This is the call of Resend::Emails.send: a move from Resend changes only the constant. The keys are from, to, cc, bcc, reply_to, subject, html, text, attachments and priority. A key that 1billion does not support (for example tags or scheduled_at) raises an ArgumentError that names it: the gem never sends the mail without it.

Verify a webhook

event = OneBillion::Signature.verify(payload: request.raw_post, headers: request.headers)

It returns the event as a Hash, and raises OneBillion::Signature::Invalid for a bad signature or an old timestamp. The secret comes from ONEBILLION_WEBHOOK_SECRET. See Webhooks.

Read, validate, unsuppress

OneBillion.client is a client with the key of ONEBILLION_API_KEY, ready to use:

OneBillion.client.message("9f2c…")
OneBillion.client.validate("jane@gmial.com")
OneBillion.client.unsuppress("jane@example.com")

More than one key

OneBillion::Emails.send uses one key for the whole process. To send for two apps from one process, make a client for each. send_mail takes the same keys as Emails.send, and also raw, a finished MIME message:

billing = OneBillion::Client.new(api_key: ENV["BILLING_ONEBILLION_API_KEY"])

billing.send_mail(
  from: "Acme Billing <billing@acme.com>",
  to: ["jane@example.com"],
  subject: "Your receipt",
  html: "<p>Thanks for your order.</p>",
  idempotency_key: "receipt-8812"
)

Errors

begin
  OneBillion::Emails.send(mail)
rescue OneBillion::NoCredits => error
  # 429: error.body["retry_after"], error.body["buy_url"]
rescue OneBillion::Refused => error
  # 422: error.check, error.address
rescue OneBillion::Error => error
  # Any other answer: error.status (nil for a network error), error.body
end

Options

OptionDefaultWhat
api_key:ENV["ONEBILLION_API_KEY"]the API key of the app
url:ENV["ONEBILLION_URL"], else the 1billion APIanother console, for example a dev stack
timeout:10seconds for each try
backoff:0.5the first wait between tries, in seconds

Source and changelog: clowk-in/1billion-ruby.

On this page