Authentication

Authentication

Make an app key, keep it as MAILSET_KEY, and the SDK signs every request with it.

On this page
  1. Make a key
  2. Use it
  3. App keys and API tokens
  4. Keep a key alive
  5. Revoke a key
  6. When the person who approved a key leaves
  7. Commands that need a passkey

An app key is the one secret your product's server holds. Keep it in the environment as MAILSET_KEY: the SDK reads it from there and signs every request with it, so your code never handles the key or a signature.

Make a key

A key sends as one mailbox, or as the addresses you name on a sending-only domain. Which one depends on where your team's email lives.

When your team's mailboxes are in Mailset, make the key for the mailbox your product sends as:

mailset keys create --send --mailbox moc.elpmaxe@olleh --name "Acme API"

When your team's email stays elsewhere and the domain only sends, make the key for the domain and name each From address your product uses:

mailset keys create --send --domain example.com --from moc.elpmaxe@stpiecer --from moc.elpmaxe@ylperon --name "Acme API"

A key that may send as any address could send as your CEO, so name the ones your product uses. --any-address in place of --from is for a domain where nobody has a mailbox. Send from a domain whose mail stays elsewhere sets such a domain up.

Either command prints a link and a code. Open the link on a device with your passkey, check the code and approve. The key then prints once.

In the app, open Settings › Developers › App keys and press Make a key. Under What the key sends as, pick One mailbox or Addresses on a sending domain, name the key, and press Make the key. Your passkey approves it, and the key shows once with a Copy key button.

Either way:

  • Keep the key in your secret store as MAILSET_KEY. Mailset shows it once and can't show it again.
  • It sends as that mailbox, or as those addresses, and does nothing else.
  • Make one key for each kind of email. Sign-in codes on one key and announcements on another never stop each other.

Use it

Install the SDK for your language.

npm install mailset
pip install mailset
go get mailset.app/go
gem install mailset
composer require mailset-app/php
implementation("app.mailset:java:0.7.0")
cargo add mailset

Make one client when your server starts. It reads the key from the environment.

import { Mailset } from 'mailset';

// One client for the whole server, from MAILSET_KEY
// in the environment. Nothing is sent until you
// call send.
export const mailset = new Mailset();
from mailset import Mailset

# One client for the whole server, from MAILSET_KEY.
# Nothing is sent until you call send.
mailset = Mailset()
import (
	"log"

	mailset "mailset.app/go"
)

// One client for the whole server.
client, err := mailset.NewClient() // reads MAILSET_KEY
if err != nil {
	log.Fatal(err)
}
// Nothing is sent until you call a method, and it
// prints the kind of credential, never its value.
log.Println(client)
require 'mailset'

# One client for the whole server.
# Mailset::Client.new alone reads MAILSET_KEY too.
# Nothing is sent until you call a method.
MAILSET = Mailset::Client.new(
  key: ENV.fetch('MAILSET_KEY')
)
<?php

require 'vendor/autoload.php';

// One client for the whole server. It reads MAILSET_KEY
// from the environment; ['key' => …] passes one instead.
// Nothing is sent until you call send.
$mailset = new Mailset\Client();
import app.mailset.Mailset;

// One client for the whole server, from
// MAILSET_KEY in the environment. Safe to share
// between threads; nothing is sent until you
// call a method.
Mailset mailset = Mailset.fromEnv();
use mailset::Client;

// Reads MAILSET_KEY, else MAILSET_TOKEN.
let mailset = Client::from_env()?;
// One client for the whole server: its clones
// share its connections. Nothing is sent until
// you call a method.

Or pass the key in, from your own config or secret store:

import { Mailset } from 'mailset';

// From your secret store or config, never in the code.
// Or { token } with an API token.
const mailset = new Mailset({ key: config.mailsetKey });
from mailset import Mailset

# From your secret store or config, never in the code.
# Or token= with an API token.
mailset = Mailset(key=settings.mailset_key)
import (
	"log"

	mailset "mailset.app/go"
)

// From your secret store or config, never in the code.
// Or WithToken(cfg.MailsetToken) with an API token.
client, err := mailset.NewClient(
	mailset.WithKey(cfg.MailsetKey),
)
if err != nil {
	log.Fatal(err)
}
require 'mailset'

# From your secret store or config, never in the code.
# Or token: with an API token.
mailset = Mailset::Client.new(key: config.mailset_key)
require 'vendor/autoload.php';

// From your secret store or config, never in the code.
// Or ['token' => …] with an API token.
$key = $config['mailset_key'];
$mailset = new Mailset\Client(['key' => $key]);
import app.mailset.Mailset;

// From your secret store or config, never in the code.
// Or .token(…) with an API token.
Mailset mailset = Mailset.builder()
        .key(config.mailsetKey())
        .build();
use mailset::Client;

// From your secret store or config, never in the code.
// Or `.token(…)` with an API token.
let mailset = Client::builder()
    .key(&config.mailset_key)
    .build()?;

Each SDK has a page of its own with every call from here, starting with Send.

App keys and API tokens

App keyAPI token
Looks likemsk1_…mst_…
Kept asMAILSET_KEYMAILSET_TOKEN
Made withmailset keys createmailset tokens create
Acts asYour productYou
Can runOnly what it was grantedEvery command you can

A product's server gets an app key, never an API token. A token is your own login for a script and can run every command you can.

Keep a key alive

A key that goes 90 days without a signed call stops working. Any signed call moves that 90 days out, so call key.check() when your server starts and once a day. Keep a key alive has the code.

Mailset writes to whoever approved the key 14 days and 3 days before it stops, with a link that renews the same key. From a terminal:

  • mailset keys list shows each key's id, what it sends as and when it was last used.
  • mailset keys renew <id> renews one, with your passkey.

A key already past its end is gone. Make a new one and put it in the old one's place.

Revoke a key

mailset keys revoke <id> ends a key at once. So does Revoke beside the key in Settings › Developers › App keys. From that moment its server can't send with it.

Revoke a key you think has leaked, then make a new one.

When the person who approved a key leaves

A key belongs to the person who approved it. When that person is removed from the org or deactivated, or their account is deleted, their keys stop at once and the products that hold them can't send.

Mailset warns before it happens and tells the owners after:

  • Before. The app's Remove and Deactivate dialogs and mailset members remove list the person's keys that send for the org: each key's name, what it sends as, and when it was last used. A key used in the last 24 hours is marked in use.
  • After. Every owner of the org gets a notice naming the keys that stopped, and so does the admin who did it.

So make the replacements first. Someone who is staying approves a new key, it goes in the product's MAILSET_KEY, and then the person can go.

Commands that need a passkey

Some commands change something an attacker would want, such as adding a mailbox or removing a domain. With an API token they wait until you approve them with your passkey: the CLI prints a link and waits for you. An app key never meets this, since nothing it can run needs a passkey.

Over HTTP the approval is four requests, listed under Commands that need a passkey.


Calling the API without an SDK →