59API

← 返回教程列表

Custom Base URLs for AI CLIs: A Practical Guide

API 使用 · EN · 2026-10-06

Many AI command-line tools are built around a default provider, but that does not mean you are locked into its endpoint. A custom base URL lets you route requests through a compatible gateway, private deployment, company proxy, or lower-cost API relay. The main challenge is knowing which environment variable or configuration field your CLI actually reads.

What a custom base URL changes

An API base URL is the root address used before paths such as /v1/messages or /v1/chat/completions are added. Changing it can redirect requests without changing your workflow, prompts, model selection, or client library. Authentication usually remains separate: you provide an API key through the variable expected by the tool.

Before changing anything, confirm three compatibility details: the endpoint protocol, the authentication header, and the model names accepted by the service. Claude-based tools generally use Anthropic's Messages API format, while OpenAI-compatible tools expect Chat Completions or Responses API behavior. A URL that merely looks compatible may still fail if the request format differs.

Point Claude Code at a custom endpoint

For Claude Code, set the Anthropic base URL and key in the same shell session before launching the CLI. For 59API, the endpoint is https://api.59api.com:

export ANTHROPIC_BASE_URL=https://api.59api.com

export ANTHROPIC_API_KEY=your_59api_key

Then start Claude Code normally. On Windows PowerShell, the equivalent commands are $env:ANTHROPIC_BASE_URL="https://api.59api.com" and $env:ANTHROPIC_API_KEY="your_59api_key". Keeping these values in the shell makes the change easy to test and easy to remove. For persistent use, place them in your shell profile or a project-specific environment file that is excluded from version control.

Use a model identifier supported by the relay. 59API provides access to Claude Opus, Sonnet, Haiku, and Fable models, so you can choose a smaller model for routine coding and reserve a stronger model for complex reasoning.

Configure Codex and OpenAI-compatible CLIs

OpenAI-compatible tools commonly use OPENAI_BASE_URL and OPENAI_API_KEY. A typical setup is:

export OPENAI_BASE_URL=https://api.59api.com/v1

export OPENAI_API_KEY=your_59api_key

Some CLIs expose the same values in a config file or use a provider-specific setting instead. Check the tool's help output, such as tool --help, and search its documentation for “base URL,” “endpoint,” or “provider.” Do not assume that an OpenAI SDK variable will automatically affect Claude Code, or that every CLI expects the /v1 suffix. The correct path depends on how the relay maps its routes.

For applications using the OpenAI SDK directly, set the client option rather than relying on a global shell variable. In Python, this is typically a base_url argument; in JavaScript or TypeScript, it is usually the same option in the client constructor. This approach is clearer when one application uses multiple providers.

Relay versus direct provider access

The tradeoffs are equally important. A relay adds another service boundary, so you must review its privacy, retention, uptime, and support policies before sending sensitive code or data. There can also be small differences in model availability, streaming behavior, rate limits, or error messages. Direct provider access offers the simplest support path and the earliest access to provider-specific features.

Troubleshooting checklist

For developers who want inexpensive access to Claude and GPT from familiar tools, 59API is a practical starting point: it supports Claude Code, Codex, and OpenAI-compatible SDKs, offers pay-as-you-go pricing, and includes a referral rebate program. Sign up, obtain an API key, set the matching base URL, and validate one small request before moving a larger workflow across.

价格 · 接入文档

准备好开始了吗?

几分钟接入 Claude 与 GPT,全网超低价,原生不降智。立即注册即可领取 API 密钥。

免费注册