AIXHUBDocs
API integration

Anthropic Messages API

Send non-streaming and streaming AIXHUB Anthropic-compatible Messages requests, inspect the response, and retry safely.

Use the Anthropic-compatible surface when the selected AIXHUB model route supports Messages. Raw HTTP calls use the complete endpoint POST https://api.aixhub.org/v1/messages; Anthropic SDKs and clients usually receive the root Base URL https://api.aixhub.org and append /v1/messages themselves.

SettingValue
Base URL for an Anthropic SDK or compatible clienthttps://api.aixhub.org
Complete HTTP endpointhttps://api.aixhub.org/v1/messages
Authenticationx-api-key: AIXHUB_API_KEY
Required version headeranthropic-version: 2023-06-01
Example modelpanel-anthropic-model-id copied from Model routes

Create a dedicated AIXHUB API key before running the examples. Do not send an OpenAI Bearer header in place of x-api-key.

Bash request

read -rsp "AIXHUB API key: " AIXHUB_API_KEY && echo
export AIXHUB_API_KEY
export AIXHUB_MODEL="panel-anthropic-model-id"

curl --fail-with-body --silent --show-error \
  https://api.aixhub.org/v1/messages \
  --header "x-api-key: $AIXHUB_API_KEY" \
  --header "anthropic-version: 2023-06-01" \
  --header "content-type: application/json" \
  --data "{\"model\":\"$AIXHUB_MODEL\",\"max_tokens\":16,\"messages\":[{\"role\":\"user\",\"content\":\"Reply with OK only.\"}]}"

PowerShell request

$secureKey = Read-Host "AIXHUB API key" -AsSecureString
$env:AIXHUB_API_KEY = [System.Net.NetworkCredential]::new("", $secureKey).Password
$env:AIXHUB_MODEL = "panel-anthropic-model-id"

$headers = @{
  "x-api-key" = $env:AIXHUB_API_KEY
  "anthropic-version" = "2023-06-01"
}
$body = @{
  model = $env:AIXHUB_MODEL
  max_tokens = 16
  messages = @(@{ role = "user"; content = "Reply with OK only." })
} | ConvertTo-Json -Depth 4 -Compress

$response = Invoke-RestMethod `
  -Method Post `
  -Uri "https://api.aixhub.org/v1/messages" `
  -Headers $headers `
  -ContentType "application/json" `
  -Body $body
$response.content |
  Where-Object { $_.type -eq "text" } |
  Select-Object -ExpandProperty text

Replace the model stand-in with an exact Messages-compatible route. After testing in a disposable shell, run unset AIXHUB_API_KEY AIXHUB_MODEL in Bash. In PowerShell, remove the environment values and every object that holds the derived key:

Remove-Item Env:AIXHUB_API_KEY, Env:AIXHUB_MODEL -ErrorAction SilentlyContinue
Remove-Variable headers, body, response, secureKey -ErrorAction SilentlyContinue

Request fields

FieldRequiredMeaning
modelYesExact Anthropic-compatible model ID copied from AIXHUB Model routes
max_tokensYesMaximum number of tokens to generate; it does not guarantee that many tokens will be used
messagesYesOrdered user and assistant turns; the minimal example sends one user turn
messages[].roleYesuser or assistant; a system instruction belongs in the top-level system field
messages[].contentYesA string or supported content-block array
systemNoTop-level system instruction; do not insert a system role into messages
streamNoSet true to receive server-sent events instead of one JSON response
temperatureNoSampling control supported only when the selected route accepts it

Start with the minimal fields. Add tools, images, prompt caching, or other optional features only after the route documentation confirms support and the basic request is visible in Usage.

Response example

The following JSON shows the stable shape to inspect. IDs and token counts are illustrative; do not hard-code them.

{
  "id": "msg_01Example",
  "type": "message",
  "role": "assistant",
  "model": "panel-anthropic-model-id",
  "content": [
    {
      "type": "text",
      "text": "OK"
    }
  ],
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 12,
    "output_tokens": 1
  }
}

Read text blocks from content and inspect stop_reason before treating the output as complete. Preserve the HTTP status and any request ID header returned by the service when investigating a failure.

Streaming

Add "stream": true and disable curl buffering:

curl --no-buffer --fail-with-body --silent --show-error \
  https://api.aixhub.org/v1/messages \
  --header "x-api-key: $AIXHUB_API_KEY" \
  --header "anthropic-version: 2023-06-01" \
  --header "content-type: application/json" \
  --data "{\"model\":\"$AIXHUB_MODEL\",\"max_tokens\":16,\"stream\":true,\"messages\":[{\"role\":\"user\",\"content\":\"Reply with OK only.\"}]}"

A normal text stream follows an event sequence such as message_start, content_block_start, one or more content_block_delta events, content_block_stop, message_delta, and message_stop. ping events can appear between content events. Concatenate text only from text-delta content blocks in event order. Save the final message_delta.delta.stop_reason before message_stop.

Treat message_stop as the protocol end of the message, then inspect the saved stop_reason before accepting the output as complete. For this bounded OK test, expect end_turn; max_tokens means the output was truncated, while other reasons require application-specific handling. A stream can deliver an error event after the initial HTTP 200; in that case, keep the error, discard assumptions based on partial text, check Usage, and decide whether a new request is safe.

Errors and retries

ObservationActionRetry rule
400 or invalid requestCheck JSON shape, required fields, model ID, and protocolCorrect it before retrying
401Confirm x-api-key and the complete dedicated keyRetry only after fixing authentication
403Check key state, group, subscription, and balance; INSUFFICIENT_BALANCE is 403Retry only after the account state changes
413Reduce prompt or attachment sizeRetry the smaller request
429Reduce rate and concurrency; honor Retry-After when presentUse capped backoff and a limited attempt count
502 or 503Preserve time and request ID, then check Usage and service stateRetry a limited number of times after waiting
SSE error eventTreat partial output as incomplete and preserve the event payloadCheck acceptance and Usage before starting a new message

A model ID or route problem is not always a 404. For any transient retry, allow at most three total attempts including the original, and honor Retry-After whenever the response actually returns it. Do not replay a 5xx, disconnect, or SSE error unless the application explicitly accepts duplicate output and charges, or can prove the prior generation was not accepted. See the error code reference for source classification and safe diagnostics.

Verification

The non-streaming example should print:

OK

For streaming, require message_stop and verify the final message_delta.delta.stop_reason is end_turn for this bounded test. Treat max_tokens as truncated rather than successful completion. Then open AIXHUB Usage and match the request time, selected model, and successful status. If Usage displays the protocol, confirm it is the Anthropic-compatible route.

The Messages request returns an assistant text block containing OK, the stream reaches message_stop with stop_reason end_turn, and AIXHUB records the matching model and status.

Continue with the Anthropic Python SDK or configure Claude Code.

Official sources

Last verified: 2026-07-14

On this page