Messages
檔案分析
使用 Claude Messages API 分析圖像與文件。
Messages API 透過 content 陣列中的內容區塊支援多模態輸入。圖像使用 image 區塊,PDF 與其他檔案使用 document 區塊。
端點與驗證方式同建立訊息。
請先確認所選模型支援對應媒體類型。file_id 必須來自同一服務與帳戶可讀取的上傳結果;本頁未定義上傳端點,不可直接使用其他供應商的檔案 ID。網址須可由服務端存取,本機路徑不能當作遠端檔案網址。
目前本地網關的 /v1/files 上傳路由尚未實作。首次串接請使用本頁的 URL 或 Base64 輸入方式;只有已確認部署環境支援上傳,且已取得同帳戶可讀取的檔案 ID 時,才使用 file_id 範例。
內容區塊類型
| 類型 | 說明 |
|---|---|
text | 隨媒體輸入附上的文字提示 |
image | 透過 URL、Base64 或檔案 ID 輸入圖像 |
document | 透過 URL、Base64 或檔案 ID 輸入文件(PDF、純文字等) |
圖像分析
透過 URL 載入圖像
json
{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "url",
"url": "https://example.com/image.jpg"
}
},
{
"type": "text",
"text": "Describe this image"
}
]
}
]
}透過 Base64 載入圖像
json
{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": "/9j/4AAQSkZJRg..."
}
},
{
"type": "text",
"text": "What objects are visible in this image?"
}
]
}
]
}文件分析
分析 PDF 文件:
json
{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "document",
"source": {
"type": "url",
"url": "https://example.com/report.pdf"
}
},
{
"type": "text",
"text": "Summarize the key findings in this document."
}
]
}
]
}請求範例
執行方式見多語言範例說明。先設定 API_KEY,並替換模型、檔案網址及 ID 占位值;四種方式會顯示相同請求的原始回應。
bash
curl --fail-with-body --silent --show-error --max-time 180 \
--request POST \
--url "https://api.tokatlas.ai/v1/messages" \
--header "x-api-key: $API_KEY" \
--header "anthropic-version: 2023-06-01" \
--header "Content-Type: application/json" \
--data '{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "url",
"url": "https://example.com/image.jpg"
}
},
{
"type": "text",
"text": "Describe this image"
}
]
}
]
}'python
import os
import requests
headers = {
'x-api-key': os.environ["API_KEY"],
'anthropic-version': '2023-06-01',
'Content-Type': 'application/json',
}
payload = {'model': 'claude-sonnet-4-6',
'max_tokens': 1024,
'messages': [{'role': 'user',
'content': [{'type': 'image',
'source': {'type': 'url',
'url': 'https://example.com/image.jpg'}},
{'type': 'text', 'text': 'Describe this image'}]}]}
response = requests.request(
'POST', 'https://api.tokatlas.ai/v1/messages', headers=headers,
json=payload,
timeout=180,
)
response.raise_for_status()
print(response.text)js
if (!process.env.API_KEY) throw new Error("Set API_KEY first.");
const response = await fetch("https://api.tokatlas.ai/v1/messages", {
method: "POST",
headers: {
"x-api-key": process.env.API_KEY,
"anthropic-version": "2023-06-01",
"Content-Type": "application/json",
},
body: JSON.stringify({
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "url",
"url": "https://example.com/image.jpg"
}
},
{
"type": "text",
"text": "Describe this image"
}
]
}
]
}),
signal: AbortSignal.timeout(180_000),
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${await response.text()}`);
}
console.log(await response.text());java
import java.net.URI;
import java.net.http.*;
import java.time.Duration;
public class Example {
public static void main(String[] args) throws Exception {
String apiKey = System.getenv("API_KEY");
if (apiKey == null || apiKey.isBlank()) {
throw new IllegalArgumentException("Set API_KEY first.");
}
String payload = String.join("\n",
"{",
" \"model\": \"claude-sonnet-4-6\",",
" \"max_tokens\": 1024,",
" \"messages\": [",
" {",
" \"role\": \"user\",",
" \"content\": [",
" {",
" \"type\": \"image\",",
" \"source\": {",
" \"type\": \"url\",",
" \"url\": \"https://example.com/image.jpg\"",
" }",
" },",
" {",
" \"type\": \"text\",",
" \"text\": \"Describe this image\"",
" }",
" ]",
" }",
" ]",
"}"
);
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(30)).build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://api.tokatlas.ai/v1/messages"))
.timeout(Duration.ofSeconds(180))
.header("x-api-key", apiKey)
.header("anthropic-version", "2023-06-01")
.header("Content-Type", "application/json")
.method("POST", HttpRequest.BodyPublishers.ofString(payload))
.build();
HttpResponse<String> response = client.send(
request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() < 200 || response.statusCode() >= 300) {
throw new IllegalStateException("HTTP " + response.statusCode() + ": "
+ response.body());
}
System.out.println(response.body());
}
}回應範例
json
{
"code": 200,
"data": {
"id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "This image shows a scenic landscape with a boardwalk path through a green meadow under a blue sky with scattered clouds."
}
],
"model": "claude-sonnet-4-6",
"stop_reason": "end_turn",
"usage": {
"input_tokens": 1520,
"output_tokens": 42
}
}
}