如何在 Claude Code 使用第三方 API 供應商:以 ModAPI 為例
How to Use Claude Code with a Third-Party API Provider (ModAPI Example)
Claude Code 可連接第三方 API 供應商,但端點必須支援 Anthropic Messages API;只提供 OpenAI 相容的 /v1/chat/completions 端點並不足夠。
Disclosure: I work on ModAPI. The general steps apply to any provider that implements the Anthropic Messages API.
Claude Code talks to Anthropic's API by default, but it doesn't have to. You can point it at a third-party API provider or gateway that speaks the same protocol, which is handy when you want one bill for several model vendors, per-key spending limits for a team, or a different data-retention setup.
This guide explains what a third-party endpoint has to support, which settings Claude Code reads, and how to set it up with ModAPI as a worked example.
What "third-party API" means for Claude Code
Claude Code is not an OpenAI-compatible client. It sends requests in Anthropic's Messages API format (POST /v1/messages), including streaming events, tool use and prompt-caching fields.
So the one hard requirement is:
A Claude Code custom API endpoint must implement the Anthropic Messages API. An endpoint that only offers OpenAI-style
/v1/chat/completionswill not work, however many models it lists.
Before you pick a provider, check its docs for an Anthropic-format endpoint, not just "OpenAI compatible". If you are comparing gateways for Claude in general, our overview of what a Claude API gateway does covers the trade-offs.
The settings Claude Code reads
Claude Code picks up its endpoint and credentials from environment variables:
| Variable | What it does |
|---|---|
ANTHROPIC_BASE_URL |
Where requests go instead of Anthropic's API |
ANTHROPIC_AUTH_TOKEN |
Sent as Authorization: Bearer <token>
|
ANTHROPIC_API_KEY |
Sent as the x-api-key header (use one of the two, as your provider documents) |
ANTHROPIC_MODEL |
The main model Claude Code uses |
ANTHROPIC_DEFAULT_HAIKU_MODEL |
The model used when Claude Code selects the Haiku alias, including for a subagent configured to use Haiku |
We checked this behaviour with Claude Code 2.1.329: with ANTHROPIC_BASE_URL set, it sends POST /v1/messages to that root with streaming on; ANTHROPIC_AUTH_TOKEN is sent as a Bearer header and ANTHROPIC_API_KEY as x-api-key; and a subagent set to Haiku requests the model in ANTHROPIC_DEFAULT_HAIKU_MODEL. Check Claude Code's environment variable docs for the current list, since names occasionally change between versions.
A full Claude Code session needs more than single replies: the endpoint must stream Messages events and handle tool-use round trips (tool_use then tool_result), and pass cache_control through for prompt caching.
Example: Claude Code with ModAPI
ModAPI is an AI gateway with an Anthropic-format endpoint for Claude models, alongside its OpenAI-compatible API for other vendors.
1. Get an API key
Create an account at modapi.ai and create a key in the dashboard. You can restrict the key to the Claude models you plan to use and give it a spending quota and expiry date, which is useful when the key lives on a shared machine or in CI.
2. Set the environment variables
export ANTHROPIC_BASE_URL="https://modapi.ai"
export ANTHROPIC_AUTH_TOKEN="your-modapi-api-key"
export ANTHROPIC_MODEL="claude-sonnet-5-5"
export ANTHROPIC_DEFAULT_HAIKU_MODEL="claude-haiku-5-5" # used when Claude Code picks the Haiku alias
Note that the base URL has no /v1 suffix: Claude Code appends /v1/messages itself.
To make this permanent, add the lines to your shell profile, or put them in the env block of Claude Code's settings.json so they only apply to Claude Code.
3. Start Claude Code
claude
Check it in two stages so you know where a failure happens:
- Ask it to reply with just
OK. This confirms the endpoint, key and model. - Ask it to hand a small task to a subagent, for example:
Use a subagent to reply OK, then tell me what it said.This confirms streaming and a tool round trip, and, if a subagent is set to Haiku, that the Haiku model works too.
If you get an authentication error, check that the key is pasted without spaces and that it is allowed to call the model you set.
4. Pick a model
Any Claude model ModAPI serves on its Anthropic endpoint can go in ANTHROPIC_MODEL. Current IDs and prices are on the pricing page, for example:
-
claude-sonnet-5-5for everyday coding -
claude-opus-5-5for long, hard tasks -
claude-haiku-5-5for the Haiku alias (for example, a subagent set to Haiku)
Things to know before you switch
- Prices and billing. Requests are billed by the provider, not Anthropic. Check ModAPI's current pricing page for the selected model and group, including input, output, cache-read and cache-write rates.
- Data retention. Each provider has its own policy. For stateless text requests covered by its policy, ModAPI does not persist prompt or response content at the gateway layer. LLM_Prime applies upstream zero-data-retention routing restrictions by default. See the data retention statement.
- Features. Prompt caching, tool use and extended thinking depend on the provider passing those fields through unchanged. Test the features you rely on before moving a whole team over.
-
Switching back. Unset the variables (or remove them from
settings.json) and Claude Code goes back to Anthropic's API.
If your goal is mainly a cheaper or more flexible way to reach Claude, our comparison of Claude API alternatives walks through the options beyond Claude Code.
Troubleshooting
| Symptom | Likely cause |
|---|---|
401 / authentication error |
Wrong key, or the key header doesn't match what the provider expects (ANTHROPIC_AUTH_TOKEN vs ANTHROPIC_API_KEY) |
404 on every request |
Base URL includes /v1, or the provider has no Anthropic Messages endpoint |
| Model not found | The model ID isn't served on the provider's Anthropic endpoint, or the key isn't allowed to call it |
| Works, but a Haiku subagent fails |
ANTHROPIC_DEFAULT_HAIKU_MODEL points to a model the key can't use |
| A tool call hangs or times out | Contact ModAPI support with the time of the request and the model you used |
Summary
To use Claude Code with a third-party API, you need a provider with an Anthropic Messages endpoint, then set ANTHROPIC_BASE_URL, a credential variable and the model names. With ModAPI that is one key, one base URL and the Claude model IDs from the pricing page.
Questions or a setup that doesn't work? Ask in the ModAPI Discord.
來源:DEV Community · dev.to