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

Secure a GraphQL API with SSL/TLS

Secure a GraphQL API with SSL/TLS

A GraphQL API exposes a single endpoint, usually /graphql. That makes transport security simple in one way: if you get that one URL right, every query and mutation is covered. It also means a mistake there exposes everything, including auth tokens in headers and whatever your resolvers return. This guide walks through the TLS side of the job. Authorization and query cost limits are separate topics.

GraphQL SSL: why the traffic needs TLS

Every GraphQL request is a POST (or sometimes a GET) carrying a query string and variables. Variables often hold passwords, tokens or personal data. Responses can return nested records in a single payload. Without TLS, anyone on the network path can read both directions and alter them. Browsers also block plain http:// or ws:// calls from an HTTPS page as mixed content, so a half-secured setup tends to break in production anyway.

Step 1: Get a certificate for the API hostname

Use a hostname such as api.example.com and issue a certificate for exactly that name, or a wildcard if you run many services. A free Let's Encrypt certificate is fine for a production API. If you have not issued one yet, see getting a free certificate with Certbot. Keep the private key readable only by the account that runs your web server, and read how to protect SSL private keys for storage advice.

GraphQL SSL, step 2: terminate at a proxy

Most teams put Nginx, HAProxy or a cloud load balancer in front of Apollo Server, GraphQL Yoga, Hasura or a similar server and let the proxy handle TLS. The GraphQL process then listens on localhost. A minimal Nginx server block looks like this:

server {
    listen 443 ssl;
    server_name api.example.com;

    ssl_certificate     /etc/ssl/api.example.com/fullchain.pem;
    ssl_certificate_key /etc/ssl/api.example.com/privkey.pem;
    ssl_protocols       TLSv1.2 TLSv1.3;

    location /graphql {
        proxy_pass http://127.0.0.1:4000;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

server {
    listen 80;
    server_name api.example.com;
    return 301 https://$host$request_uri;
}

The second block redirects plain HTTP. For an API, consider whether a redirect is wise at all: a client that sends a POST over HTTP has already leaked its payload before it is redirected. Some teams close port 80 for API hostnames and let clients fail loudly. Either way, never serve GraphQL on both schemes.

Step 3: Secure subscriptions with wss

GraphQL subscriptions run over WebSockets. Clients must use wss://, not ws://. The proxy needs to pass the upgrade headers through, or the connection will fail with a 400 or 426 style error:

location /graphql {
    proxy_pass http://127.0.0.1:4000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 3600s;
}

Long-lived sockets are cut by the proxy read timeout, so set it to match how long you expect a subscription to stay open. Our Enable HTTPS for WebSockets covers this in more depth.

Step 4: Add HSTS and strict transport settings

Once HTTPS works for every client, send a Strict-Transport-Security header so browsers refuse to downgrade. Start with a short max-age and raise it when you are sure. The full process is in how to enable HSTS. Only add includeSubDomains if every subdomain supports HTTPS.

Step 5: Keep secrets out of URLs

  • Send tokens in the Authorization header, never in a query string. URLs end up in logs, proxies and browser history.
  • Avoid GET for mutations. Use POST so variables stay in the body.
  • For WebSocket connections, pass credentials in the connection_init payload instead of the URL where your library allows it.
  • Restrict CORS to the exact origins that need the API.

Step 6: Test the setup

Run a query over HTTPS and check the response, then confirm HTTP behaves the way you intended:

curl -i https://api.example.com/graphql \
  -H "Content-Type: application/json" \
  -d '{"query":"{ __typename }"}'

curl -i http://api.example.com/graphql

openssl s_client -connect api.example.com:443 \
  -servername api.example.com -brief

The __typename query is valid on every GraphQL server and returns almost nothing, so it is a safe probe. For the certificate chain, expiry and protocol versions, the free SSL chain checker and the OpenSSL commands in testing SSL configuration with OpenSSL give a quick answer.

Quick checklist

CheckExpected result
HTTPS request to /graphql200 with a valid JSON body
Certificate hostnameMatches the API hostname
TLS versions1.2 and 1.3 only
wss:// subscriptionConnects and stays open
HTTP requestRefused or redirected, by design
HSTS headerPresent on HTTPS responses

Renewal

A certificate that expires takes the whole API down for every mobile app and integration at once. Automate renewal and reload the proxy after each renewal; see automating renewal with Certbot. Also add an expiry alert so a failed renewal is noticed days before it matters.

The certificate paths in the examples are shortened for readability. With Certbot the real files sit under /etc/letsencrypt/live/ in a folder named after your domain. Point the config at those files, or at copies placed by a deploy hook.

Related reading: mTLS for APIs, securing an API gateway with TLS and Best TLS Configuration for Nginx and Apache.

Frequently Asked Questions

Is TLS enough to secure a GraphQL API?

No. TLS protects the connection only. Authorization, query depth limits and rate limiting are separate controls, and a GraphQL endpoint needs all of them.

Where should TLS terminate?

At a reverse proxy in front of the app, so the certificate and its renewal live in one place. Terminate inside the app only when nothing else can do it.

Do subscriptions need anything extra?

Yes. They run over WebSocket, so the endpoint has to be wss:// and the proxy has to pass the upgrade headers through.

🔒 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