Cloudflare
API Shield · mTLS

Only clients with a certificate reach your API

A common scenario: your API is consumed by your customers' systems, and each one must present a client certificate. This page shows who does what, the rules to write, and a test notebook you can run against mtls-api-lab. Statements link to the public Cloudflare documentation.

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.
Enabling mTLS on a hostname does not block anyone by itself. In this lab, a request without a certificate to 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).

StepCustomerYou (API owner)Cloudflare
Generate key and CSRDoes it and keeps the key
Issue the certificateSends the CSRSigns it: Cloudflare API (managed CA) or your own CA (BYOCA)Signs, when you use the managed CA
Install the certificateInstalls it in the HTTP client
Check on every requestPresents the certificateValidates at the edge
Block requests without a valid certificateCreates the WAF ruleApplies the rule
Revoke a certificateRequests the revocationBlocks 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-prod and acme-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

Generate a private key and a CSR (the key stays on the customer's machine)
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)

Sign a CSR with the managed CA
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.

Rule 1: require a valid certificate from your CA, on one path
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>")
Get the issuer SKI from a client certificate
openssl x509 -noout -ext authorityKeyIdentifier -in client.pem | tail -n1 | tr -d ': '
Rule 2: reject revoked certificates (managed CA only)
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.
Place these rules above any Skip rule for the same host. A Skip placed first can stop later rules from running.

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.

#HeaderOperationExpressionValue
1Client-CertRemovetrue
2Client-Cert-ChainRemovetrue
3Client-CertSet dynamiccf.tls_client_auth.cert_verified and not cf.tls_client_auth.cert_revoked and not cf.tls_client_auth.cert_rfc9440_too_largecf.tls_client_auth.cert_rfc9440
4Client-Cert-ChainSet dynamiccf.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_largecf.tls_client_auth.cert_chain_rfc9440
The certificate fields are filled even when validation fails, and a client can send its own 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.

IDRequestExpected
M-1Valid client certificate on /api/mtls/whoami200 and the response shows the certificate (with the forwarding rules)
M-2No certificate on /api/mtls/whoami403 at the edge once the rule is active
M-3A self-signed certificate not issued by your CA403: it is not verified
M-4A revoked certificate403 with the revocation rule. The documentation does not state how long revocation takes to apply.
M-5No certificate on /healthz200: the path is outside the rule
M-1 to M-5
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/healthz
Evidence: Security Events show the rule name and action. The origin log has no trace of a request blocked at the edge.
Lab status: the rules and certificate for this host are being set up, so results are not published here yet. Observed so far, with mTLS enabled on the host and no rule: a request without a certificate completed the handshake and returned 200.

6 Renewal and lifecycle

What Cloudflare does for you, and what you build.

Cloudflare providesYou build
Validation at the edge on every requestThe 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, statusExpiry monitoring and customer notifications
The documentation reviewed does not describe automatic renewal of client certificates. Expiry alerts exist for certificates uploaded through BYOCA and for Access mTLS, not for certificates issued by the managed CA. Plan the renewal flow as part of the solution.
  • 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 CABYOCA (your CA)
Who signsCloudflare, on each API callYou, with your PKI tooling
Limit100 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
RevocationSupported, and checked by a ruleCloudflare does not check CRLs for uploaded CAs
FitsA small, stable set of clientsHigh volume, high churn, or an existing PKI
Before promising a number to a customer, confirm the certificate limit applied to the zone with your account team.

8 Safe rollout on an existing API

  1. Pilot on a separate hostname that points to the same origin.
  2. Put the rule in Log and see who arrives without a certificate.
  3. Issue certificates to customers and let them install them.
  4. Switch the rule to Block, one endpoint at a time.
The mTLS rule is scoped by host and path, so you can protect one endpoint and leave the others open. The notebook above tests exactly that with /healthz.