Testing APIs that need client certificates (mutual TLS)
Most APIs authenticate the client with a token in a header. Some go further and authenticate it at the connection level, before a single HTTP byte is exchanged: the client has to present a certificate the server trusts. That's mutual TLS (mTLS), and you'll meet it in banking and open banking APIs, B2B integrations, payment networks and service meshes.
How mTLS works, briefly
In normal TLS, only the server shows a certificate, and your client checks it. In mutual TLS:
- The server shows its certificate, and the client checks it against a certificate authority (CA) it trusts.
- The server asks for a client certificate.
- The client sends its certificate and proves it holds the matching private key.
- The server checks the client certificate against the CA it trusts.
What you need
- A client certificate, in PEM format (
client.crt). - Its private key, in PEM (
client.key), possibly encrypted with a passphrase. - Often, the server's CA certificate (
ca.crt), if the server uses a private CA your system doesn't trust.
Your API provider gives you these, or you generate a key and a certificate signing request (CSR) and they sign it.
Call an mTLS API from the CLI
requestarium run "Get accounts" --env Sandbox \
--cert ./certs/client.crt --key ./certs/client.keyIf the key is encrypted:
requestarium run "Get accounts" --env Sandbox \
--cert ./certs/client.crt --key ./certs/client.key --key-passphrase "$KEY_PASSPHRASE"If the server's certificate is issued by a private CA:
requestarium run-collection "Open Banking" --env Sandbox \
--cert ./certs/client.crt --key ./certs/client.key --ca-cert ./certs/ca.crt--ca-cert adds to the trusted authorities; it doesn't replace the system's. All three flags work with run and run-collection.
In CI
Store the certificate and key as CI secrets, write them to files in the job, and remove them afterwards. In GitHub Actions:
- name: API tests over mTLS
env:
REQUESTARIUM_TOKEN: ${{ secrets.REQUESTARIUM_TOKEN }}
CLIENT_CERT: ${{ secrets.MTLS_CLIENT_CERT }}
CLIENT_KEY: ${{ secrets.MTLS_CLIENT_KEY }}
run: |
umask 077
printf '%s' "$CLIENT_CERT" > client.crt
printf '%s' "$CLIENT_KEY" > client.key
requestarium run-collection "Open Banking" --env Sandbox --cert client.crt --key client.key --junit results.xml
rm -f client.crt client.keyDon't turn off verification
Troubleshooting
| Symptom | Usual cause |
|---|---|
| Connection reset, or a handshake failure, right after connecting | No client certificate sent, or the server doesn't trust its issuer |
| "unable to verify the first certificate" / self-signed in chain | The server uses a private CA: pass it with --ca-cert |
| "bad decrypt" or a key error | Wrong passphrase, or the key doesn't match the certificate |
| Works with curl, not elsewhere | Check the certificate includes the full chain the server expects |
To check that a key matches a certificate, compare their public keys:
openssl x509 -noout -pubkey -in client.crt | openssl sha256
openssl pkey -pubout -in client.key | openssl sha256The two hashes must be identical.