1 The scenario
Your customer calls your API from a server. You want to be sure the caller is one of your customers before the request reaches your origin.
- Each customer holds a client certificate and its private key, installed in the HTTP client they use to call you.
- The Cloudflare edge checks the certificate on every connection. The customer does not talk to Cloudflare to validate it, and does not need a Cloudflare account.
- Cloudflare validates the certificate against the certificate authority (CA) set at account level: the Cloudflare-managed CA by default, or your own CA (BYOCA, Enterprise).
- The result is stored in
cf.tls_client_auth.cert_verified, and your WAF rule decides what to do with it.
mtls-api-lab still returned 200 before any rule existed. The block comes from the WAF rule in section 4.2 Who does what
The private key is created by the client and never leaves it. You only receive a certificate signing request (CSR).
| Step | Customer | You (API owner) | Cloudflare |
|---|---|---|---|
| Generate key and CSR | Does it and keeps the key | ||
| Issue the certificate | Sends the CSR | Signs it: Cloudflare API (managed CA) or your own CA (BYOCA) | Signs, when you use the managed CA |
| Install the certificate | Installs it in the HTTP client | ||
| Check on every request | Presents the certificate | Validates at the edge | |
| Block requests without a valid certificate | Creates the WAF rule | Applies the rule | |
| Revoke a certificate | Requests the revocation | Blocks it, managed CA only |
- One certificate per device is the documented recommendation. For server-to-server customers, one per customer and environment (for example
acme-prodandacme-sandbox) is a common starting point. That is a practical choice, not a Cloudflare rule. - Do not use the option that generates the private key in the dashboard for customers: the key would pass through your browser. Ask for a CSR instead.
3 Issue a certificate
The customer creates a key and a CSR, and you sign it.
Customer side
openssl req -new -newkey rsa:2048 -nodes -keyout client.key -out client.csr -subj '/O=Customer Name/CN=customer-prod'The subject fields (O, CN) are written into the certificate, so they work as a label to tell customers apart.
Your side: dashboard
- SSL/TLS → Client Certificates → Add Certificate. The Cloudflare-managed CA is the default.
- Choose Use my private key and CSR and paste the CSR.
- Pick a Certificate Validity. The default can be long (10 years in this lab); shorter is better for most customers.
- Copy the certificate immediately, it is not shown again, and send it to the customer.
Your side: API (what an onboarding portal would call)
curl "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/client_certificates" \
--header "Authorization: Bearer $TOKEN" --header "Content-Type: application/json" \
--data "{\"validity_days\": 365, \"csr\": \"$(awk 'NF {printf "%s\\n", $0}' client.csr)\"}"The response includes the certificate, its serial number, fingerprint and expiry. The token that can create certificates is a high-value secret: keep it only in the issuing service, with the narrowest scope and a short life.
Enable the hostname
- SSL/TLS → Client Certificates → Hosts → Edit: type the host label only (the domain is appended). A CNAME is not enough: enable the exact hostname that must be checked.
- The origin certificate must match the API hostname. Wildcard origin certificates are documented as not supported for mTLS. This lab tests it with a wildcard; see the notebook result.
4 Rules
Block requests that do not present a valid certificate, scoped to the endpoint you want to protect. Start in Log.
http.host eq "mtls-api-lab.santosedge.com" and
http.request.uri.path eq "/api/mtls/whoami" and
not (cf.tls_client_auth.cert_verified and cf.tls_client_auth.cert_issuer_ski eq "<ISSUER_SKI>")openssl x509 -noout -ext authorityKeyIdentifier -in client.pem | tail -n1 | tr -d ': 'http.host eq "mtls-api-lab.santosedge.com" and cf.tls_client_auth.cert_revoked- Checking the issuer SKI is recommended. Without it, any valid certificate is accepted whichever CA issued it.
- Revoked certificates are blocked only if a rule checks for them. The check does not apply to BYOCA: Cloudflare does not check CRLs for uploaded CAs.
- The mTLS rule applies only to requests that match its host and path. Other paths on the same host stay open, which is how you protect one endpoint and not the whole API.
Show the certificate to your origin (optional)
To identify the customer in your application, forward the certificate as the RFC 9440 Client-Cert header with four Request Header Transform Rules. The two Remove rules must come first and match every request, so a client cannot inject its own header.
| # | Header | Operation | Expression | Value |
|---|---|---|---|---|
| 1 | Client-Cert | Remove | true | |
| 2 | Client-Cert-Chain | Remove | true | |
| 3 | Client-Cert | Set dynamic | cf.tls_client_auth.cert_verified and not cf.tls_client_auth.cert_revoked and not cf.tls_client_auth.cert_rfc9440_too_large | cf.tls_client_auth.cert_rfc9440 |
| 4 | Client-Cert-Chain | Set dynamic | cf.tls_client_auth.cert_verified and not cf.tls_client_auth.cert_revoked and cf.tls_client_auth.cert_chain_rfc9440 ne "" and not cf.tls_client_auth.cert_chain_rfc9440_too_large | cf.tls_client_auth.cert_chain_rfc9440 |
Client-Cert header. Only trust the header if the origin accepts traffic from Cloudflare alone (for example Authenticated Origin Pulls or an IP allowlist) and the Remove rules are in place.5 Test notebook
Run against https://mtls-api-lab.santosedge.com. The protected path is /api/mtls/whoami; /healthz is outside the rule.
| ID | Request | Expected |
|---|---|---|
| M-1 | Valid client certificate on /api/mtls/whoami | 200 and the response shows the certificate (with the forwarding rules) |
| M-2 | No certificate on /api/mtls/whoami | 403 at the edge once the rule is active |
| M-3 | A self-signed certificate not issued by your CA | 403: it is not verified |
| M-4 | A revoked certificate | 403 with the revocation rule. The documentation does not state how long revocation takes to apply. |
| M-5 | No certificate on /healthz | 200: the path is outside the rule |
H=https://mtls-api-lab.santosedge.com
curl -s -o /dev/null -w "M-1 %{http_code}\n" --cert client.pem --key client.key $H/api/mtls/whoami
curl -s -o /dev/null -w "M-2 %{http_code}\n" $H/api/mtls/whoami
openssl req -x509 -newkey rsa:2048 -nodes -keyout fake.key -out fake.pem -days 1 -subj '/CN=not-your-ca' 2>/dev/null
curl -s -o /dev/null -w "M-3 %{http_code}\n" --cert fake.pem --key fake.key $H/api/mtls/whoami
curl -s -o /dev/null -w "M-5 %{http_code}\n" $H/healthz6 Renewal and lifecycle
What Cloudflare does for you, and what you build.
| Cloudflare provides | You build |
|---|---|
| Validation at the edge on every request | The onboarding flow: receive a CSR, authenticate the customer, issue the certificate |
| An API to issue and revoke certificates (managed CA) | Renewal: issue a new certificate before the old one expires, keep both valid for a short overlap, then revoke the old one |
Certificate details in the API: serial, fingerprint, expires_on, status | Expiry monitoring and customer notifications |
- During an overlap, both the old and the new certificate count toward your quota.
- A revoked certificate stays listed and can be restored; revoking frees its quota slot.
- The customer's HTTP client must load the new certificate. Plan for that: an agent, an SDK or the customer's secret store.
7 Choosing a CA at scale
The same enforcement works with either CA. What changes is who issues and who stores the certificates.
| Cloudflare-managed CA | BYOCA (your CA) | |
|---|---|---|
| Who signs | Cloudflare, on each API call | You, with your PKI tooling |
| Limit | 100 active certificates per zone by default. Enterprise with API Shield: 100,000 per zone once the entitlement is applied. | Up to 5 CAs per account, no per-certificate limit |
| Revocation | Supported, and checked by a rule | Cloudflare does not check CRLs for uploaded CAs |
| Fits | A small, stable set of clients | High volume, high churn, or an existing PKI |
8 Safe rollout on an existing API
- Pilot on a separate hostname that points to the same origin.
- Put the rule in Log and see who arrives without a certificate.
- Issue certificates to customers and let them install them.
- Switch the rule to Block, one endpoint at a time.
/healthz.