Anthropic on universal Gateway

import requests
import json

def test_anthropic_api():
messages = [
{"role": "user", "content": "Hello, world"}
]
data = [
{
"provider": "anthropic",
"endpoint": "v1/messages",
"headers": {
"x-api-key": "YOUR_ANTHROPIC_API_KEY",
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
"query": {
"model": "claude-3-opus-20240229",
"messages": messages,
"max_tokens": 500
}
}
]
response = requests.post(
"https://gateway.ai.cloudflare.com/v1/YOUR_ACCOUNT_TAG/YOUR_GATEWAY",
headers={"Content-Type": "application/json"},
json=data
)
response.raise_for_status()
print(json.dumps(response.json(), indent=4))

if __name__ == "__main__":
test_anthropic_api()
import requests
import json

def test_anthropic_api():
messages = [
{"role": "user", "content": "Hello, world"}
]
data = [
{
"provider": "anthropic",
"endpoint": "v1/messages",
"headers": {
"x-api-key": "YOUR_ANTHROPIC_API_KEY",
"anthropic-version": "2023-06-01",
"content-type": "application/json",
},
"query": {
"model": "claude-3-opus-20240229",
"messages": messages,
"max_tokens": 500
}
}
]
response = requests.post(
"https://gateway.ai.cloudflare.com/v1/YOUR_ACCOUNT_TAG/YOUR_GATEWAY",
headers={"Content-Type": "application/json"},
json=data
)
response.raise_for_status()
print(json.dumps(response.json(), indent=4))

if __name__ == "__main__":
test_anthropic_api()
Still getting the same raise HTTPError(http_error_msg, response=self) requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url
3 Replies
rob
rob2mo ago
@Cameron Aaron I tested this and its working, here's working code with CURL You should be able to get this working with python as a post request
curl -X POST https://gateway.ai.cloudflare.com/v1/ACCOUNT_TAG/GATEWAY_NAME \
-H 'Content-Type: application/json' \
-d '[
{
"provider": "anthropic",
"endpoint": "messages",
"headers": {
"x-api-key": "sk-XXXXXXXXX",
"content-type": "application/json",
"anthropic-version": "2023-06-01"
},
"query": {
"model": "claude-3-opus-20240229",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}
}
]'
curl -X POST https://gateway.ai.cloudflare.com/v1/ACCOUNT_TAG/GATEWAY_NAME \
-H 'Content-Type: application/json' \
-d '[
{
"provider": "anthropic",
"endpoint": "messages",
"headers": {
"x-api-key": "sk-XXXXXXXXX",
"content-type": "application/json",
"anthropic-version": "2023-06-01"
},
"query": {
"model": "claude-3-opus-20240229",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": "What is Cloudflare?"
}
]
}
}
]'
Cameron Aaron
Cameron Aaron2mo ago
Thank you
rob
rob2mo ago
yw