- Get started
- Introduction
- Authentication
- Making requests
- Streaming
- Audio
- Batch
- Models
- Invites
- Chat
- Users
- Projects
- Project users
- Project service accounts
- Project API keys
- Audit logs
- Usage
- Project rate limits
- Completions
- Uploads
- Embeddings
- Files
- Moderations
- Assistants (v1)
- Threads (v1)
- Messages (v1)
- Runs (v1)
- Images
- Fine-tuning
Create chat completion
POST
https://api.openai.com/v1/chat/completions
Request
Authorization
Provide your bearer token in the
Authorization
header when making requests to protected resources.Example:
Authorization: Bearer ********************
Header Params
Content-Type
stringÂ
required
Example:
application/json
Accept
stringÂ
required
Example:
application/json
Authorization
stringÂ
optional
Example:
Bearer {{YOUR_API_KEY}}
Body Params application/json
model
stringÂ
required
messages
array [object {2}]Â
required
role
stringÂ
optional
system
, user
, assistant
, or function
.content
stringÂ
optional
content
 is required for all messages except assistant messages with function calls.temperature
integerÂ
optional
top_p
 but not both.top_p
integerÂ
optional
temperature
 but not both.n
integerÂ
optional
stream
booleanÂ
optional
data: [DONE]
 message. Example Python code.stop
stringÂ
optional
max_tokens
integerÂ
optional
presence_penalty
numberÂ
optional
frequency_penalty
numberÂ
optional
logit_bias
nullÂ
optional
user
stringÂ
optional
Example
{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "Hello!"
}
]
}
Request samples
Shell
JavaScript
Java
Swift
Go
PHP
Python
HTTP
C
C#
Objective-C
Ruby
OCaml
Dart
R
Request Request Example
Shell
JavaScript
Java
Swift
curl --location --request POST 'https://api.openai.com/v1/chat/completions' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data-raw '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello!"}]
}'
Responses
🟢200OK
application/json
Body
id
stringÂ
required
object
stringÂ
required
created
integerÂ
required
choices
array [object {3}]Â
required
index
integerÂ
optional
message
objectÂ
optional
finish_reason
stringÂ
optional
usage
objectÂ
required
prompt_tokens
integerÂ
required
completion_tokens
integerÂ
required
total_tokens
integerÂ
required
Example
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "\n\nHello there, how may I assist you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}
Modified at 2024-12-13 10:20:37