Subscribe to the Non-Human & AI Identity Journal

How should security teams choose between API keys, Device Flow, and Client Credentials for CLI apps?

Choose by actor type and operating context. Use API keys only for narrowly scoped machine automation, Device Flow for interactive users who need SSO and MFA, and Client Credentials for backend services that require short-lived tokens. The deciding factors are attribution, revocation, and whether the environment can support a browser-based approval step.

Why This Matters for Security Teams

For CLI apps, the credential choice is really a question of who or what is acting, how much trust the environment can bear, and whether attribution matters after the fact. api key are simple, but they are also static Secrets and therefore harder to scope, rotate, and attribute. Client Credentials align better with backend workloads because short-lived tokens reduce standing exposure. Device Flow is different again: it preserves user identity, MFA, and SSO for humans operating from a terminal. The wrong choice usually shows up as shared credentials, unclear audit trails, or secrets left valid long after the workflow changed. Guidance from OWASP Non-Human Identity Top 10 and NIST SP 800-63 Digital Identity Guidelines both reinforce that authentication strength is not the same as operational fit. NHIMG’s Guide to the Secret Sprawl Challenge shows why static secrets become a systemic risk once they spread across build scripts, terminals, and shared docs. In practice, many security teams discover the wrong credential model only after a terminal-based workflow has already become part of production operations.

How It Works in Practice

Start by classifying the CLI app as either a human-operated tool, a backend service, or a narrowly scoped automation job. For humans, Device Flow is usually the safest default when the CLI needs SSO, MFA, and clear user attribution. It lets the operator authenticate in a browser while the terminal receives a token, which is much better than embedding a password or long-lived API key. For backend services, Client Credentials is usually the cleaner pattern because the app exchanges its workload identity for a short-lived access token. That makes it easier to enforce JIT access, revoke quickly, and tie activity to a service principal rather than a person.

API keys should be reserved for constrained automation where the environment cannot support an interactive browser step and the risk can be bounded tightly. Even then, current guidance suggests treating keys as transitional, not permanent. Pair them with narrow scopes, rapid rotation, and strong secret handling. NHIMG’s Ultimate Guide to NHIs — Static vs Dynamic Secrets explains why dynamic credentials reduce blast radius compared with static ones, and the 52 NHI Breaches Analysis shows how often exposed identities become persistence points when revocation lags. A practical control set looks like this:

  • Use Device Flow when a real user is present and can complete browser approval.
  • Use Client Credentials when the CLI is a service wrapper or scheduled workload.
  • Use API keys only for bounded machine automation with aggressive rotation and secret scanning.
  • Prefer short-lived tokens over static Secrets wherever the platform supports them.

These controls tend to break down when the CLI is copied into CI/CD runners, because shared automation often erases the distinction between user-driven and workload-driven access.

Common Variations and Edge Cases

Tighter credential controls often increase setup overhead, requiring organisations to balance usability against reduced exposure. That tradeoff matters most in developer tooling, ephemeral build jobs, and air-gapped or legacy environments where browser-based approval may be impossible. In those cases, teams sometimes default to API keys, but current guidance suggests doing so only with compensating controls such as IP restrictions, vault-backed retrieval, per-job issuance, and automated revocation. There is no universal standard for this yet, especially where CLI tools are used both interactively and in pipelines.

Two edge cases deserve special attention. First, if the CLI can act on behalf of different users, Device Flow remains the better choice because it preserves human attribution and MFA. Second, if the CLI is effectively a backend component, then Client Credentials should be paired with workload identity rather than a shared static key. NHIMG’s DeepSeek breach and OmniGPT breach illustrate the cost of weak secret discipline in AI-enabled environments, where exposed credentials are often reused faster than teams can revoke them. The practical rule is simple: if the identity must be attributable to a person, use Device Flow; if it must represent a workload, use Client Credentials; if neither is true and only a bounded automation need exists, use an API key temporarily, not permanently.

Standards & Framework Alignment

This section maps relevant standards and security frameworks to the operational risks and controls described in this guidance.

OWASP Non-Human Identity Top 10 address the attack and risk surface, while NIST SP 800-63 and NIST CSF 2.0 set the governance and control requirements practitioners need to meet.

Framework Control / Reference Relevance
OWASP Non-Human Identity Top 10 NHI-01 Covers choosing the right NHI auth model and avoiding static secret sprawl.
NIST SP 800-63 Device Flow depends on strong user authentication and federation assurance.
NIST CSF 2.0 PR.AC-4 Access control and authorization scoping are central to token selection.

Prefer short-lived, least-privilege identities over reusable API keys for CLI access.