API Authentication Basics: Bearer Tokens, HTTPS, and Keeping Keys Out of Client Code
By Nimana · Last updated November 28, 2026 · 4 min read
Learn how API authentication works: Bearer tokens, HTTPS, project headers, and why keys never belong in browser or mobile code, with working examples.
Why This Matters
When your app sends a message, creates a user, or reads data through an API, the service needs to know that the request really comes from you. Authentication is how it finds out. Get it wrong, and one of two things happens: your own requests fail with confusing errors, or someone else's requests get through using your credentials.
The basics are simple, and they are worth knowing properly.
What Authentication Does
Authentication answers one question: who are you? Most modern APIs use a secret value called an API key. You send it with every request, and the service looks it up to find your account.
Do not confuse this with authorisation, which answers a different question: what are you allowed to do? A good API combines both. Your key identifies you, and rules decide what that key may access.
The Bearer Token Pattern
The most common way to send an API key is in the HTTP Authorization header, as a Bearer token:
Authorization: Bearer YOUR_API_KEY
The word "Bearer" tells the server that whoever holds, or "bears", this token is allowed in. That is exactly why it must stay secret. Anyone who has the key can use it.
Seven Rules for Authenticating Safely
1. Always use HTTPS
Requests over plain HTTP are sent as readable text, so anyone on the network path could read your key. HTTPS encrypts the connection. Many APIs refuse plain HTTP completely, and you should treat it that way too.
2. Keep keys in environment variables
Never type a key directly into your source code. Load it from an environment variable or a secrets manager, so it stays out of your repository.
const apiKey = process.env.API_KEY;
3. Never put a key in client-side code
Anything that runs in a browser or a mobile app can be inspected by the user. A key embedded in a React, Vue, or mobile app is a key you have published. Keep the key on your server, and let your front end talk to your own backend.
4. Expect a key to be shown once
Good services do not store your key in readable form, so they can show it to you only when it is created. Save it somewhere safe straight away. If you lose it, you create a new one and revoke the old.
5. Use a different key for each job
A separate key for each application or environment means a leak affects one thing, and you can revoke it without breaking everything else.
6. Handle the 401 properly
A 401 Unauthorized response means the key is missing, wrong, or revoked. Do not retry it blindly. Check the header, the key, and whether it has been revoked.
7. Revoke at the first sign of trouble
If a key might have leaked, revoke it immediately. Create a replacement, update your servers, and look through your logs for anything you did not send.
Common Mistakes
- Committing a key to a public repository
- Using a key in a browser or mobile app
- Sharing one key across every project
- Sending requests over HTTP
- Retrying a
401endlessly
How SendAPI Helps
SendAPI authenticates every request with a Bearer token in the Authorization header. A few details from the docs:
- HTTPS only: all API requests must be made over HTTPS, and calls made over plain HTTP fail.
- Clear failure: a missing, invalid, or revoked key returns
401 Unauthorized. - Keys are shown once: SendAPI stores only a SHA-256 hash of your key, so it cannot be displayed again. The API Keys page shows a prefix, such as
sk_live_1pOy…, so you can tell your keys apart, but the prefix is not a usable credential. - Key type: all keys currently start with
sk_live_, and they run real production traffic. The docs say never to commit a key to GitHub or embed it in client-side code such as React, Vue, or mobile apps. - SDKs attach the header for you: with the Node.js SDK, you create the client with your key and it adds the authorization header automatically.
import { SendAPI } from '@sendapi/node';
const client = new SendAPI(process.env.SENDAPI_KEY);
- Multiple projects: if your account has more than one project, add an
X-Project-Idheader to choose one. If you leave it out, requests go to your Default Project. You can list your projects withGET /v1/team/projects.
Authorization: Bearer sk_live_YOUR_API_KEY
X-Project-Id: 14
All of this comes with WhatsApp, SMS, OTP, and email under one API from $9.99 a month.