Ruby SDK
A Ruby client for the API that does the encryption the Mailcycle apps do, on your machine. With it a program can create addresses that receive mail, open what arrives, and send. The recovery phrase never leaves the machine it runs on.
It needs Ruby 3.1 or later. Its one dependency is the openssl gem, version 3.0 or later, which Ruby 3.1 already ships.
gem install mailcycleOr in a Gemfile:
gem "mailcycle"For a step-by-step tutorial with Rails, ActiveJob and an Action Mailer delivery method, see How to send email from Ruby on the Mailcycle Blog.
Your recovery phrase
Section titled “Your recovery phrase”Every call starts from the account’s twelve-word recovery phrase; there is no sign-up form. Make a free account at https://app.mailcycle.email/operator/signup (no card) and keep the twelve words it shows, or make one from Ruby:
require "mailcycle"
mc, phrase = Mailcycle.create_accountFile.write("mailcycle-phrase.txt", phrase, perm: 0o600) # nothing can recover the account without itputs mc.account_idThen put it in the environment, where the examples on this page read it:
export MAILCYCLE_PHRASE="$(cat mailcycle-phrase.txt)" # or paste the twelve words in quotesKeep it out of your code and out of version control. If MAILCYCLE_PHRASE is not set, ENV.fetch raises KeyError.
Try it: your first mail
Section titled “Try it: your first mail”require "mailcycle"
mc = Mailcycle.sign_in(ENV.fetch("MAILCYCLE_PHRASE"))address = mc.addresses.createputs "Send an email to #{address.email_address}"
message = mc.messages.wait_for(address_id: address.id, timeout: 300)puts "#{message.from_address}: #{message.subject}"puts message.textSend it an email from any mailbox you use. wait_for returns it opened, usually a few seconds after you press Send.
Sign in
Section titled “Sign in”require "mailcycle"
mc = Mailcycle.sign_in(ENV.fetch("MAILCYCLE_PHRASE"))This is the same handshake the apps use: nothing secret crosses the wire, and it works on every plan.
With an API key instead, on Operator and up:
mc = Mailcycle.with_api_key( ENV.fetch("MAILCYCLE_API_KEY"), phrase: ENV["MAILCYCLE_PHRASE"] # nil to manage without opening mail)Without the phrase a client can manage addresses, devices and metadata, but cannot open mail or create an address that receives it, because the address’s key is derived from the phrase. With both, the phrase is checked against the key’s account before anything is opened.
Each sign_in opens a 30-day session. Keep mc.session_token somewhere private and pick it up with Mailcycle.resume(token, phrase) rather than signing in on every run.
One account covers every device and agent you run. Make it once, then pair each device to it or give each one its own address, as Pair many devices shows. Paid plans pair as many devices as you have, and Fleet is built for device fleets at scale.
Each of these also takes api_url: and timeout: (in seconds).
Addresses
Section titled “Addresses”address = mc.addresses.create(prefix: "signup", label: "Test run 42")
all = mc.addresses.listThe public half of the address’s key goes up with the request so the mail server can seal what arrives; the private half stays on your machine. The label is sealed locally, and the server stores it as ciphertext.
With no prefix: or local_part:, the server rolls a name like quiet_falcon. Pass style: to choose the kind:
address = mc.addresses.create(style: :person) # maya.holtThe styles are :person, :business, :team and :neutral, the default, as symbols, strings or the constants in Mailcycle::NameStyle. style: is ignored when prefix: or local_part: is given.
To pick a name before creating, roll a few:
names = mc.addresses.roll_names(:business) # up to 8, like harbor-supply42
address = mc.addresses.create(local_part: names.first)A rolled name works as local_part: on every plan; any other name needs Scale and up. The names are free when rolled, but someone can take one before you create it, and then create is refused. Pass domain: to roll for another domain; leaving it out uses the one create would.
Export and rotate an address key
Section titled “Export and rotate an address key”exported = mc.addresses.export_key(address.id) # the current key epochold = mc.addresses.export_key(address.id, 0) # or any epoch from 0 to the current oneputs exported.private_key # base64url, 32 bytesputs exported.public_key
mc.addresses.rotate_key(address.id) # => #<struct AddressRotation address_id=..., epoch=1>export_key returns one address’s private key, to give a helper that reads that one address’s mail, such as the decrypt Worker for n8n, Make, Zapier and Pipedream. It opens that address’s mail and nothing else, so treat it like the mail itself. The export is recorded in the account’s history before the key is derived, and only the address id is sent. An epoch outside 0 to the current one raises invalid_epoch.
rotate_key moves the address to its next key epoch, so an exported key stops opening new mail, and hands the new key to the device or agent that holds the address. An address shared with team members raises team_address: rotate it in the web app, which also gives each member the new key. Both need the phrase and a signed-in session.
Reading mail
Section titled “Reading mail”messages = mc.messages.all(address.id)messages.each do |message| puts "#{message.from_address} #{message.subject}"end
data = mc.messages.attachment(messages.first, 0) # a binary String, opened locallyA message that will not open, because there is no phrase or it is the wrong one, comes back with opened false and empty content rather than an error, so one unreadable message does not hide the rest of a page.
message.html is the HTML as the sender wrote it. message.safe_html has every remote image, stylesheet and embed removed, as the apps do it, and is the one to render; message.blocked lists what was taken out.
Waiting for a message
Section titled “Waiting for a message”code = mc.messages.wait_for(address_id: address.id, subject: "verification")Listens on the event stream where it is available and polls the address otherwise, and returns the message opened. It waits 60 seconds unless you pass timeout: (seconds). If nothing matching arrives in time it raises a Mailcycle::ApiError with code "timeout". Mail received from since: on counts, even if it landed before the stream was up; it defaults to now, so to wait for a reply to something you are about to send, take the time before sending.
since = Time.nowmc.messages.send(from: address.email_address, to: "bot@example.com", subject: "Ping", text: "Hi")reply = mc.messages.wait_for(address_id: address.id, subject: "Ping", since: since)Sending
Section titled “Sending”mc.messages.send(from: address.email_address, to: "someone@example.com", subject: "Hello", text: "Hi")Pass reply_to: the message you are answering to thread a reply. A session can send from Personal up; an API key on Scale and up. Free receives only.
send returns a Mailcycle::SendResult: message_id is the Message-ID header, and sent_copy_id is the id of the copy in Sent, which the other message calls take, or nil when the address keeps none.
Attachments take raw bytes and the client encodes them. A message can carry up to ten files, 3.5 MB between them. Give a file a content_id to show it inline, where the HTML says cid: and that id. Program and script files, like .exe and .js, are refused.
files = [ Mailcycle::OutgoingAttachment.new(filename: "invoice.pdf", content: File.binread("invoice.pdf"), mime_type: "application/pdf"), Mailcycle::OutgoingAttachment.new(filename: "logo.png", content: File.binread("logo.png"), mime_type: "image/png", content_id: "logo")]mc.messages.send(from: address.email_address, to: "someone@example.com", subject: "Your invoice", text: "Attached.", html: '<img src="cid:logo"><p>Attached.</p>', attachments: files)Events and webhooks
Section titled “Events and webhooks”stream = mc.events.streamstream.each do |event| puts event.event_typeendEvents carry ids, never content, and are not replayed after a drop, so on_reconnect:, passed to stream, is where to refetch what you are showing. each runs until the stream is closed; next_event(timeout: 30) reads one event with a deadline and returns nil if none comes, and close ends it.
To check a webhook came from Mailcycle, pass the body exactly as it arrived:
genuine = Mailcycle.verify_webhook_signature(secret, raw_body, header)API keys
Section titled “API keys”created = mc.api_keys.create("Deploy script")puts created.token # shown once; keep it somewhere private
mc.api_keys.list.each do |key| puts "#{key.id} #{key.name} ...#{key.hint}"endmc.api_keys.revoke(created.api_key.id)Listing, creating and revoking keys need a signed-in session, so a leaked key cannot make more of itself. The name is sealed on your machine, and the server stores it as ciphertext. A name that will not open comes back as nil.
sign_out on a client built with an API key does nothing. A key is not a session; it works until it is revoked.
Webhooks
Section titled “Webhooks”created = mc.webhooks.create("https://example.com/hooks/mail", ["message.received"], "Orders")secret = created.secret # shown once, for checking signatures
mc.webhooks.update(created.webhook.id, enabled: false)mc.webhooks.test(created.webhook.id)
page = mc.webhooks.deliveries(created.webhook.id)page.deliveries.each do |delivery| puts "#{delivery.event_type} #{delivery.status} #{delivery.response_status}"endWebhooks are on Operator and up. list returns the webhooks with their names opened, and every event type one can ask for. rotate_secret returns a new secret once, and redeliver sends a logged event again with the same event id. Adding, changing, rotating and removing need a signed-in session; an API key can list, test, read deliveries and redeliver.
Sessions
Section titled “Sessions”mc.sessions.list.each do |session| puts "#{session.id} #{session.expires_at} #{session.current}"endmc.sessions.revoke_othersrevoke(id) ends one session. All three need a signed-in session.
Deleting the account
Section titled “Deleting the account”mc.delete_accountErases the account and everything in it at once, with no grace period. Its addresses are retired and never issued again. It needs a signed-in session. If the account balance still holds money you paid in, it is refused with balance_held: ask for the balance back through support first.
mc.subscription returns the plan, renewal date and balance, and mc.plans lists every plan.
Pairing devices
Section titled “Pairing devices”# The text of the device's QR code, or the eight digits it shows.device = mc.devices.pair("MC1:12345678:...", "Kitchen Pi")
mc.devices.assign(device.id, address.id) # hands the keys over toomc.devices.unassign(device.id, address.id)mc.devices.hand_over_keys(device.id) # again, on its ownThis is what the app’s Add device screen does, done by a script. It needs the phrase and a signed-in session. The name is sealed on your machine.
Each device has a wrap secret, and its address keys are sealed under it. The account keeps a copy in the device’s sealed profile, so keys can be handed over again when an address is assigned or taken off.
There are two ways the secret gets here. The QR code carries the device’s own secret, so it never passes through Mailcycle. The eight digits carry no secret, so the client makes one and seals it to a key the device published. The server relays that key, so it could put its own in its place at pairing and read the keys that follow. The QR path does not have that gap. Use it where you can.
A device on an older build publishes no key. Pairing it by its digits fails with an error that says to pair it in the app, or pass its QR code instead.
Errors
Section titled “Errors”Everything this gem raises is a Mailcycle::Error. An API failure is a Mailcycle::ApiError carrying the HTTP status and the API’s own code in code, which is what to match on rather than the message.
begin mc.webhooks.listrescue Mailcycle::ApiError => e raise unless e.code == "plan_required" # Webhooks are on Operator and up.endA plan refusal also carries required_plan_id, the lowest plan that allows the call, and upgrade_url, the page that moves the account to it. The message ends with the link:
plan_required: Webhooks need the Operator plan or higher. Upgrade at https://app.mailcycle.email/operator/billing?upgrade=plan_operatorHow the encryption is checked
Section titled “How the encryption is checked”It is checked against the published test vectors, including the refusals. SHA-256, HMAC-SHA256, PBKDF2, ChaCha20 and X25519 all come from OpenSSL, through Ruby’s own openssl.
© Northlab Studios Ltd, trading as Mailcycle. Registered in England and Wales, company no. 17462734. Registered office: 167-169 Great Portland Street, London W1W 5PF.