How to Configure Claude Code with a Custom API Endpoint
Claude Code can use a custom Anthropic-compatible API endpoint, which is useful when your team routes model traffic through YW Code or needs one place to manage API keys and model access.
This guide walks through the environment variables, a first request, and the most common setup errors. It is written by the YW Code team. YW Code is a third-party API gateway and is not affiliated with Anthropic. Model availability, pricing, and request limits depend on your YW Code account and the upstream configuration.
What you need
- Claude Code installed and working locally.
- An API key created in your YW Code account at api.ywcode.top.
- Access to at least one Claude model through the API key's group or account settings.
Keep the API key private. Do not paste a real key into a public issue, commit it to a repository, or include it in a screenshot.
Configure Claude Code on macOS or Linux
Set the endpoint and key in the terminal session where you will run Claude Code:
export ANTHROPIC_BASE_URL="https://api.ywcode.top" export ANTHROPIC_API_KEY="YOUR_YWCODE_API_KEY" claude Replace the placeholder with your own key. These export commands apply to the current shell session. Open a new terminal and run them again if the variables are not configured persistently.
Configure Claude Code in Windows PowerShell
Set the variables in PowerShell, then start Claude Code from the same window:
$env:ANTHROPIC_BASE_URL = "https://api.ywcode.top" $env:ANTHROPIC_API_KEY = "YOUR_YWCODE_API_KEY" claude PowerShell session variables are temporary. If you choose a persistent setup, protect the file that stores the key and keep it out of source control.
Check the endpoint path
Start with the base URL shown in the YW Code Claude Code integration guide. Depending on how a client version joins paths, an installation may expect the API root or a base URL ending in /v1. If you see a 404, check the integration guide and try the documented alternative:
https://api.ywcode.tophttps://api.ywcode.top/v1
Use one form at a time. A client that already appends /v1 can produce a duplicated path if you add it to the base URL yourself.
Make a first request
After starting claude, ask a small, non-sensitive question, such as asking it to explain a short function in a test project. In Claude Code, use /model to inspect or choose from the models available to your account. The model list can vary by API key group and upstream configuration, so use the options shown by your account or the API's /v1/models response rather than assuming every Claude model is enabled.
A successful response confirms that Claude Code reached the configured service. It does not imply that every model, tool, or optional parameter is available; those depend on the gateway and upstream settings.
Common setup errors
401 Unauthorized usually means the key is missing, mistyped, expired, or not present in the terminal session where Claude Code was started. Recheck the environment variable name and restart the CLI after changing it. Never share the key while debugging.
403 Forbidden can indicate that the account or key does not have access to the selected model. Check the key's group and model permissions, then select an enabled model.
404 Not Found often points to a base URL/path mismatch. Compare the configured URL with the integration guide, especially whether the client expects the root endpoint or a URL ending in /v1.
The CLI still uses another account or endpoint: print the non-secret base URL in the same terminal before launching Claude Code. Also check project-level or user-level Claude Code settings for environment values that may override the shell.
Keep keys out of your project
Use environment variables or a protected local settings file. Do not put an API key in a checked-in .env file, shell script shared with teammates, issue, or command example. If a key is exposed, revoke it and create a replacement in your account.
For the current YW Code endpoint and account-specific instructions, see the Claude Code integration guide. For Claude Code's supported configuration options, consult Anthropic's Claude Code documentation.
FAQ
Does this replace my Claude Code installation?
No. You continue to run Claude Code locally and configure the API endpoint it uses. Authentication and model access are handled by the API service associated with the key.
Can I use my Claude subscription key here?
This setup uses an API key issued by the configured API service. A Claude subscription and a third-party API key are separate products with separate access and billing. Check the terms and account details for each service.
Why does a model shown elsewhere fail here?
Model availability is account- and route-specific. Confirm that the model is enabled for your YW Code key and use its exact model identifier in Claude Code.
If you are integrating Claude Code with a custom API endpoint, start with the provider's current setup guide, verify the base URL and key in the same terminal session, and test with a small request before using it in a larger project.
原文:https://www.a8q9.cn/en/article/how-to-configure-claude-code-with-a-custom-api-endpoint-6190aa.html