How to Set Up Claude Code with a Third-Party API Endpoint
Claude Code third-party API endpoint setup: a practical guide from HeFu.
HeFu · Published 2026-09-279 min read
As of September 2026, the fastest way to run Claude Code without paying full official API rates or fighting subscription restrictions is to route it through an Anthropic-compatible third-party endpoint such as Hefu. You only need two environment variables — ANTHROPIC_BASE_URL=https://api.hefu.hk/v1 and ANTHROPIC_API_KEY=<your Hefu key> — and the complete configuration takes 30 seconds to 5 minutes (KissAPI, 2026 — source), giving you access to Claude Opus 5, Sonnet 5, and the rest of the Claude family through one unified console. Model availability may change, so check the official model directory for the current list.
Why Use a Third-Party API Endpoint for Claude Code
Cost is the first reason. Heavy Claude Code users can spend roughly $13 per day when billing directly through the official Anthropic API (CloudZero, 2026, accessed September 2026 — source). A third-party endpoint lets you meter usage, switch to cheaper models for routine tasks, and reserve Claude for the coding workloads where it genuinely outperforms alternatives.
Second, the subscription rules changed. On Apr 3, 2026, Anthropic announced that Claude subscription plans no longer cover usage inside third-party tools, so developers must shift to API-key billing (Boris Cherny's statement, reported by Business Insider — source). Routing through Hefu gives you a compliant, key-based setup without requiring an overseas credit card, since the endpoint runs on a Hong Kong node with direct connectivity (see Hefu docs).
Third, a gateway avoids lock-in. One Hefu key can reach Claude Opus 5 / Fable 5, Claude Sonnet 5 / Sonnet 4.6, and the Opus 4.8 / 4.7 / 4.6 family, plus GPT-5.6, DeepSeek-V4-Pro, Kimi K2.6, and Gemini 3.6 Flash, all listed in the model directory (accessed September 2026). Note that Claude Code cannot talk to a raw OpenAI endpoint: the third-party service must expose an Anthropic Messages API-compatible surface (Atlas Cloud, 2026 — source; see also Anthropic Messages API docs). Hefu provides exactly that compatibility layer, so the official Claude Code CLI works without modification.
Prerequisites Before You Start
Before configuring anything, gather the following:
- A Hefu account and an active API key. You can generate or renew keys from the dashboard linked on the pricing page (accessed September 2026).
- A runtime that can launch Claude Code. Typically Node.js on macOS or Linux; Windows users can run it under WSL. Node.js 18+ is required for current Claude Code releases (Anthropic docs).
- The current Claude Code CLI installed. If you are on macOS inside Xcode, note that the Xcode built-in Claude Code requires a recent macOS and Xcode version, plus a placeholder API key to bypass the UI login check; exact minimum versions vary (see Apple Xcode release notes). For the CLI itself, see the official installation guide.
The whole process, including key creation, takes about 2–5 minutes — the same effort as the third-party endpoint guides we previously published for Cline and Open WebUI. If you are primarily interested in Claude Opus 5 cost and model tiers, our Claude Opus 5 API Access guide covers that angle in depth.
Choosing and Creating Your Hefu API Key
Log into the Hefu console at https://www.hefu.hk and navigate to the API key section, which is also reachable from the pricing page (accessed September 2026). Create a dedicated key for Claude Code — do not reuse a key that is already embedded in production applications. Copy both values immediately:
- Base URL:
https://api.hefu.hk/v1 - API key: the
sk-...string generated by the console
If you plan to test multiple Claude models, check the model directory first. For coding workloads, Claude Sonnet 5 is the default recommendation for speed-to-quality balance; Claude Opus 5 / Fable 5 is the flagship choice for complex refactors and long-context reasoning. Hefu rates are published on the official pricing page and may change, so confirm current pricing there before committing a heavy workload.
Configuration via Environment Variables
Claude Code reads two environment variables at startup. Set them in your shell profile or in the session where you launch claude:
export ANTHROPIC_BASE_URL="https://api.hefu.hk/v1"
export ANTHROPIC_API_KEY="sk-your-key-from-hefu-console"
On Windows PowerShell:
$env:ANTHROPIC_BASE_URL = "https://api.hefu.hk/v1"
$env:ANTHROPIC_API_KEY = "sk-your-key-from-hefu-console"
After exporting both variables, restart Claude Code so it picks up the new values. This is the minimal path: just two variables, and the fastest setups finish in about 30 seconds (KissAPI, 2026 — source). The environment variable names are part of the official Claude Code configuration surface (Anthropic docs). If your environment uses a settings.json override for the base URL, remove or replace that value as well — the Hefu developer documentation shows both approaches.
Verification: Testing the Third-Party Endpoint Connection
Launch claude in your terminal and send a simple prompt, for example: Reply with the single word OK. If the configuration is correct, you will receive a normal Claude response within a few seconds. To confirm which model is answering, use an explicit model flag such as --model claude-sonnet-5 or check the model selector inside the Claude Code UI.
If the first request fails, do not assume the endpoint is wrong. Consult the official documentation first — it always reflects the current base URL and supported model identifiers. You can also verify connectivity with curl https://api.hefu.hk/v1/models using your key in the Authorization header; a 200 response confirms the endpoint and key are valid.
Troubleshooting Common Endpoint Setup Issues
The most frequent problems, and their quick fixes:
- 401 authentication errors. The key is invalid, expired, or was copied with extra whitespace. Regenerate the key in the Hefu console and re-export
ANTHROPIC_API_KEY. - 404 on the base URL. The URL is missing the
/v1suffix. Use exactlyhttps://api.hefu.hk/v1, with no trailing path or slash variations. - Timeout on the first request. This is usually cold-start latency or a network path issue to the
api.hefu.hkdomain. Retry after 10 seconds (suggested interval); if it persists, check your firewall and proxy settings. - Xcode refuses to connect. Xcode's built-in Claude Code (as of September 2026) requires a recent macOS and Xcode version, and the UI login check needs a placeholder API key before it will accept a custom endpoint; exact version requirements vary (see Apple Xcode release notes). Equivalent guidance is also covered in the Claude Code docs.
For deeper diagnostics, the troubleshooting section of the Hefu dev docs lists error codes and expected response payloads for every supported model.
Security and Best Practices for API Credentials
Treat your Hefu API key like a production secret:
- Use environment variables, never hardcoded strings. Hardcoding keys into scripts or notebooks is the most common leak vector.
- Rotate keys periodically. The Hefu console supports regenerating a key without deleting your account; rotate immediately if a key is exposed in a commit.
- Keep the base URL consistent with the official endpoint. Only
https://api.hefu.hk/v1is valid; do not use third-party mirrors or older addresses found in forum posts. - Scope keys per tool. If you run Claude Code, Cline, and Open WebUI against Hefu, issue a separate key for each tool so you can revoke one without breaking the others.
- Add
.envto.gitignoreif you store credentials in a dotenv file.
Comparison Table: Official Anthropic API vs. Hefu Third-Party Endpoint
| Dimension | Official Anthropic API | Hefu Third-Party Endpoint |
|---|---|---|
| Setup time | Full account, billing, and approval flow; typically minutes to hours | Two environment variables; ~30 seconds to 5 minutes |
| Billing model | Per-token API pricing; Claude subscriptions no longer cover third-party tools as of Apr 3, 2026 | Key-based pay-as-you-go; current rates on the pricing page |
| Protocol | Native Anthropic Messages API | Anthropic Messages API-compatible gateway, also translates for non-Claude models |
| Key management | Single Anthropic key per account | Unified Hefu key for Claude, GPT-5.6, DeepSeek, Kimi, Gemini, and more |
| Documentation & support | Anthropic official docs (API docs) | Hefu dev docs plus step-by-step guides for Cline and Open WebUI |
| Service-terms risk | Subscription misuse if used inside third-party tools after the Apr 2026 change | Dedicated API-key route is the compliant path recommended by Anthropic |
FAQ
What is the exact API base URL I should use for Claude Code?
Use https://api.hefu.hk/v1 and set it as ANTHROPIC_BASE_URL in your environment variables. The official documentation always reflects the current value, so check there if you ever see a version mismatch.
Do I need a different API key for the Hefu endpoint?
Yes. Generate a dedicated key from the Hefu console, which is linked from the pricing page, and export it as ANTHROPIC_API_KEY. Do not reuse your Anthropic key, because the two billing systems are unrelated.
Can I switch back to the official Anthropic API later?
Yes. Unset ANTHROPIC_BASE_URL (or restore the Anthropic default) and restart Claude Code. Keep your original Anthropic key saved; your project files and settings are unaffected by the switch.
Why can't I point Claude Code directly at an OpenAI endpoint?
Claude Code expects the Anthropic Messages API protocol, and a raw OpenAI endpoint returns a response format it cannot parse. A third-party gateway must translate between protocols; Hefu provides this Anthropic-compatible surface so the official Claude Code CLI works unchanged (Atlas Cloud, 2026 — source).
Where do I go if the endpoint works but responses are slow?
Check the Hefu status and support channels linked from the pricing page, then verify your network latency to the https://api.hefu.hk domain. The developer documentation also lists timeout thresholds and recommended retry intervals for slow-response scenarios.
FAQ
What is the exact API base URL I should use for Claude Code?
Use `https://api.hefu.hk/v1` and set it as `ANTHROPIC_BASE_URL` in your environment variables. The [official documentation](https://www.hefu.hk/docs) always reflects the current value, so check there if you ever see a version mismatch.
Do I need a different API key for the Hefu endpoint?
Yes. Generate a dedicated key from the Hefu console, which is linked from the [pricing page](https://www.hefu.hk/pricing), and export it as `ANTHROPIC_API_KEY`. Do not reuse your Anthropic key, because the two billing systems are unrelated.
Can I switch back to the official Anthropic API later?
Yes. Unset `ANTHROPIC_BASE_URL` (or restore the Anthropic default) and restart Claude Code. Keep your original Anthropic key saved; your project files and settings are unaffected by the switch.
Why can't I point Claude Code directly at an OpenAI endpoint?
Claude Code expects the Anthropic Messages API protocol, and a raw OpenAI endpoint returns a response format it cannot parse. A third-party gateway must translate between protocols; Hefu provides this Anthropic-compatible surface so the official Claude Code CLI works unchanged (Atlas Cloud, 2026 — [source](https://atlascloud.ai/docs/claude-code-third-party/)).
Where do I go if the endpoint works but responses are slow?
Check the Hefu status and support channels linked from the [pricing page](https://www.hefu.hk/pricing), then verify your network latency to the `https://api.hefu.hk` domain. The [developer documentation](https://www.hefu.hk/docs) also lists timeout thresholds and recommended retry intervals for slow-response scenarios.