← Back to API List

Create Message with Claude 3 Opus

POST/v1/messagesAnthropic

Send a structured list of input messages with text and/or image content, and the model will generate the next message in the conversation.

text generationclaudeopusreasoningcomplex
Official Documentation

Headers

Content-TypestringRequired

Set to `application/json`.

x-api-keystringRequired

Your Anthropic API key.

anthropic-versionstringRequired

API version to use.

Request Body

modelstringRequired

The model that will complete your prompt.

Example: claude-3-opus-20240229

top_pnumber

Use nucleus sampling.

Example: 0.9

messagesarray of objectRequired

Input messages for the conversation.

max_tokensintegerRequired

The maximum number of tokens to generate before stopping.

Example: 1024

temperaturenumber

Amount of randomness injected into the response.

Defaults to: 1

Example: 0.7

stop_sequencesarray of string

Custom text sequences that will cause the model to stop generating.

Response Body

idstring

Unique object identifier.

rolestring

Conversational role, always "assistant".

typestring

Object type, always "message".

modelstring

The model that handled the request.

usageobject

contentarray of object

stop_reasonstring

The reason that we stopped.

stop_sequencestring

Which custom stop sequence was generated, if any.

Code Examples

cURL

cURL
curl https://api.anthropic.com/v1/messages \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-3-opus-20240229",
    "max_tokens": 1024,
    "messages": [
      {"role": "user", "content": "Hello, Claude"}
    ]
  }'

Python

Python
import anthropic

client = anthropic.Anthropic(
    api_key="YOUR_ANTHROPIC_API_KEY",
)

message = client.messages.create(
    model="claude-3-opus-20240229",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello, Claude"}
    ]
)
print(message.content[0].text)

Example Response

JSON

JSON
{
  "id": "msg_013Zva2CMHLNnXjNJJKqJ2EF",
  "type": "message",
  "role": "assistant",
  "content": [
    {
      "type": "text",
      "text": "Hello! It's nice to meet you. I'm Claude, an AI assistant created by Anthropic. How can I help you today?"
    }
  ],
  "model": "claude-3-opus-20240229",
  "stop_reason": "end_turn",
  "stop_sequence": null,
  "usage": {
    "input_tokens": 10,
    "output_tokens": 25
  }
}