Skip to main content
Version: 3.19.0

Proxy Anthropic Requests

Anthropic provides the native Messages API and an OpenAI-compatible API for accessing Claude models.

This guide shows how to integrate APISIX with Anthropic using the ai-proxy plugin. With provider set to anthropic, you do not need to set a custom endpoint.

Prerequisite(s)​

  • Install Docker.
  • Install cURL to send requests to the services for validation.
  • Follow the Getting Started Tutorial to start a new APISIX instance in Docker or on Kubernetes.

Obtain an Anthropic API Key​

Create an account and API key by following the Anthropic API Documentation. Optionally save the key to an environment variable:

export ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx # replace with your API key

Create a Route to Anthropic​

This example accepts an OpenAI Chat Completions request on a custom path. APISIX identifies the format from the messages field and sends it to Anthropic's OpenAI-compatible endpoint. To accept the native Anthropic Messages format instead, configure a route URI ending in /v1/messages; see Native Anthropic Messages API Pass-Through.

Create a route with the ai-proxy plugin as such:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
--data-binary @- <<EOF
{
"id": "anthropic-chat",
"uri": "/anything",
"plugins": {
"ai-proxy": {
"provider": "anthropic",
"auth": {
"header": {
"x-api-key": "$ANTHROPIC_API_KEY"
}
},
"options": {
"model": "claude-sonnet-4-5"
}
}
}
}
EOF

❶ Set the provider to anthropic.

❷ Attach the Anthropic API key using the x-api-key header.

❸ Set a model supported by Anthropic, for example claude-sonnet-4-5.

Verify​

Send a request with the following prompts to the route:

curl "http://127.0.0.1:9080/anything" -X POST \
-H "Content-Type: application/json" \
-d '{"messages": [{"role": "user", "content": "Hello"}]}'

You should receive a response similar to the following:

{
"request": {
"messages": [
{
"role": "user",
"content": "Hello"
}
]
},
"response": {
"id": "msg_01HUQ8fAR1XvJ9PodefrZixW",
"object": "chat.completion",
"created": 1770029318,
"model": "claude-sonnet-4-5-20250929",
"choices": [
{
"index": 0,
"finish_reason": "stop",
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
}
}
],
"usage": {
"prompt_tokens": 8,
"completion_tokens": 12,
"total_tokens": 20
}
}
}

Proxy Native Anthropic Messages​

When the client and Anthropic both use the Messages API, configure a route ending in /v1/messages. APISIX detects the path and forwards the native request without protocol conversion, preserving Anthropic-specific request and response fields.

Create a native Messages route:

curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \
-H "X-API-KEY: ${ADMIN_API_KEY}" \
--data-binary @- <<EOF
{
"id": "anthropic-messages",
"uri": "/v1/messages",
"plugins": {
"ai-proxy": {
"provider": "anthropic",
"auth": {
"header": {
"x-api-key": "$ANTHROPIC_API_KEY"
}
},
"options": {
"model": "claude-sonnet-4-5"
}
}
}
}
EOF

Send a native Messages request:

curl "http://127.0.0.1:9080/v1/messages" -X POST \
-H "Content-Type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{
"max_tokens": 128,
"messages": [
{
"role": "user",
"content": "Hello"
}
]
}'

You should receive a native Anthropic Messages response similar to the following:

{
"id": "msg_01HUQ8fAR1XvJ9PodefrZixW",
"type": "message",
"role": "assistant",
"model": "claude-sonnet-4-5-20250929",
"content": [
{
"type": "text",
"text": "Hello! How can I help you today?"
}
],
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 8,
"output_tokens": 12
}
}

To verify streaming, send a request with stream set to true. The -N option disables curl response buffering so that each event is displayed as it arrives:

curl -N "http://127.0.0.1:9080/v1/messages" -X POST \
-H "Content-Type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{
"max_tokens": 128,
"stream": true,
"messages": [
{
"role": "user",
"content": "Hello"
}
]
}'

You should receive Anthropic SSE events ending with message_stop, similar to the following abbreviated response:

event: message_start
data: {"type":"message_start","message":{"type":"message","role":"assistant",...}}

event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello"}}

...

event: message_stop
data: {"type":"message_stop"}

Next Steps​

You have learned how to integrate APISIX with Anthropic. See the Anthropic API Documentation and Models pages for more details.