Skip to content

Go SDK

A Go 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 Go 1.26 or later.

Terminal window
go get mailcycle.email/go

For a step-by-step tutorial with a net/http handler, HTML, attachments and webhooks, see How to send email from Go on the Mailcycle Blog.

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 Go:

mc, phrase, err := mailcycle.CreateAccount("", mailcycle.Options{})
if err != nil {
log.Fatal(err)
}
// Nothing can recover the account without the phrase.
if err := os.WriteFile("mailcycle-phrase.txt", []byte(phrase), 0o600); err != nil {
log.Fatal(err)
}
fmt.Println(mc.AccountID())

Then put it in the environment, where the examples on this page read it:

Terminal window
export MAILCYCLE_PHRASE="$(cat mailcycle-phrase.txt)" # or paste the twelve words in quotes

Keep it out of your code and out of version control. os.Getenv returns "" for a variable that is not set, and SignIn then refuses a phrase of 0 words.

Save this as main.go in a folder with a go.mod (go mod init example then go get mailcycle.email/go), and go run .:

package main
import (
"fmt"
"log"
"os"
"time"
"mailcycle.email/go" // package mailcycle
)
func main() {
mc, err := mailcycle.SignIn(os.Getenv("MAILCYCLE_PHRASE"), mailcycle.Options{})
if err != nil {
log.Fatal(err)
}
address, err := mc.Addresses().Create(mailcycle.NewAddress{Prefix: "signup"})
if err != nil {
log.Fatal(err)
}
fmt.Println("Send an email to", address.EmailAddress)
message, err := mc.Messages().WaitFor(mailcycle.WaitOptions{
AddressID: address.ID,
Timeout: 5 * time.Minute,
})
if err != nil {
log.Fatal(err)
}
fmt.Println(message.FromAddress, message.Subject)
fmt.Println(message.Text)
}

Send it an email from any mailbox you use. WaitFor returns it opened, usually a few seconds after you press Send.

The snippets below leave out the if err != nil checks to stay short. Go will not build a program that sets err and never reads it, so check each one as above.

import "mailcycle.email/go" // package mailcycle
mc, err := mailcycle.SignIn(os.Getenv("MAILCYCLE_PHRASE"), mailcycle.Options{})

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, err := mailcycle.WithAPIKey(
os.Getenv("MAILCYCLE_API_KEY"),
os.Getenv("MAILCYCLE_PHRASE"), // pass "" to manage without opening mail
mailcycle.Options{},
)

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 SignIn opens a 30-day session. Keep mc.SessionToken() somewhere private and pick it up with mailcycle.Resume(token, phrase, options) 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.

address, err := mc.Addresses().Create(mailcycle.NewAddress{
Prefix: "signup",
Label: "Test run 42",
})
addresses, err := mc.Addresses().List()

The 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.

To have the server pick a name, set Style to mailcycle.StylePerson, StyleBusiness, StyleTeam or StyleNeutral. It is used only when LocalPart and Prefix are empty. With none of the three, the name is neutral.

address, err := mc.Addresses().Create(mailcycle.NewAddress{Style: mailcycle.StylePerson})

To choose from a few first, RollNames returns up to eight that are free on the domain right now, like maya.holt or harbor-supply42. An empty domain means the first the account can use.

names, err := mc.Addresses().RollNames(mailcycle.StyleBusiness, "")
address, err := mc.Addresses().Create(mailcycle.NewAddress{LocalPart: names[0]})

A rolled name works as LocalPart on every plan; any other name needs Scale and up. Someone may take a rolled name before you use it, and Create then returns an error.

// The private key at the current epoch, base64url. Pass a pointer for an earlier epoch.
key, err := mc.Addresses().ExportKey(address.ID, nil)
fmt.Println(key.Epoch, key.PrivateKey, key.PublicKey)
// Move the address to its next key epoch, so an exported key stops opening new mail.
id, epoch, err := mc.Addresses().RotateKey(address.ID)

ExportKey gives one address’s private key to a helper that reads that 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 history before the key is derived. An epoch above the address’s current one returns invalid_epoch. RotateKey seals new mail to a new key and hands it to the device or agent holding the address. An address shared with team members returns team_address: rotate it in the web app. Both need the phrase.

messages, err := mc.Messages().All(address.ID, "")
for _, message := range messages {
fmt.Println(message.FromAddress, message.Subject)
fmt.Println(message.Text)
}
data, err := mc.Messages().Attachment(messages[0], 0) // []byte, opened locally

A 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.SafeHTML 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.

code, err := mc.Messages().WaitFor(mailcycle.WaitOptions{
AddressID: 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 one minute unless Timeout says otherwise; when a person sends the mail by hand, give it a few. If nothing matching arrives in time it returns an *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.

sent, err := mc.Messages().Send(mailcycle.Outgoing{
From: address.EmailAddress,
To: "someone@example.com",
Subject: "Hello",
Text: "Hi",
})

sent.SentCopyID is the copy in Sent, which Messages().Get takes, and is empty when the address keeps no copy. sent.MessageID is the Message-ID header.

Set InReplyTo to thread a reply. A session can send from Personal up; an API key on Scale and up. Free receives only.

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 ContentID to show it inline, where the HTML says cid: and that id. Program and script files, like .exe and .js, are refused.

pdf, _ := os.ReadFile("invoice.pdf")
logo, _ := os.ReadFile("logo.png")
_, err := mc.Messages().Send(mailcycle.Outgoing{
From: address.EmailAddress,
To: "someone@example.com",
Subject: "Your invoice",
Text: "Attached.",
HTML: `<img src="cid:logo"><p>Attached.</p>`,
Attachments: []mailcycle.OutgoingAttachment{
{Filename: "invoice.pdf", MimeType: "application/pdf", Content: pdf},
{Filename: "logo.png", MimeType: "image/png", Content: logo, ContentID: "logo"},
},
})
stream, err := mc.Events().Stream(nil)
defer stream.Close()
for {
event, ok := stream.Next(0)
if !ok {
break
}
fmt.Println(event.Type)
}

Events carry ids, never content, and are not replayed after a drop, so StreamHooks.OnReconnect is where to refetch what you are showing.

To check a webhook came from Mailcycle, pass the body exactly as it arrived:

mailcycle.VerifyWebhookSignature(secret, rawBody, r.Header.Get("Mailcycle-Signature"))
key, token, err := mc.APIKeys().Create("CI runner") // keep token; it is shown once
keys, err := mc.APIKeys().List()
err = mc.APIKeys().Revoke(key.ID)

These need a session; an API key gets session_required. The name is sealed on your machine. A name that will not open comes back empty with NameOpened == false. See API keys.

webhook, secret, err := mc.Webhooks().Create(
"https://example.com/mailcycle",
[]string{"message.received"},
"Orders",
)
off := false
_, err = mc.Webhooks().Update(webhook.ID, mailcycle.WebhookUpdate{Enabled: &off})

List, Delete, RotateSecret, Test, Deliveries and Redeliver do what the webhook endpoints do. The secret is shown on create and on rotate, and never again. Creating, changing, rotating and deleting need a session.

sessions, err := mc.Sessions().List()
ended, err := mc.Sessions().RevokeOthers()
err = mc.DeleteAccount()

DeleteAccount erases the account and all its mail at once, with no undo, and needs a session. SignOut on an API-key client does nothing; revoke the key instead.

device, err := mc.Devices().Pair("4827 1934", "Front desk") // the 8 digits
device, err = mc.Devices().Pair("MC1:48271934:9vQ...", "Front desk") // or the QR text
err = mc.Devices().Assign(device.ID, address.ID)
err = mc.Devices().Unassign(device.ID, address.ID)
err = mc.Devices().HandOverKeys(device.ID)

This is the app’s Add device screen, done by a script. A new device holds no addresses. Assign gives it one and hands it the keys, sealed on your machine. Unassign takes one back and hands over a fresh set without it. HandOverKeys does the handover alone.

The QR text carries the device’s own secret, so nothing about the keys passes through Mailcycle. With only the digits, a secret is made here and sealed to a key the device published. The server relays that key, so at pairing it could put its own in its place and read the keys handed over after. The QR path does not have that step. A device that published no key cannot be paired from its digits; scan its QR code in the app, or pass the QR text.

All of this needs a session signed in with the phrase. An API key gets session_required.

An API failure is an *APIError carrying the HTTP Status and the API’s own code. mailcycle.ErrorCode(err) pulls it out, and that is what to branch on.

When the account’s plan does not include a call (plan_required or plan_capability_required), the error also carries RequiredPlanID, the lowest plan that allows it, and UpgradeURL(), the page that moves the account to that plan. Error() ends with the link:

plan_required: API keys need the Operator plan or higher. Upgrade at https://app.mailcycle.email/operator/billing?upgrade=plan_operator
if mailcycle.ErrorCode(err) == "rate_limited" {
// back off
}

It is checked against the published test vectors, including the refusals.

© Northlab Studios Ltd, trading as Mailcycle. Registered in England and Wales, company no. 17462734. Registered office: 167-169 Great Portland Street, London W1W 5PF.