ssl-guides 2026-12-02 ⏱ 4 min read

Enable HTTPS for REST APIs

Enable HTTPS for REST APIs

REST APIs carry tokens, personal data and business logic, so they should never run over plain HTTP. Enabling HTTPS means installing a trusted certificate, setting safe TLS options and making sure clients cannot be downgraded. The steps are short, but API clients are less forgiving than browsers, so testing matters.

Why APIs fail where browsers succeed

Desktop browsers can fetch a missing intermediate certificate on their own. Python scripts, Node.js, Go and Java clients, mobile apps and webhook senders generally cannot. They reject an incomplete chain with errors such as UNABLE_TO_VERIFY_LEAF_SIGNATURE or SSLCertVerificationError. Always serve the full chain file.

Step 1: Get a certificate

Use a free certificate for the API hostname. Our guide to getting a free SSL certificate for an API covers issuance. Certificates from Let's Encrypt come with fullchain.pem and privkey.pem.

HTTPS for REST APIs, step 2: configure the server

An Nginx example with TLS limited to modern versions and a proxy to the API process:

server {
    listen 443 ssl;
    http2 on;
    server_name api.example.com;
    ssl_certificate /etc/ssl/api/fullchain.pem;
    ssl_certificate_key /etc/ssl/api/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;

    location /v1/ {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $host;
    }
}

The http2 on directive suits recent Nginx versions. Older versions put http2 on the listen line, so check your version's documentation.

Step 3: Handle plain HTTP

Decide what port 80 should do. For websites, a 301 redirect is standard. For APIs, a redirect can leak secrets, because a client may already have sent a token in clear text before the redirect arrives. The safer pattern for an API is to refuse plain HTTP, return an error and fix the client. Do not serve API traffic over port 80 at all.

server {
    listen 80;
    server_name api.example.com;
    location /.well-known/acme-challenge/ {
        root /var/www/acme;
    }
    location / { return 403; }
}

Step 4: Add HSTS for browser clients

If browsers call the API, send Strict-Transport-Security from the HTTPS responses. Start with a short lifetime and raise it once everything is stable, since HSTS is hard to undo. See our security headers guide.

HTTPS for REST APIs, step 5: mutual TLS

For machine-to-machine traffic, you can require clients to present a certificate signed by your own CA. The server then authenticates the caller at the TLS layer:

    ssl_client_certificate /etc/ssl/api/client-ca.pem;
    ssl_verify_client on;

mTLS is useful between services or partners. It adds certificate management for every client, so use it where the extra control is worth it. API keys and tokens over HTTPS are enough for many public APIs.

Step 6: Test

CheckCommand or toolExpected
Chain completeopenssl s_client -connect host:443Verify return code 0
TLS versionsopenssl s_client with -tls1_3Connects
Plain HTTP refusedcurl -i http://api.example.com/v1/Not served
Real client worksCall from the production runtime200 with no certificate error

Also run the SSL checker and read fixing chain errors if it reports a problem.

Renewal

Reload the server after every renewal, and monitor the served certificate from outside. See renewing without downtime.

Common mistakes

  • Leaf certificate only. Browsers hide the problem, while API clients fail.
  • No reload after renewal. New files on disk do nothing until the server reloads its TLS context.
  • Stale AAAA record. Half of the clients reach the wrong machine when an old IPv6 record remains.
  • Terminating TLS at a proxy but trusting forwarded headers blindly. Make sure the backend only accepts traffic from the proxy.
  • Disabling verification in client code. Developers sometimes turn off certificate checks to make an error go away. That removes the protection HTTPS gives, so fix the chain instead.

Key and certificate formats

Some platforms want PKCS#12 or JKS files rather than PEM. You can convert with OpenSSL, for example openssl pkcs12 -export, and our guide to converting PEM to a Java keystore shows the steps for Java services.

Key takeaways

  • Serve the full chain so non-browser clients can verify it.
  • Allow only TLS 1.2 and 1.3.
  • Refuse plain HTTP for APIs rather than redirecting it.
  • Consider mTLS for service-to-service calls, and test with real clients.

Related reading: mTLS for APIs, securing an API gateway with TLS and HTTPS for WebSockets.

Frequently Asked Questions

Why does curl fail when the browser works?

curl does not fetch a missing intermediate. Serve the full chain from the server and the error goes away.

Do I need mutual TLS for an API?

Only when the client has to prove its identity with a certificate. A token over TLS covers most public APIs.

How do I stop clients using plain HTTP?

Redirect at the server and add HSTS. For machine clients, document the https base URL and retire the http one.

🔒 Test Your Website SSL Certificate Now
Run Free SSL Check →

✅ Post-Installation Verification & SEO Checklist

After applying the steps above, run through this 5-point checklist to prevent browser warnings and protect your Google SEO rankings:

  • Verify full CA chain trust and TLS 1.3 support using our Live SSL Checker.
  • Scan for Mixed Content HTTP links and check HSTS/CSP headers on Security Headers Checker.
  • Enforce a permanent 301 HTTP-to-HTTPS redirect with our HTTPS Redirect Generator.
  • Confirm both root and www versions resolve cleanly to one canonical HTTPS URL.
  • Verify your HTTPS property and sitemap.xml inside Google Search Console and Bing Webmaster Tools.
Technically Reviewed by TryFreeSSL Security Team
Last updated: 2026-12-05
About Us →

Free SSL Certificates, Live SSL Checker & CSR Generator

Issue trusted Let's Encrypt SSL certificates in 60 seconds with zero server setup.

Related Articles

🤖 TryFreeSSL AI Assistant