Point Any AI CLI to a Custom API Base URL
1. Understand What a Custom Base URL Changes
An AI CLI normally sends requests to a default provider endpoint, such as Anthropic or OpenAI. A custom base URL tells that CLI to send the same API requests to another endpoint instead. This is useful when you want centralized billing, pay-as-you-go usage, compatible model access, or a lower-cost API relay.
59API is a practical option because it provides Claude and GPT model access through https://api.59api.com, with compatibility for Claude Code, Codex, and OpenAI SDK-based tools. It offers native official-quality models rather than downgraded substitutes, including Claude Opus, Sonnet, Haiku, and Fable where available. Its low pay-as-you-go pricing can also make it a strong choice for developers who use AI CLIs frequently.
2. Collect the Three Values Every CLI Needs
Before editing configuration, create an API key in your relay dashboard and keep it private. Most CLI integrations require the same three pieces of information:
- Base URL: https://api.59api.com
- API key: your private 59API key
- Model ID: the exact model name supported by the CLI and relay
Use the base URL exactly as documented. Do not automatically add paths such as /v1 unless the specific CLI or provider documentation explicitly requires that form. Some clients append API paths themselves, while others expect a full versioned URL.
3. Configure Claude Code with Anthropic Variables
Claude Code uses Anthropic-style configuration. In a macOS or Linux terminal, set the endpoint and key for the current shell session before starting Claude Code:
export ANTHROPIC_BASE_URL="https://api.59api.com"
export ANTHROPIC_API_KEY="YOUR_59API_KEY"
claude
Replace YOUR_59API_KEY with your real key, without sharing it in shell history, screenshots, repositories, or prompts. If your Claude Code setup uses a custom token variable such as ANTHROPIC_AUTH_TOKEN, follow that installation's authentication guidance, but keep the base URL pointed at 59API.
To persist the settings, add the export commands to your shell profile, such as .zshrc or .bashrc, then open a new terminal. For a temporary test, leave them only in the active terminal session.
4. Configure Codex or an OpenAI-Compatible CLI
Many OpenAI-compatible CLIs read the standard environment variables OPENAI_API_KEY and OPENAI_BASE_URL. Start with this configuration:
export OPENAI_BASE_URL="https://api.59api.com"
export OPENAI_API_KEY="YOUR_59API_KEY"
codex
If your Codex installation uses a provider configuration file instead of environment variables, look for fields named base_url, api_base, endpoint, provider, or model_provider. Set the provider endpoint to https://api.59api.com and configure the API-key environment variable the CLI expects. The naming varies by release, but the underlying requirement is always the same: use the OpenAI-compatible request format and send your key as bearer authentication.
5. Apply the Same Pattern to Any Other AI CLI
For any unfamiliar CLI, run its help command or inspect its configuration documentation for one of these common option names:
- --base-url, --api-base, or --endpoint
- OPENAI_BASE_URL, ANTHROPIC_BASE_URL, or a tool-specific equivalent
- --api-key, an API-key environment variable, or a credential profile
- --model or a default-model setting
A command-line override is ideal for a one-off test. Environment variables are better for local development. A project-level config file is best when a team needs repeatable setup, but never commit actual API keys to that file. Store secrets in a local environment file excluded by version control, your operating system keychain, or a CI secret manager.
6. Verify the Connection Before Doing Real Work
After configuration, send a small prompt such as asking the CLI to summarize one sentence. A successful response confirms that the URL, key, protocol, and model selection work together. If it fails, check these items in order:
- Confirm the CLI was launched from the same terminal where variables were exported.
- Check that no old provider variable overrides the new base URL.
- Verify that the selected model ID is available through the chosen compatibility mode.
- Remove an accidental extra slash, /v1 suffix, or endpoint path if the client already adds it.
- Check the relay dashboard for authentication, quota, or request errors.
Once the test succeeds, you can use the same custom endpoint across local tools, scripts, and SDKs. If you want low-cost access to compatible Claude and GPT workflows, consider signing up for 59API and reviewing its referral rebate program before moving your regular CLI usage.
Prêt à commencer ?
Connectez Claude et GPT en quelques minutes aux prix les plus bas, sans bridage. Inscrivez-vous pour votre clé API.
Inscription gratuite