acme API reference
An ACME (RFC 8555) client: obtain and renew TLS certificates from Let's Encrypt and compatible CAs. It drives the full flow - account registration, a new order, HTTP-01 or DNS-01 challenge, CSR finalize, and certificate download - over http + json, with every request a JWS signed by the account key (RS256 for an RSA key; ES256 / ES384 / ES512 for an EC key, by curve). Keys and the CSR come from the crypto library, so this needs the default jennifer binary.
The building blocks are separate calls, not one obtain(), because proving domain control is the caller's job: between order and finalize you must publish the challenge response - serve keyAuthorization at /.well-known/acme-challenge/<token> (HTTP-01), or set the dnsRecord TXT at _acme-challenge.<domain> (DNS-01) - then accept the challenge. See the demo for the orchestration.
Test against a CA's staging endpoint first (Let's Encrypt staging issues untrusted certs with far higher rate limits).
A CA's refusal or a malformed answer throws an acme.AcmeError (kind "acme"); a socket failure, a timeout or a reply over http's cap goes on as http threw it (kind "net", "timeout", "limit", ...). A CA's error answer carries its RFC 7807 problem document: the problemType (urn:ietf:params:acme:error:rateLimited, ...:unauthorized, ...), the detail, the HTTP status, the whole problem (with subproblems), and retryAfter, the seconds a rate-limited client must wait. An answer that is not the JSON object RFC 8555 defines (not JSON, a required member missing or of the wrong type, an unknown status) is an AcmeError too.
Every request goes over https:// (RFC 8555 6.1): a directory or endpoint URL with http:// to a non-loopback host throws an AcmeError before anything is sent, unless the client comes from connectWith with allowInsecureHttp set (a test CA on a trusted network only). http:// to localhost, 127.0.0.0/8 or [::1] is always allowed.
Import with import "acme.j" as acme;. See the acme guide for prose and examples.
Contents
| Name | What it is |
|---|---|
accept | Tell the CA a challenge response is in place (a POST of {} to the challenge URL). |
authorization | Fetch an authorization: its domain, status, and the challenges the CA offers. |
challenge | The challenge of a given type within an authorization ("http-01" / "dns-01"). |
connect | Build a client from a CA directory URL and an account private key (PEM bytes, RSA or EC - the private h... |
connectWith | connect with options: allowInsecureHttp lets the client talk plain http:// to a non-loopback test CA ... |
dnsRecord | The TXT record value for a DNS-01 challenge: the base64url (unpadded) SHA-256 of the key authorization. |
downloadCertificate | Download the issued certificate chain (PEM) for a valid order. |
fetchOrder | Fetch an order's current state by URL (a POST-as-GET). |
finalize | Finalize an order by submitting a CSR, then poll the order until it is valid (the certificate is ready) o... |
keyAuthorization | The key authorization for a challenge token: token.thumbprint, where the thumbprint is the RFC 7638 SHA-2... |
order | Create an order for one or more domains. |
pollAuthorization | Poll an authorization until it leaves pending, waiting intervalMs between attempts up to maxTries. |
register | Register (or look up) the account with the CA and return a client carrying the account URL (kid). |
AcmeError | struct: A failed ACME operation. |
Authorization | struct: A domain-control authorization: the challenges the CA offers for it. |
Challenge | struct: One challenge within an authorization. |
Client | struct: An ACME client: the CA's directory endpoints plus the account key and (once registered) the account URL use... |
Options | struct: Options for connectWith. |
Order | struct: An ACME order for one or more identifiers (domains). |
Functions
accept
acme.accept(client as Client, challengeUrl as string)Tell the CA a challenge response is in place (a POST of {} to the challenge URL). Do this only after publishing the HTTP-01 / DNS-01 response.
client- a registered client
challengeUrl- the challenge URL (from
challenge) - Returns
null - nothing
- Throws
AcmeError - on an accept error
authorization
acme.authorization(client as Client, authzUrl as string)Fetch an authorization: its domain, status, and the challenges the CA offers.
client- a registered client
authzUrl- an authorization URL (from
order) - Returns
Authorization - the authorization
challenge
acme.challenge(kind as string, authz as Authorization)The challenge of a given type within an authorization ("http-01" / "dns-01").
kind- the challenge type to select
authz- an authorization from
authorization - Returns
Challenge - the matching challenge
- Throws
AcmeError - when no challenge of that type is offered
connect
acme.connect(directoryUrl as string, accountKey as bytes)Build a client from a CA directory URL and an account private key (PEM bytes, RSA or EC - the private half of crypto.keypair("rsa2048") or crypto.keypair("p256")). Fetches the directory to learn the CA's endpoints. Does not yet register.
directoryUrl- the CA's ACME directory URL
accountKey- the account private key, PEM-encoded
- Returns
Client - the configured client (kid empty until
register) - Throws
AcmeError - on a refused or malformed directory, or
http://to a non-loopback host (a socket failure is http's error, kind "net")
connectWith
acme.connectWith(
directoryUrl as string,
accountKey as bytes,
options as Options)connect with options: allowInsecureHttp lets the client talk plain http:// to a non-loopback test CA (RFC 8555 requires https; never set it for a real CA - anyone on the path could then redirect the requests).
directoryUrl- the CA's ACME directory URL
accountKey- the account private key, PEM-encoded
options- the connection options
- Returns
Client - the configured client (kid empty until
register) - Throws
AcmeError - as
connect
use crypto;
import "acme.j" as acme;
def key as bytes init crypto.keypair("p256").private;
def client as acme.Client init acme.connectWith(
"http://ca.lab.internal:4001/directory", $key,
acme.Options{allowInsecureHttp: true});dnsRecord
acme.dnsRecord(client as Client, token as string)The TXT record value for a DNS-01 challenge: the base64url (unpadded) SHA-256 of the key authorization. Publish it at _acme-challenge.<domain>.
client- the client
token- the challenge token
- Returns
string - the
_acme-challengeTXT value
downloadCertificate
acme.downloadCertificate(client as Client, order as Order)Download the issued certificate chain (PEM) for a valid order.
client- a registered client
order- a
validorder (with acertificateURL) - Returns
string - the PEM certificate chain (leaf first)
- Throws
AcmeError - when the order has no certificate yet
fetchOrder
acme.fetchOrder(client as Client, orderUrl as string)Fetch an order's current state by URL (a POST-as-GET). Use it to poll after finalize.
client- a registered client
orderUrl- the order URL
- Returns
Order - the order's current state
finalize
acme.finalize(
client as Client,
order as Order,
csrDer as bytes,
intervalMs as int,
maxTries as int)Finalize an order by submitting a CSR, then poll the order until it is valid (the certificate is ready) or invalid. csrDer is a DER PKCS#10 request (crypto.csr(certKey, domains)) for the same domains as the order.
client- a registered client
order- the (ready) order
csrDer- the DER CSR
intervalMs- milliseconds between status polls
maxTries- maximum number of polls
- Returns
Order - the finalized order (
certificateset when valid) - Throws
AcmeError - on a finalize error or if issuance does not complete
keyAuthorization
acme.keyAuthorization(client as Client, token as string)The key authorization for a challenge token: token.thumbprint, where the thumbprint is the RFC 7638 SHA-256 of the account key's JWK (base64url, unpadded). This is the exact body an HTTP-01 challenge serves at /.well-known/acme-challenge/<token>.
client- the client (its account key defines the thumbprint)
token- the challenge token
- Returns
string - the key authorization
order
acme.order(client as Client, domains as list of string)Create an order for one or more domains. The returned order is pending with one authorization URL per domain.
client- a registered client
domains- the domains to certify (the first is the CN)
- Returns
Order - the new order
- Throws
AcmeError - on an order error, or a response without the order URL
pollAuthorization
acme.pollAuthorization(
client as Client,
authzUrl as string,
intervalMs as int,
maxTries as int)Poll an authorization until it leaves pending, waiting intervalMs between attempts up to maxTries. It returns once the authorization is valid; one that settles otherwise (invalid, deactivated, expired, revoked) throws, with the failed challenge's error problem document (why the CA could not validate) as the error's problem, problemType and detail.
client- a registered client
authzUrl- the authorization URL
intervalMs- milliseconds between polls
maxTries- maximum number of polls
- Returns
Authorization - the
validauthorization - Throws
AcmeError - if it settles other than
valid, or is still pending aftermaxTries
register
acme.register(client as Client, email as string)Register (or look up) the account with the CA and return a client carrying the account URL (kid). Agrees to the CA's terms of service. Idempotent: the CA returns the existing account for a known key.
client- a client from
connect email- a contact email (empty for none)
- Returns
Client - a client with
kidset - Throws
AcmeError - on a registration error
Structs
AcmeError
A failed ACME operation. Every error of kind "acme" is one, so its fields are safe to read once the kind matches.
def struct AcmeError {
kind as string,
message as string,
status as int,
problemType as string,
detail as string,
retryAfter as int,
problem as json.Value
};kind- always
"acme" message- what failed, with the status and the start of the CA's answer
status- the HTTP status of the CA's answer; 0 when there was none
problemType- the problem document's
type(urn:ietf:params:acme:error:rateLimited,...:badCSR,...:unauthorized, ...), "" when the answer had none detail- the problem document's
detail, "" when none retryAfter- the
Retry-Afterheader in seconds (a rate limit, or a pending order); 0 when absent or given as a date problem- the whole problem document (with
subproblemsandidentifier); JSONnullwhen the answer was not one
Authorization
A domain-control authorization: the challenges the CA offers for it.
def struct Authorization {
domain as string,
status as string,
challenges as list of Challenge
};domain- the identifier being authorized
statuspending/valid/invalidchallenges- the offered challenges
Challenge
One challenge within an authorization.
def struct Challenge {
kind as string,
url as string,
token as string,
status as string
};kind- the challenge type (
http-01/dns-01/tls-alpn-01) url- the challenge URL (POST to it to accept / tell the CA it is ready)
token- the challenge token
statuspending/valid/invalid
Client
An ACME client: the CA's directory endpoints plus the account key and (once registered) the account URL used as the JWS kid. Value-semantic; register returns a copy with kid filled in.
def struct Client {
directory as string,
newNonce as string,
newAccount as string,
newOrder as string,
accountKey as bytes,
alg as string,
kid as string,
allowInsecureHttp as bool
};directory- the directory URL the client was built from
newNonce- the CA's new-nonce endpoint
newAccount- the CA's new-account endpoint
newOrder- the CA's new-order endpoint
accountKey- the account private key, PEM-encoded
alg- the JWS algorithm for the key:
RS256for RSA;ES256/ES384/ES512for EC, by curve kid- the account URL (empty until
register) allowInsecureHttp- allow
http://to a non-loopback host (set byconnectWith)
Options
Options for connectWith.
def struct Options {
allowInsecureHttp as bool
};allowInsecureHttp- allow
http://to a non-loopback test CA (default false)
Order
An ACME order for one or more identifiers (domains).
def struct Order {
url as string,
status as string,
authorizations as list of string,
finalize as string,
certificate as string
};url- the order URL (poll this for status)
statuspending/ready/processing/valid/invalidauthorizations- the per-identifier authorization URLs
finalize- the finalize URL (POST the CSR here)
certificate- the certificate URL (empty until issued)