Custom Messages endpoint in the Anthropic Python SDK

Point the Anthropic Python SDK at the gateway with base_url, see why the path shows /v1 twice, and confirm it hits the Messages route before you send a key.

Checked 2026-11-22

·All guides·Models

The Anthropic Python SDK sends Messages requests to a versioned resource path of its own, so pointing it at a custom service means reasoning about the base and that path together. UplinkSwitch's canonical base is https://api.uplinkswitch.com/v1, and it is safe here: the SDK builds its path onto the base, the result carries /v1 twice, and the gateway folds the doubled segment back to a single /v1/messages. Both halves were checked on 2026-11-22: anthropic 2.12.0 joins the base and the resource exactly this way, both in its source and in a request captured by a local listener, and the live service answered a request to /v1/v1/messages as the Messages route. This page gives the configuration, the reason the doubled path appears, and how to confirm the destination before you send an authenticated request.

Why the path shows /v1 twice

The SDK's Messages resource already contains the version: it appends /v1/messages to whatever base it holds. Its default base is the host with no version, so the default request is a clean /v1/messages. Set the base to https://api.uplinkswitch.com/v1 and the SDK appends /v1/messages to that, producing /v1/v1/messages. Rather than ask you to strip the version off the canonical base, the gateway folds the repeated /v1, so the canonical base works as written. Knowing this is why you should confirm the sent path rather than assume it from the base string.

Configure the client

Pass base_url on the client and the gateway key, and keep the native Messages call shape.

python
from anthropic import Anthropic

client = Anthropic(
    base_url="https://api.uplinkswitch.com/v1",
    api_key="sk-uplink-live-...",
)

message = client.messages.create(
    model="claude-sonnet-5-5",
    max_tokens=256,
    messages=[{"role": "user", "content": "One sentence on idempotency."}],
)
print(message.content[0].text)

The 2026-11-23 catalog listed claude-sonnet-5-5 as ok, and its endpoints include /v1/messages.

You can also supply the base through the ANTHROPIC_BASE_URL environment variable rather than in code; an explicit base_url on the client governs that client where both are present. The 2.12.0 source states that order outright (argument first, then the variable, then any profile setting, then the default), and the variable is read once, when the client is built. The asynchronous client, AsyncAnthropic, takes the same base_url and is configured on its own, so set it explicitly rather than expecting a shared value.

Keep the native shape

Configure the endpoint, not the request format. This is the Messages API, so keep max_tokens, the messages array and the message.content response as the SDK gives them; do not rewrite the call into Chat Completions, which is a different route. Keeping the native shape is what lets existing Anthropic code move with only the base and key changed.

Confirm the Messages destination

Before sending a real key against it, confirm the path. Turn on the SDK's request logging, or send one call to a small local listener that echoes back its path, and read that; blank the authorization header in anything saved. You are checking that the request resolves to the Messages route as intended. Path composition in general is explained in the base-URL composition guide; a 404 from a wrong model id is diagnosed on the 404 fix page, not here; the generic setup is on the SDKs guide; and the Agent SDK, a different library, is on the Agent SDK guide. This page owns the Python Messages path and client configuration only.

Questions and answers

Because the SDK appends its own /v1/messages to the base. A base ending in /v1 becomes /v1/v1/messages. The gateway folds the repeat back to /v1/messages, so the canonical base is safe; confirm the sent path to be sure.

The SDK reads ANTHROPIC_BASE_URL from the environment, and an explicit base_url on the client governs that client where both are set. That is the order in 2.12.0; if a client lands on the wrong host, the two values disagreeing is the usual reason.

Point the client at a local echo endpoint with a placeholder key and read the captured POST path, or enable the SDK's request logging and check the host as well as the path, redacting the authorization header first. A captured path resolving to the Messages route confirms the base before any real key goes out.
Join on Telegram
Help