API Authentication
Token Based Authentication
Web API Security
Headless Authentication
Backend API Integration

Token based authentication in Web API without any user interface

Codemia Knowledge Hub ·

Introduction

If there is no user interface, token-based authentication is still normal. The difference is that the client is usually another service, script, daemon, or mobile/backend component, so the design should focus on machine-to-machine authentication rather than browser login screens.

Start with the Real Question: Who Is the Client

Without a UI, the client is usually one of these:

  • another backend service
  • a scheduled job
  • a CLI tool
  • a mobile app backend
  • an integration partner system

That matters because the right token flow depends on whether you are authenticating:

  • a machine
  • an end user
  • or both

For headless service-to-service communication, the most common pattern is a client-credentials style token flow or a signed service token issued by your identity provider.

A Typical Bearer Token Pattern

The API exposes protected endpoints. The client first obtains a token, then sends it in the Authorization header:

http
GET /orders/123 HTTP/1.1
Host: api.example.com
Authorization: Bearer eyJhbGciOi...

On the server side, the API validates:

  • signature
  • expiration
  • issuer
  • audience
  • scopes or roles

If validation passes, the request is authorized.

That is token-based authentication whether a browser is involved or not.

Machine-to-Machine Tokens Are Better Than Username and Password

If there is no UI, avoid designing an API around raw username and password submission on every request. A better model is:

  1. client authenticates once with its own credentials
  2. identity provider issues a short-lived access token
  3. API accepts that token on subsequent calls

This improves:

  • revocation
  • auditability
  • least-privilege scoping
  • secret rotation

It also decouples the API from hard-coded user-password flows that do not fit service integrations well.

Example Token Validation in an API

A simple Python example with JWT validation might look like this:

python
1import jwt
2
3SECRET = "replace-me"
4
5
6def validate_token(token):
7    payload = jwt.decode(
8        token,
9        SECRET,
10        algorithms=["HS256"],
11        audience="orders-api",
12    )
13    return payload
14
15
16claims = validate_token("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...")
17print(claims)

In production, many teams validate tokens issued by an external identity provider rather than minting and verifying them entirely inside the same API codebase.

The principle is the same either way: the API trusts a signed token, not a browser session.

Use Scopes or Roles, Not Just Identity

Authentication answers "who is this client?" Authorization answers "what is it allowed to do?"

A good token design includes claims that let the API enforce that distinction:

  • service identity
  • audience
  • scopes
  • roles
  • expiration

For example, one token may allow read:orders while another allows both reading and writing. That is much safer than one universal long-lived token with unlimited access.

Token Storage Still Matters

No UI does not mean no storage risk. Headless clients still need to protect:

  • client secrets
  • signing keys
  • refresh tokens
  • cached access tokens

For a backend service, that usually means:

  • secret manager
  • environment injection
  • short-lived tokens
  • no hard-coded secrets in source control

The absence of a login page does not reduce the need for secret hygiene.

When API Keys Are Enough

Not every headless integration needs OAuth-style flows. For simple internal or low-risk scenarios, an API key may be enough. But API keys are weaker than scoped bearer tokens because they are often:

  • long-lived
  • hard to rotate safely
  • coarse in permissions
  • harder to audit by principal

So if the integration is serious or long-lived, token-based authentication is usually the better design.

Common Pitfalls

  • Designing a headless API around username and password on every request.
  • Using one permanent token with broad access instead of short-lived scoped tokens.
  • Treating authentication and authorization as the same problem.
  • Storing client secrets in source control or container images.
  • Assuming "no UI" means security can be simpler than normal.

Summary

  • Token-based authentication works perfectly well for APIs with no user interface.
  • In headless systems, the client is usually another service or machine, so machine-to-machine token flows are the right model.
  • The API should validate token signature, issuer, audience, expiration, and scopes.
  • Short-lived scoped tokens are usually better than raw passwords or permanent API keys.
  • No UI changes the client type, not the need for strong authentication design.

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design