Calling the API without an SDK
How to sign a request yourself, with a test vector and a client in Python, Go, Ruby, PHP, Java and Rust.
On this page
This page is for a language Mailset has no SDK for, or a client you would rather write yourself. With an SDK or the CLI you can skip it, since both sign every request for you. Authentication covers making a key and keeping it alive.
Every request carries a token and a signature made with a private key. Both live inside one credential, so your code holds one secret and signs each request with it. A captured request can't be changed, or replayed more than 2 minutes later, because the signature covers the body and the time.
An app key (MAILSET_KEY) and an API token (MAILSET_TOKEN) sign requests the same way.
What is inside
Take the prefix off (msk1_ or mst_) and decode the rest as base64url. It is JSON:
{
"api": "https://mailset.app",
"token": "the bearer token",
"privateKey": { "kty": "EC", "crv": "P-256", "d": "…", "x": "…", "y": "…" }
}
privateKey is a P-256 key as a JWK. An API token has no api: send its requests to https://mailset.app.
Sign a request
Send three headers with every request.
| Header | Value |
|---|---|
Authorization | Bearer and the credential's token |
ms-time | The time now, in milliseconds since the epoch, as digits |
ms-proof | The signature, base64url with no padding |
To make ms-proof:
- Hash the body's bytes with SHA-256 and write the hash as base64url with no padding. With no body, hash zero bytes.
- Join five lines with a line feed (
\n), with none after the last: the textms-proof-v1, the method in capitals, the path with its query string, the samems-time, and the hash from step 1. - Sign that text's UTF-8 bytes with the private key: ECDSA on P-256 with SHA-256.
- Write the signature as the 32 bytes of
rfollowed by the 32 bytes ofs, then base64url those 64 bytes.
Mailset accepts a request whose ms-time is within 2 minutes of its own clock.
Two traps
Your library returns DER, and Mailset wants raw r and s. Most ECDSA libraries answer with an ASN.1 structure of 70 to 72 bytes. Take r and s out of it and pad each to 32 bytes. Go's ecdsa.Sign gives you both numbers already. Java has SHA256withECDSAinP1363Format and Rust's p256 has to_bytes(), and each writes the 64 bytes directly. Python, Ruby and PHP need the conversion, and the clients below do it.
The key is a JWK, and your library may want something else. d is the private number, 32 bytes, big-endian, in base64url. Python, Go and Rust build a key from it directly. Ruby and PHP want a DER or PEM key, so the clients below wrap d in the fixed bytes of a P-256 private key.
Smaller things that give the same refusal:
- Sign the bytes you send. Encode the JSON once, sign those bytes, and send them unchanged.
- The path includes the query string, exactly as sent.
ms-timeis milliseconds, not seconds.- base64url uses
-and_, and has no=at the end.
Every signing mistake gets the same answer, 401 with unauthorized, so a wrong signature tells an attacker nothing. Check your code against the vector below to find out which step is off.
Test vector
The key here is a public example, made for this page. It is no one's credential.
The key, as MAILSET_KEY:
msk1_eyJhcGkiOiJodHRwczovL21haWxzZXQuYXBwIiwidG9rZW4iOiJleGFtcGxlLXNlc3Npb24tdG9rZW4tbm90LWEtcmVhbC1vbmUiLCJwcml2YXRlS2V5Ijp7ImNydiI6IlAtMjU2IiwiZCI6Imp3SlNWRnpaY3NqQW5FNzRoYWVtclEtbHJCbFo1T05xT2ctUUp0OFJNdVEiLCJleHQiOnRydWUsImtleV9vcHMiOlsic2lnbiJdLCJrdHkiOiJFQyIsIngiOiJsclRfcTRjcGpubC05c1BDTG1KNnp1OVpWcURpMURJVy0zT25uMDdPaXNzIiwieSI6IjFkbjdaUGRjeWNFYW0tdVFuRnZRQnRKZHFTWUUycXZQa05meTl2eW1BZWcifX0
What it decodes to:
{
"api": "https://mailset.app",
"token": "example-session-token-not-a-real-one",
"privateKey": {
"crv": "P-256",
"d": "jwJSVFzZcsjAnE74haemrQ-lrBlZ5ONqOg-QJt8RMuQ",
"ext": true,
"key_ops": [
"sign"
],
"kty": "EC",
"x": "lrT_q4cpjnl-9sPCLmJ6zu9ZVqDi1DIW-3Onn07Oiss",
"y": "1dn7ZPdcycEam-uQnFvQBtJdqSYE2qvPkNfy9vymAeg"
}
}
The request is POST /v1/grants.current at ms-time: 1800000000000, with this body:
{"input":{}}
The body’s digest, base64url of its SHA-256:
LucVGPpXytzpZJYfHPBNn_to9wFu4zKm0rqNW0-CXSQ
The text to sign, five lines joined by a line feed, with none after the last:
ms-proof-v1
POST
/v1/grants.current
1800000000000
LucVGPpXytzpZJYfHPBNn_to9wFu4zKm0rqNW0-CXSQ
A signature is different every time, so yours won’t match this one. This one is valid for the text above, and your verifier should accept it with the public key below:
KU1GgeZBYLyL81sJwaXinOvjxQpYdTDFSw8YjE9HK3ff5pOYc7oAjH6xmw4qlnhHs84FW8b_udj8nzSu43WqSw
{
"kty": "EC",
"crv": "P-256",
"x": "lrT_q4cpjnl-9sPCLmJ6zu9ZVqDi1DIW-3Onn07Oiss",
"y": "1dn7ZPdcycEam-uQnFvQBtJdqSYE2qvPkNfy9vymAeg"
}
A client in your language
Each file is a whole program: it reads MAILSET_KEY or MAILSET_TOKEN, signs a request and prints the answer. sign_request is the part to copy, and run is one signed call. These are the files Mailset's own tests run against the API.
"""Calls Mailset's HTTP API from Python: one signed request, and its answer.
pip install cryptography # the standard library has no ECDSA
MAILSET_KEY=msk1_... python3 request.py
An API token works the same way, on the commands a person may run:
MAILSET_TOKEN=mst_... python3 request.py whoami
"""
import base64
import hashlib
import json
import os
import sys
import time
import urllib.error
import urllib.request
// Calls Mailset's HTTP API from Go: one signed request, and its answer.
// Standard library only, Go 1.25 or later.
//
// MAILSET_KEY=msk1_... go run request.go
//
// An API token works the same way, on the commands a person may run:
//
// MAILSET_TOKEN=mst_... go run request.go whoami
package main
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
# Calls Mailset's HTTP API from Ruby: one signed request, and its answer.
# Standard library only. On Ruby 3.4 and later under Bundler, add gem "base64".
#
# MAILSET_KEY=msk1_... ruby request.rb
#
# An API token works the same way, on the commands a person may run:
#
# MAILSET_TOKEN=mst_... ruby request.rb whoami
require 'base64'
require 'json'
require 'net/http'
require 'openssl'
# base64url with no padding, as Mailset writes it.
def b64url(bytes)
Base64.urlsafe_encode64(bytes, padding: false)
end
<?php
// Calls Mailset's HTTP API from PHP 8: one signed request, and its answer.
// Needs the openssl and curl extensions. No Composer packages.
//
// MAILSET_KEY=msk1_... php request.php
//
// An API token works the same way, on the commands a person may run:
//
// MAILSET_TOKEN=mst_... php request.php whoami
// base64url with no padding, as Mailset writes it.
function b64url(string $bytes): string
{
return rtrim(strtr(base64_encode($bytes), '+/', '-_'), '=');
}
function b64urlDecode(string $text): string
{
return base64_decode(strtr($text, '-_', '+/'));
}
// Calls Mailset's HTTP API from Java 17: one signed request, and its answer.
// Standard library only.
//
// MAILSET_KEY=msk1_... java Request.java
//
// An API token works the same way, on the commands a person may run:
//
// MAILSET_TOKEN=mst_... java Request.java whoami
import java.math.BigInteger;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.security.AlgorithmParameters;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
// Calls Mailset's HTTP API from Rust: one signed request, and its answer.
//
// Rust runs a project, not a file, so make one and put this file in it:
//
// cargo new mailset-request && cp request.rs mailset-request/src/main.rs
// cd mailset-request && MAILSET_KEY=msk1_... cargo run
//
// with these lines in its Cargo.toml:
//
// [dependencies]
// base64 = "0.23.1"
// p256 = { version = "0.14.0", features = ["ecdsa"] }
// reqwest = { version = "0.13.5", features = ["blocking"] }
// serde_json = "1.0.154"
// sha2 = "0.11.3"
//
// An API token works the same way, on the commands a person may run:
//
// MAILSET_TOKEN=mst_... cargo run -- whoami
Python needs the cryptography package, since its standard library has no ECDSA. Go needs 1.25 or later. Ruby, PHP and Java use what ships with them. Rust needs 1.88 or later and the p256, sha2, base64, reqwest and serde_json crates. It runs a project, not a file, so the client opens with the lines for Cargo.toml and the commands to run it.
Run a command
Every command is POST /v1/<command id> with a JSON body, { "input": { … } }. The answer is JSON:
{
"ok": true,
"data": { "name": "Acme API", "expiresAt": 1803772800000, "mailboxes": [] }
}
Or a refusal, with an HTTP status to match:
{
"ok": false,
"error": {
"code": "forbidden",
"message": "This app’s grant doesn’t allow that."
}
}
An org's command also takes org beside input, for a person in more than one org. Errors and limits lists every code, and the API reference every command.
To send an email this way, build the whole message and submit it. Send your own MIME has the code in each language.
Commands that need a passkey
Some commands change something an attacker would want: adding a mailbox, removing a domain. With an API token they answer 403 with step_up_required and a purpose. To go ahead:
POST /v1/approvalswith thatpurposeas the body. The answer has anidand anapproveUrl.- Open
approveUrlon a device with your passkey and approve it. GET /v1/approvals/<id>untilstateisapproved. It can also enddeniedorexpired.- Send the command again with
"approval": "<id>"besideinput.
Both calls are signed like any other. An app key never meets this: nothing it can run needs a passkey.