Skip to content

Commit a5cb3a5

Browse files
Prefer ANTHROPIC_API_KEY over OAuth token when both are set (#62)
* fix(gateway): prefer API key over OAuth token when both are set Reverses the auth precedence so ANTHROPIC_API_KEY takes priority over CLAUDE_CODE_OAUTH_TOKEN. This aligns with documentation guidance that API keys are recommended for automation, while OAuth tokens are appropriate for personal testing and development only. Fixes #61 * docs: hide OAuth token option from public documentation Remove CLAUDE_CODE_OAUTH_TOKEN from user-facing docs and examples. The option remains functional but is now undocumented, discoverable only by developers reading the source code. Inline code comments now include terms of use warning explaining that OAuth tokens may violate Anthropic's Consumer Terms for automated use.
1 parent e7dd97d commit a5cb3a5

File tree

6 files changed

+25
-35
lines changed

6 files changed

+25
-35
lines changed

.env.example

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
# Generate a secure key with: openssl rand -hex 32
66
CLAUDE_CODE_GATEWAY_API_KEY=your-secret-api-key
77

8-
# Claude authentication (one of these is required)
9-
# Recommended: Anthropic API key for automation
8+
# Claude authentication (required)
109
ANTHROPIC_API_KEY=
1110

12-
# Alternative: OAuth token (Claude Pro/Max) - for personal testing and development only
13-
# CLAUDE_CODE_OAUTH_TOKEN=
14-
1511
# Gateway server port (optional, default: 3100)
1612
GATEWAY_PORT=3100

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,9 @@ console.log(result.text);
101101
>
102102
> - **Use Docker**: containers provide essential filesystem and process isolation
103103
> - **Internal networks only**: deploy on VPN or Docker networks, not public internet
104-
> - **Use API keys for automation**: Anthropic API keys are the recommended authentication method. They operate under the [Commercial Terms](https://www.anthropic.com/legal/commercial-terms) which permit programmatic access.
105-
> - **OAuth tokens for personal use only**: Claude Pro/Max OAuth tokens are appropriate for personal testing and development. For production or shared deployments, use an API key.
104+
> - **Use API keys**: Anthropic API keys operate under the [Commercial Terms](https://www.anthropic.com/legal/commercial-terms) which permit programmatic access
106105
>
107-
> See [Environment Variables](docs/environment-variables.md#terms-considerations) for details.
106+
> See [Environment Variables](docs/environment-variables.md) for configuration details.
108107
109108
## Documentation
110109

docker-compose.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ services:
1414
HOME: /home/bun
1515
# Authentication for the gateway API
1616
CLAUDE_CODE_GATEWAY_API_KEY: ${CLAUDE_CODE_GATEWAY_API_KEY:-}
17-
# Claude auth: ANTHROPIC_API_KEY recommended for automation (see docs/environment-variables.md)
18-
# OAuth tokens are appropriate for personal testing and development only
17+
# Claude authentication
1918
ANTHROPIC_API_KEY: ${ANTHROPIC_API_KEY:-}
2019
CLAUDE_CODE_OAUTH_TOKEN: ${CLAUDE_CODE_OAUTH_TOKEN:-}
2120
volumes:

docs/environment-variables.md

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,7 @@
55
| Variable | Description |
66
|----------|-------------|
77
| `CLAUDE_CODE_GATEWAY_API_KEY` | Bearer token for gateway API requests |
8-
9-
## Claude Authentication (one required)
10-
11-
| Variable | Description |
12-
|----------|-------------|
13-
| `ANTHROPIC_API_KEY` | Anthropic API key (recommended for automation) |
14-
| `CLAUDE_CODE_OAUTH_TOKEN` | OAuth token (Claude Pro/Max). See [Terms Considerations](#terms-considerations). |
8+
| `ANTHROPIC_API_KEY` | Anthropic API key for Claude authentication |
159

1610
## Optional
1711

@@ -28,10 +22,6 @@ ANTHROPIC_API_KEY=sk-ant-...
2822
GATEWAY_PORT=3100
2923
```
3024

31-
## Terms Considerations
32-
33-
**API keys** are the recommended authentication method for Koine and automation use cases. They operate under [Anthropic's Commercial Terms](https://www.anthropic.com/legal/commercial-terms) which explicitly permit programmatic access.
34-
35-
**OAuth tokens** (Claude Pro/Max subscriptions) are appropriate for personal testing and development only. For production or shared deployments, use an API key.
25+
## Terms
3626

37-
If both are set, the OAuth token takes precedence. For automation, set only `ANTHROPIC_API_KEY`.
27+
Anthropic API keys operate under [Anthropic's Commercial Terms](https://www.anthropic.com/legal/commercial-terms) which explicitly permit programmatic access.

packages/gateway/__tests__/cli.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,23 @@ describe("CLI Module", () => {
4444
process.env = originalEnv;
4545
});
4646

47-
it("returns process.env when no OAuth token is set", () => {
48-
process.env.CLAUDE_CODE_OAUTH_TOKEN = undefined;
49-
process.env.ANTHROPIC_API_KEY = "test-api-key";
47+
it("returns process.env when no API key is set", () => {
48+
process.env.ANTHROPIC_API_KEY = undefined;
49+
process.env.CLAUDE_CODE_OAUTH_TOKEN = "oauth-token";
5050

5151
const env = buildClaudeEnv();
5252

53-
expect(env.ANTHROPIC_API_KEY).toBe("test-api-key");
53+
expect(env.CLAUDE_CODE_OAUTH_TOKEN).toBe("oauth-token");
5454
});
5555

56-
it("clears ANTHROPIC_API_KEY when OAuth token is present", () => {
57-
process.env.CLAUDE_CODE_OAUTH_TOKEN = "oauth-token";
56+
it("clears OAuth token when API key is present", () => {
5857
process.env.ANTHROPIC_API_KEY = "test-api-key";
58+
process.env.CLAUDE_CODE_OAUTH_TOKEN = "oauth-token";
5959

6060
const env = buildClaudeEnv();
6161

62-
expect(env.ANTHROPIC_API_KEY).toBeUndefined();
63-
expect(env.CLAUDE_CODE_OAUTH_TOKEN).toBe("oauth-token");
62+
expect(env.CLAUDE_CODE_OAUTH_TOKEN).toBeUndefined();
63+
expect(env.ANTHROPIC_API_KEY).toBe("test-api-key");
6464
});
6565
});
6666

packages/gateway/src/cli.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import type { ClaudeCliOutput, ErrorCode, UsageInfo } from "./types.js";
55

66
/**
77
* Builds environment variables for Claude CLI with auth precedence.
8-
* Prefers OAuth token (Max subscription) over API key when both are present.
8+
* Prefers API key over OAuth token when both are present.
9+
*
10+
* CLAUDE_CODE_OAUTH_TOKEN is an undocumented fallback for personal testing only.
11+
* OAuth tokens (Claude Pro/Max) operate under Anthropic's Consumer Terms which
12+
* prohibit automated access. Use ANTHROPIC_API_KEY for all automation.
13+
* See: https://www.anthropic.com/legal/consumer-terms
914
*
1015
* Also passes through tool proxy variables for Claude skills that need to
1116
* invoke Inbox Zero API tools.
@@ -15,9 +20,10 @@ export function buildClaudeEnv(options?: {
1520
}): NodeJS.ProcessEnv {
1621
const env = { ...process.env };
1722

18-
// Prefer OAuth token for Max subscribers over API key
19-
if (env.CLAUDE_CODE_OAUTH_TOKEN) {
20-
env.ANTHROPIC_API_KEY = undefined;
23+
// API key takes precedence - OAuth is undocumented fallback for personal testing only.
24+
// OAuth tokens may violate Anthropic's Consumer Terms for automated use.
25+
if (env.ANTHROPIC_API_KEY) {
26+
env.CLAUDE_CODE_OAUTH_TOKEN = undefined;
2127
}
2228

2329
// Pass through tool proxy variables for Claude skills

0 commit comments

Comments
 (0)