What is an API token

What is an API token

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

What is an API token

When architecting authentication for a mobile application communicating with a PHP backend, distinguishing between session-based state and API tokens is crucial for security and scalability. The confusion often stems from the fact that both mechanisms rely on a server-side record to verify identity without requiring the client to resend credentials on every request. However, their underlying models and security implications differ significantly.

A traditional web session ties the authenticated user to a specific browser instance or device through a cookie. The server maintains state by storing a session ID in a database table alongside an array of user attributes (e.g., user_id, last_activity, ip_address). When a mobile app requests a new session, it receives this ID and typically stores it in local storage or secure keychain. While effective for web browsers that handle cookies automatically, this model presents challenges for mobile environments where network conditions are unstable or where the application needs to function across different devices.

In contrast, an API token is a long-lived credential issued by the server after initial authentication. Unlike a session ID which often has a short lifespan and is bound to a specific client IP or user agent string, an API token is designed to be stateless from the server's perspective regarding the token itself (though not necessarily stateless regarding the user). The mobile app requests a token using username and password, receives a unique opaque string back, and then includes that string in the Authorization header of every subsequent request.

The primary distinction lies in how they handle revocation and context. A session is inherently tied to the specific browser tab or device holding it; if you clear cookies on your phone, the session ends. An API token can be configured to last for months or even years (e.g., expires_at timestamp). This makes them ideal for mobile apps where users might switch devices frequently, but it introduces a significant security risk: if that token is compromised, an attacker gains access until expiration unless you have implemented granular revocation logic. To mitigate this, the token itself should ideally be scoped or associated with a specific application identifier rather than being a generic user credential.

Regarding the third point in your research questions, OAuth 2.0 is not merely a protocol for creating API tokens; it is an authorization framework that defines how those tokens are obtained and used. While simplified OAuth flows (like PKCE or implicit grants) result in the issuance of access tokens that look like API tokens, OAuth adds critical layers such as refresh tokens, scopes (limiting what data the token can access), and a standardized token lifecycle management process. Using raw API tokens without an OAuth wrapper bypasses these safety nets, making it harder to manage permissions or rotate credentials securely.

From a PHP implementation standpoint, you would typically create a dedicated endpoint that validates the username/password hash, then generates a random string (e.g., bin2hex(random_bytes(32))) and stores it in your database with an expiration timestamp. On subsequent requests, your middleware checks this token directly against the database rather than querying for an active session record. This decouples the authentication logic from the user's browsing habits, ensuring that valid credentials allow access regardless of the client's network state or cookie settings.

Our network