API Testing · Testing APIs by Hand

Testing API Authentication: API Keys, Tokens and OAuth

Lesson 5 of 7 · 9 min read

Almost every real API checks who is calling (authentication) and what they are allowed to do (authorisation). These checks are where many serious bugs hide.

The common types

TypeHow it is sentWhere you see it
API keyA header like x-api-key: abc123 or a query parameterPublic APIs, server-to-server
Basic authAuthorization: Basic plus base64 of user:passwordOld or internal systems
Bearer token (often a JWT)Authorization: Bearer eyJ...Most web and mobile apps
OAuth 2.0You get a token from an auth server first, then send it as a bearer token"Log in with Google", third-party access

The usual token flow

  1. POST /auth/login with email and password.
  2. The response contains a token, for example {"token": "eyJ..."}.
  3. Every next request sends Authorization: Bearer <token>.
  4. The token expires after some time and you log in again or use a refresh token.

In Postman, save the token in a variable after login, then set the collection's Authorization tab to "Bearer Token" with {{token}}. Every request in the collection uses it.

What is inside a JWT

A JWT has three parts separated by dots: header, payload and signature. The payload is only base64-encoded, not encrypted, so anyone can read it at jwt.io. Check that it contains no secrets, like passwords, and that it has an expiry (exp).

Authentication test cases

TestExpected
Correct login200 and a token
Wrong password401 with a message that does not say which field was wrong
Request with no token401
Token with one character changed401
Expired token401
Token after logout401, if the API supports logout
Many wrong passwords in a rowLockout or 429, per the requirements

Authorisation test cases

  • Log in as user A and request user B's record by id. Expect 403 or 404, never B's data.
  • Log in as a normal user and call an admin endpoint. Expect 403.
  • Try to change your own role by sending "role": "admin" in a profile update. It must be ignored.
Keep secrets out of tests: never commit real passwords, API keys or tokens to Git. Read them from environment variables, and keep Postman secrets in the "current value" column, which is not shared.
All 7 lessons in API Testing
  1. What Is API Testing?
  2. HTTP Basics for Testers: Methods, Status Codes, Headers
  3. API Testing with Postman, Step by Step
  4. What to Test in an API: A Complete Checklist
  5. Testing API Authentication: API Keys, Tokens and OAuth
  6. Automate API Tests with REST Assured (Java)
  7. API Test Framework Structure and CI