APIs from zero

Signing in — authentication

How an API knows who is asking — API keys, Basic, Bearer tokens, OAuth 2.0, JWT, cookies and certificates — what each is, when it's used, and the mistakes to avoid.

Most APIs don't answer just anyone. Before they do, they want to know who is asking — that's authentication ("authn") — and then whether that someone may do this — authorization ("authz"). A 401 means authentication failed; a 403 means authorization did (status codes).

There are a handful of common ways to sign in. You'll meet all of them.

API key

A long secret string the API gives you, sent with every request — usually in a header:

GET /forecast
X-API-Key: 9f3c2b7e41d8a6...

Simple, and common for public data APIs and server-to-server use. The key identifies an application, not a person. Anyone who has it can use it as you — so keep it out of code you share and out of URLs.

Basic authentication

A username and password, joined with : and encoded (Base64), in the Authorization header:

Authorization: Basic YW5uOnNlY3JldA==        ← "ann:secret"

The encoding is not encryption — anyone can decode it. Basic is only acceptable over https, and mostly appears on internal and older systems.

Bearer tokens

You sign in once (with a password, or through OAuth below), get a token back, and send that token with every request:

Authorization: Bearer eyJhbGciOiJIUzI1NiIs...

Bearer means "whoever bears this token is allowed" — so a token is as precious as a password. Tokens usually expire (after minutes or hours), which limits the damage if one leaks — and means your tests must get a new one now and then.

A typical flow — exactly what chaining automates:

  1. POST /login with a username and password → the answer contains "token": "eyJ…".
  2. Every request after that: Authorization: Bearer eyJ….

JWT: a token you can read

Many tokens are JWTs (JSON Web Tokens, said "jot"): three Base64 parts separated by dots — header.payload.signature. The payload is JSON you can decode and read:

{ "sub": "user-42", "name": "Ann", "role": "admin", "exp": 1792400000 }

sub is who, exp is when it expires (in seconds since 1970). The signature lets the server check nobody changed it. Readable doesn't mean editable: change "role": "user" to "admin" and the signature no longer matches — the server refuses it. (Testing that it does refuse is a good security test.)

OAuth 2.0: tokens from a third party

When you click "Sign in with Google", or an app asks "Allow this app to read your calendar?", that's OAuth 2.0. Instead of giving an app your password, a separate authorization server gives the app a token with limited permission (its scopes, like calendar.read).

The ways to get the token are called grants. The two you'll meet most in API testing:

  • Client credentials — server to server, no person: the app sends its client id and client secret to the token address and gets a token. Common for internal APIs.
  • Authorization code (with PKCE) — a person signs in through a browser, the app gets a code, and swaps it for a token. Used whenever a real user is involved.

Others exist: password (old, being retired), refresh token (swap an expiring token for a new one), JWT bearer and SAML bearer (enterprise systems like Salesforce and SAP). Tokens expire; a good tool renews them by itself — including during a long load test.

Cookies and sessions

Websites usually sign you in once and the server sets a cookie (Set-Cookie: session=abc…); your browser sends it back with every request (Cookie: session=abc…). Some APIs work the same way. A tool with a cookie jar keeps and returns cookies like a browser, so a login request signs in the requests after it.

Signatures and certificates

For higher security, some APIs want more than a secret in a header:

  • Signed requests — every request is signed with a secret key, over its method, address, time and body: AWS Signature, OAuth 1.0 (NetSuite and others), HMAC schemes. A copied request can't be reused later or changed.
  • Client certificates (mutual TLS) — your computer proves who it is with a certificate during the encrypted connection, before any request is sent. Common in banking and between companies.
  • Digest and NTLM — older challenge-and-answer sign-ins, still found inside companies (NTLM is Windows' own).

Keeping secrets secret

  • Put secrets in variables marked secret, not in the request itself — and never commit them to git.
  • Use test accounts with test permissions, not your own admin account.
  • A token in a screenshot or a chat message is a leaked token. Revoke it.

Key takeaways

  • Authentication is who are you (fails with 401); authorization is may you (fails with 403).
  • The common kinds: API key, Basic, Bearer token (often a JWT), OAuth 2.0, cookies, and for high security signatures and client certificates.
  • A token is as good as a password: keep it in secret variables, out of git, chats and screenshots.

In the app: Auth · OAuth 2.0 · Folders: sign in once · Cookies · Client certificates

Next: Kinds of APIs

A question these lessons don’t answer? Write to support@apitestcenter.com — we add lessons for the questions people ask.