Anthropic Messages Requests with AI Gateway
Create messages using the Anthropic Messages API format.
Endpoint
POST /v1/messages
Create a non-streaming message.
Example request
generate.ts
import Anthropic from '@anthropic-ai/sdk';
const apiKey = process.env.AI_GATEWAY_API_KEY || process.env.VERCEL_OIDC_TOKEN;
const anthropic = new Anthropic({
apiKey,
baseURL: 'https://ai-gateway.vercel.sh',
});
const message = await anthropic.messages.create({
model: 'anthropic/claude-opus-5',
max_tokens: 150,
messages: [
{
role: 'user',
content: 'Write a one-sentence bedtime story about a unicorn.',
},
],
temperature: 0.7,
});
for (const block of message.content) {
if (block.type === 'text') console.log('Response:', block.text);
}
console.log('Usage:', message.usage);generate.py
import os
import anthropic
api_key = os.getenv('AI_GATEWAY_API_KEY') or os.getenv('VERCEL_OIDC_TOKEN')
client = anthropic.Anthropic(
api_key=api_key,
base_url='https://ai-gateway.vercel.sh'
)
message = client.messages.create(
model='anthropic/claude-opus-5',
max_tokens=150,
messages=[
{
'role': 'user',
'content': 'Write a one-sentence bedtime story about a unicorn.'
}
],
temperature=0.7,
)
for block in message.content:
if block.type == "text":
print('Response:', block.text)
print('Usage:', message.usage)generate.sh
curl -X POST "https://ai-gateway.vercel.sh/v1/messages" \
-H "Authorization: Bearer $AI_GATEWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "anthropic/claude-opus-5",
"max_tokens": 150,
"messages": [
{
"role": "user",
"content": "Write a one-sentence bedtime story about a unicorn."
}
],
"temperature": 0.7
}'Response format
{
"id": "msg_123",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Once upon a time, a gentle unicorn with a shimmering silver mane danced through moonlit clouds, sprinkling stardust dreams upon sleeping children below."
}
],
"model": "anthropic/claude-opus-5",
"stop_reason": "end_turn",
"usage": {
"input_tokens": 15,
"output_tokens": 28
}
}Set stream: true to receive tokens as they are generated. See Streaming for the full example and the list of server-sent event types.
Last updated September 8, 2026
Was this helpful?