hapi icon indicating copy to clipboard operation
hapi copied to clipboard

Feature Request: Support passing environment variables via settings.json

Open qingchengliu opened this issue 10 months ago • 6 comments

What feature would you like to see?

Feature Request: Support passing environment variables via settings.json

Description

Currently, hapi CLI does not support configuring environment variables (like ANTHROPIC_AUTH_TOKEN, ANTHROPIC_BASE_URL) through settings.json. Users must set these environment variables in their shell before running hapi, which is not convenient for configuration management.

Additional information

No response

qingchengliu avatar Jan 13 '26 07:01 qingchengliu

Thanks for request. Current CLI settings only cover cliApiToken, serverUrl (plus machineId/daemon flags). No settings.json support for ANTHROPIC_AUTH_TOKEN or ANTHROPIC_BASE_URL found; only env vars read are HAPI_SERVER_URL + CLI_API_TOKEN.

Relevant code:

  • cli/src/persistence.ts:13 - settings schema fields
  • cli/src/ui/tokenInit.ts:4 - settings/env for CLI_API_TOKEN
  • cli/src/ui/serverUrlInit.ts:4 - settings/env for HAPI_SERVER_URL
  • cli/src/configuration.ts:31 - env defaults for server url + token

Need more info:

  • Which env keys should be supported in settings.json (only ANTHROPIC_* or more)?
  • Precedence preference: env over settings or settings over env?
  • Scope: apply to CLI process only, or also daemon/terminal sessions?

HAPI Bot

github-actions[bot] avatar Jan 13 '26 08:01 github-actions[bot]

https://code.claude.com/docs/en/settings#available-settings

tiann avatar Jan 13 '26 08:01 tiann

Actually, the Claude Code SDK does support passing ANTHROPIC_AUTH_TOKEN and ANTHROPIC_BASE_URL. This is the approach used by other platforms as well.

Reference: wecode-ai/Wegent executor/agents/claude_code/claude_code_agent.py


  env_config = {
      "ANTHROPIC_MODEL": model_id,
      "ANTHROPIC_SMALL_FAST_MODEL": env.get("small_model", model_id),
      "ANTHROPIC_DEFAULT_HAIKU_MODEL": env.get("small_model", model_id),
      "ANTHROPIC_AUTH_TOKEN": env.get("api_key", ""),
      "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": int(
          os.getenv("CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC", "0")
      ),
  }

  base_url = env.get("base_url", "")
  if base_url:
      env_config["ANTHROPIC_BASE_URL"] = base_url.removesuffix("/v1")

As the code shows:

  1. The SDK supports passing the API key via ANTHROPIC_AUTH_TOKEN in env
  2. The SDK supports custom API endpoints via ANTHROPIC_BASE_URL in env
  3. This is a common pattern implemented by other platforms

@tiann

qingchengliu avatar Jan 13 '26 09:01 qingchengliu

You can put it into ~/.claude/settings.json

tiann avatar Jan 13 '26 09:01 tiann

My apologies for the confusion. Let me clarify.

From my understanding, the Claude Code SDK does not directly read ~/.claude/settings. In this project, sessions created directly on the web interface are launched based on the Claude Code SDK.

In this scenario, could it cause the session to directly access Anthropic's endpoint instead of the user-configured custom endpoint?

The concern is: when a session is initiated through the SDK (rather than a local CLI session), does it bypass any custom endpoint configuration and default to Anthropic's official API?

Thank you for your reply.

@tiann

qingchengliu avatar Jan 13 '26 09:01 qingchengliu

No, the remote mode will also respect this configuration file.

tiann avatar Jan 13 '26 09:01 tiann