warpdotdev/warp · error

Your API key is invalid. Please provide a valid key via '--a

Error message

Your API key is invalid. Please provide a valid key via '--api-key' or the WARP_API_KEY environment variable.

What it means

While authenticating with an explicit API key, the AuthManager emitted NeedsReauth and the auth state shows API-key authentication — the server rejected the key (invalid, revoked, or from the wrong environment). This branch is specifically chosen when `auth_state.is_api_key_authenticated()` is true.

Source

Thrown at app/src/ai/agent_sdk/mod.rs:1709

        if dispatched {
            return;
        }
        match event {
            AuthManagerEvent::AuthComplete => {
                dispatched = true;
                if let Err(err) = dispatch_command(ctx, command.clone(), global_options.clone()) {
                    report_fatal_error(err, ctx);
                }
            }
            AuthManagerEvent::NeedsReauth => {
                dispatched = true;
                let auth_state = AuthStateProvider::handle(ctx).as_ref(ctx).get();
                let message = if auth_state.is_api_key_authenticated() {
                    "Your API key is invalid. Please provide a valid key via '--api-key' or the WARP_API_KEY environment variable.".to_string()
                } else {
                    format!("Your credentials are invalid. Please log in again with `{cli_name} login`.")
                };
                report_fatal_error(anyhow::anyhow!(message), ctx);
            }
            AuthManagerEvent::AuthFailed(err) => {
                dispatched = true;
                report_fatal_error(anyhow::anyhow!("Authentication failed: {err:#}"), ctx);
            }
            _ => {}
        }
    });

    // Trigger authentication - the subscription above will handle the result.
    AuthManager::handle(ctx).update(ctx, |auth_manager, ctx| match authentication {
        CommandAuthentication::PendingApiKey(api_key) => {
            auth_manager.authenticate_api_key(api_key, ctx);
        }
        CommandAuthentication::RefreshUser => auth_manager.refresh_user(ctx),
    });
}

View on GitHub (pinned to e72fd7aacb)

Solutions

  1. Generate a fresh API key and update WARP_API_KEY / the --api-key value
  2. Check the variable for stray quotes or whitespace (inspect its length, keep the value out of logs)
  3. Confirm the key belongs to the same environment the CLI targets (staging vs production)
  4. Fall back to interactive `warp login` if API-key management is not required

Example fix

# before
export WARP_API_KEY="pk_old_revoked_key"

# after
export WARP_API_KEY=pk_freshly_generated_key   # no quotes needed; avoids stray characters
Defensive patterns

Strategy: try-catch

Validate before calling

# Cheap sanity checks on the key variable without printing its value
[ -n "$WARP_API_KEY" ] || { echo 'WARP_API_KEY empty' >&2; exit 1; }
[ "${#WARP_API_KEY}" -ge 20 ] || { echo 'WARP_API_KEY suspiciously short' >&2; exit 1; }

Try / catch

out=$(warp agent list 2>&1) || { case "$out" in
  *'API key is invalid'*) WARP_API_KEY=$(fetch_fresh_key) && warp agent list ;;
  *) echo "$out" >&2; exit 1 ;;
esac; }

Prevention

When it happens

Trigger: Running any auth-required command with WARP_API_KEY or --api-key set to a key the server rejects: revoked or deleted key, typo/truncation, a staging key used against production (or vice versa), or shell quoting artifacts in the variable.

Common situations: Rotated/revoked keys left in CI secrets; copy-paste whitespace or quotes in WARP_API_KEY; environment mismatch between the key's origin and the targeted SERVER_ROOT_URL.

Related errors


AI-assisted analysis of warpdotdev/warp@e72fd7aacb (2026-08-16). Data as JSON: /api/errors/cef88a1652a2224f. Report an issue: GitHub.