Przejdź do treści
Dokumentacja API
Dokumentacja API

Anthropic Messages

Wyślij żądanie przez operację Messages kompatybilną z Anthropic w RunAPI.

01

Omówienie

Użyj operacji Messages zgodnej z Anthropic w RunAPI z kompatybilnym modelem oraz kształtami żądania i odpowiedzi tego protokołu.

Szybki start

  1. Utwórz klucz API i ustaw go jako RUNAPI_API_KEY.
  2. Wybierz zgodny model, a następnie wyślij udokumentowane parametry ścieżki i treść JSON.
  3. Odczytaj odpowiedź JSON lub zdarzenia wysyłane przez serwer pokazane dla tej operacji i obsłuż błędy protokołu.

Punkt końcowy

POST /v1/messages
Bazowy URL
https://runapi.ai
Wersja kontraktu
v1
Preferowane uwierzytelnianie
x-api-key: YOUR_API_TOKEN
02

Nośniki uwierzytelniania

RunAPI akceptuje każdy z wymienionych poniżej protokołów. Wygenerowane przykłady używają preferowanego nagłówka danego protokołu.

header
Authorization: Bearer YOUR_API_TOKEN
header - Preferowane
x-api-key: YOUR_API_TOKEN
header
x-goog-api-key: YOUR_API_TOKEN
query
?key=YOUR_API_TOKEN
03

Zgodne modele

Otwórz stronę modelu, aby zapoznać się z aktualnymi cenami, limitami żądań i szczegółami dotyczącymi użytku komercyjnego.

Pokaż 58 kompatybilnych modeli
04

Kontrakt żądania

Treść JSON

Wymieniane są tylko pola mające zarówno publiczną dokumentację protokołu, jak i potwierdzenie obsługi przez RunAPI. Dodatkowe pola treści JSON są przekazywane bez zmian.

Treść JSON10 pola
max_tokensinteger
Wymagane

Maksymalna liczba tokenów do wygenerowania przed zatrzymaniem.

messagesarray
Wymagane

Tury rozmowy w formacie Anthropic Messages.

messages[].content["string", "array"]
Wymagane
messages[].rolestring
Wymagane
modelstring
Wymagane

Identyfikator modelu RunAPI zwrócony przez kompatybilny katalog modeli.

streamboolean
Opcjonalne

Zwróć typowane zdarzenia Messages jako zdarzenia wysyłane przez serwer.

Domyślna wartość: false
system["string", "array"]
Opcjonalne

Instrukcje systemowe przekazywane osobno od wiadomości konwersacyjnych.

temperaturenumber
Opcjonalne

Temperatura próbkowania; obsługa przez model może się różnić.

toolsarray
Opcjonalne

Narzędzia, które model może wywoływać.

top_pnumber
Opcjonalne

Prawdopodobieństwo próbkowania jądra; obsługa przez model może być różna.

Przykład żądania

JSON
{
  "max_tokens": 64,
  "messages": [
    {
      "content": "Summarize one practical improvement for an API deployment.",
      "role": "user"
    }
  ],
  "model": "claude-sonnet-4-6"
}
05

Odpowiedź synchroniczna

HTTP 200

Schemat

JSON
{
  "additionalProperties": true,
  "properties": {
    "content": {
      "type": "array"
    },
    "id": {
      "type": "string"
    },
    "model": {
      "type": "string"
    },
    "role": {
      "const": "assistant",
      "type": "string"
    },
    "stop_reason": {
      "type": [
        "string",
        "null"
      ]
    },
    "type": {
      "const": "message",
      "type": "string"
    },
    "usage": {
      "additionalProperties": true,
      "properties": {
        "input_tokens": {
          "type": "integer"
        },
        "output_tokens": {
          "type": "integer"
        }
      },
      "required": [
        "input_tokens",
        "output_tokens"
      ],
      "type": "object"
    }
  },
  "required": [
    "id",
    "type",
    "role",
    "content",
    "model",
    "stop_reason",
    "usage"
  ],
  "type": "object"
}

Przykład

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

Zdarzenie SSE: message_start

text/event-stream

Schemat

JSON
{
  "additionalProperties": true,
  "properties": {
    "message": {
      "type": "object"
    },
    "type": {
      "const": "message_start",
      "type": "string"
    }
  },
  "required": [
    "type",
    "message"
  ],
  "type": "object"
}

Przykład

JSON
{
  "message": {
    "content": [],
    "id": "msg_example",
    "model": "claude-sonnet-4-6",
    "role": "assistant",
    "stop_reason": null,
    "stop_sequence": null,
    "type": "message",
    "usage": {
      "input_tokens": 12,
      "output_tokens": 1
    }
  },
  "type": "message_start"
}
07

Zdarzenie SSE: content_block_delta

text/event-stream

Schemat

JSON
{
  "additionalProperties": true,
  "properties": {
    "delta": {
      "type": "object"
    },
    "index": {
      "type": "integer"
    },
    "type": {
      "const": "content_block_delta",
      "type": "string"
    }
  },
  "required": [
    "type",
    "index",
    "delta"
  ],
  "type": "object"
}

Przykład

JSON
{
  "delta": {
    "text": "Hello",
    "type": "text_delta"
  },
  "index": 0,
  "type": "content_block_delta"
}
08

Zdarzenie SSE: message_delta

text/event-stream

Schemat

JSON
{
  "additionalProperties": true,
  "properties": {
    "delta": {
      "type": "object"
    },
    "type": {
      "const": "message_delta",
      "type": "string"
    },
    "usage": {
      "additionalProperties": true,
      "properties": {
        "output_tokens": {
          "type": "integer"
        }
      },
      "required": [
        "output_tokens"
      ],
      "type": "object"
    }
  },
  "required": [
    "type",
    "delta",
    "usage"
  ],
  "type": "object"
}

Przykład

JSON
{
  "delta": {
    "stop_reason": "end_turn",
    "stop_sequence": null
  },
  "type": "message_delta",
  "usage": {
    "output_tokens": 8
  }
}
09

Zdarzenie SSE: message_stop

text/event-stream

Schemat

JSON
{
  "additionalProperties": true,
  "properties": {
    "type": {
      "const": "message_stop",
      "type": "string"
    }
  },
  "required": [
    "type"
  ],
  "type": "object"
}

Przykład

JSON
{
  "type": "message_stop"
}
10

Błąd protokołu: invalid_request

HTTP 400

Schemat

JSON
{
  "additionalProperties": true,
  "properties": {
    "error": {
      "additionalProperties": true,
      "properties": {
        "message": {
          "type": "string"
        },
        "type": {
          "type": "string"
        }
      },
      "required": [
        "type",
        "message"
      ],
      "type": "object"
    },
    "request_id": {
      "type": "string"
    },
    "type": {
      "const": "error",
      "type": "string"
    }
  },
  "required": [
    "type",
    "error"
  ],
  "type": "object"
}

Przykład

JSON
{
  "error": {
    "message": "max_tokens is required",
    "type": "invalid_request_error"
  },
  "request_id": "req_example",
  "type": "error"
}
11

Użycie

Schemat

JSON
{
  "additionalProperties": true,
  "properties": {
    "input_tokens": {
      "type": "integer"
    },
    "output_tokens": {
      "type": "integer"
    }
  },
  "required": [
    "input_tokens",
    "output_tokens"
  ],
  "type": "object"
}

Przykład

JSON
{
  "input_tokens": 12,
  "output_tokens": 8
}
12

Wygenerowane przykłady kodu

Każdy przykład cURL, JavaScript i Python wysyła zwalidowane żądanie z tej specyfikacji bez konieczności używania SDK właściwego dla protokołu.

Instalacja

Shell
pip install requests
PYTHON
import requests

response = requests.post(
    "https://runapi.ai/v1/messages",
    headers={"x-api-key": "YOUR_API_TOKEN", "Content-Type": "application/json"},
    json={"model": "claude-sonnet-4-6", "max_tokens": 64, "messages": [{"role": "user", "content": "Summarize one practical improvement for an API deployment."}]}
)
response.raise_for_status()
print(response.json())