The OpenAI API can be applied to virtually any task. We offer a spectrum of models with different levels of power suitable for different tasks, as well as the ability to fine-tune custom models.
https://api.openai.com/v1Authorization HTTP header:
Authorization: Bearer OPENAI_API_KEYOrganization (optional) For users who belong to multiple organizations, you can pass a header to specify which organization is used. Usage from these API requests will count against the specified organization's quota.
OpenAI-Organization: org-xxxxxxx
$OPENAI_API_KEY with your secret API key.
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Hello!"}]
}'
This request queries the gpt-4o model to complete the text starting with "Hello!". You should get a response back that resembles the following:
{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1677858242,
"model": "gpt-4o-2024-08-06",
"choices": [{
"message": {
"role": "assistant",
"content": "Hello! How can I assist you today?"
},
"finish_reason": "stop",
"index": 0
}],
"usage": {
"prompt_tokens": 9,
"completion_tokens": 12,
"total_tokens": 21
}
}stream: true in your request body. Partial message deltas will be sent as data: [JSON] events, with the stream terminated by a data: [DONE] message.
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-4o",
"stream": true,
"messages": [{"role": "user", "content": "Hello!"}]
}'Given a list of messages comprising a conversation, the model will return a response. Related guide: Chat Completions.
/v1/chat/completionsCreates a model response for the given chat conversation. See the text generation guide and the code generation guide for more information.
| Name | Type | Description | |
|---|---|---|---|
model | string | Required | ID of the model to use. See the model endpoint compatibility table for details on which models work with the Chat API. |
messages | array | Required | A list of messages comprising the conversation so far. Each message is an object with a role (system, user, assistant) and content. |
temperature | number | Optional | What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. Defaults to 1. |
top_p | number | Optional | An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. Defaults to 1. |
n | integer | Optional | How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all choices. Defaults to 1. |
stream | boolean | Optional | If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as data-only server-sent events as they become available. Defaults to false. |
max_tokens | integer | null | Optional | The maximum number of tokens that can be generated in the chat completion. The total length of input tokens and generated tokens is limited by the model's context length. |
response_format | object | Optional | An object specifying the format that the model must output. Setting to { "type": "json_object" } enables JSON mode. |
tools | array | Optional | A list of tools the model may call. Currently, only functions are supported as a tool. Use this to provide a list of functions the model may generate JSON inputs for. |
tool_choice | string | object | Optional | Controls which (if any) tool is called by the model. "none" means the model will not call any tool. "auto" means the model can pick between generating a message or calling one or more tools. |
curl https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-4o",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Hello!"
}
]
}'{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1709123456,
"model": "gpt-4o-2024-08-06",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I assist you today?"
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 19,
"completion_tokens": 10,
"total_tokens": 29
},
"system_fingerprint": "fp_44709d6fcb"
}Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms. Related guide: Embeddings.
/v1/embeddingsCreates an embedding vector representing the input text.
| Name | Type | Description | |
|---|---|---|---|
input | string | array | Required | Input text to embed, encoded as a string or array of tokens. To embed multiple inputs in a single request, pass an array of strings or array of token arrays. |
model | string | Required | ID of the model to use. You can use the List models API to see all available models, or see our Model overview for descriptions of them. |
encoding_format | string | Optional | The format to return the embeddings in. Can be either "float" or "base64". Defaults to "float". |
dimensions | integer | Optional | The number of dimensions the resulting output embeddings should have. Only supported in text-embedding-3 and later models. |
curl https://api.openai.com/v1/embeddings \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"input": "The food was delicious and the waiter...",
"model": "text-embedding-3-small"
}'{
"object": "list",
"data": [
{
"object": "embedding",
"embedding": [
0.0023064255,
-0.009327292,
-0.0028842222,
...
],
"index": 0
}
],
"model": "text-embedding-3-small",
"usage": {
"prompt_tokens": 8,
"total_tokens": 8
}
}Given a prompt and/or an input image, the model will generate a new image. Related guide: Image generation.
/v1/images/generationsCreates an image given a prompt.
| Name | Type | Description | |
|---|---|---|---|
prompt | string | Required | A text description of the desired image(s). The maximum length is 4000 characters for dall-e-3. |
model | string | Optional | The model to use for image generation. Defaults to "dall-e-3". |
n | integer | null | Optional | The number of images to generate. Must be between 1 and 10. For dall-e-3, only n=1 is supported. Defaults to 1. |
quality | string | Optional | The quality of the image that will be generated. "hd" creates images with finer details and greater consistency. Defaults to "standard". |
response_format | string | null | Optional | The format in which the generated images are returned. Must be one of "url" or "b64_json". Defaults to "url". |
size | string | null | Optional | The size of the generated images. Must be one of "1024x1024", "1792x1024", or "1024x1792" for dall-e-3. Defaults to "1024x1024". |
style | string | null | Optional | The style of the generated images. Must be one of "vivid" or "natural". Vivid causes the model to lean towards generating hyper-real and dramatic images. Defaults to "vivid". |
curl https://api.openai.com/v1/images/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "dall-e-3",
"prompt": "A white siamese cat",
"n": 1,
"size": "1024x1024"
}'{
"created": 1709375436,
"data": [
{
"revised_prompt": "A serene white Siamese cat with striking blue almond-shaped eyes...",
"url": "https://oaidalleapiprodscus.blob.core.windows.net/..."
}
]
}Learn how to turn audio into text or text into audio. Related guide: Speech to text.
/v1/audio/speechGenerates audio from the input text.
| Name | Type | Description | |
|---|---|---|---|
model | string | Required | One of the available TTS models: tts-1 or tts-1-hd. |
input | string | Required | The text to generate audio for. The maximum length is 4096 characters. |
voice | string | Required | The voice to use when generating the audio. Supported voices are alloy, echo, fable, onyx, nova, and shimmer. |
response_format | string | Optional | The format to return audio in. Supported formats are mp3, opus, aac, flac, wav, and pcm. Defaults to "mp3". |
speed | number | Optional | The speed of the generated audio. Select a value from 0.25 to 4.0. Defaults to 1.0. |
curl https://api.openai.com/v1/audio/speech \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "tts-1",
"input": "The quick brown fox jumped over the lazy dog.",
"voice": "alloy"
}' \
--output speech.mp3# The response is the audio file content.
# The response headers include:
Content-Type: audio/mpeg
Transfer-Encoding: chunked/v1/audio/transcriptionsTranscribes audio into the input language.
| Name | Type | Description | |
|---|---|---|---|
file | file | Required | The audio file object to transcribe, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. |
model | string | Required | ID of the model to use. Only whisper-1 is currently available. |
language | string | Optional | The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. |
prompt | string | Optional | An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language. |
response_format | string | Optional | The format of the transcript output. Options are json, text, srt, verbose_json, or vtt. Defaults to "json". |
temperature | number | Optional | The sampling temperature, between 0 and 1. Defaults to 0. |
curl https://api.openai.com/v1/audio/transcriptions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: multipart/form-data" \
-F file="@/path/to/file/audio.mp3" \
-F model="whisper-1"{
"text": "Imagine the wildest idea that you've ever had, and you're curious about how it might scale to something that's a 100, a 1,000 times bigger."
}Given text and/or image inputs, classifies if those inputs are potentially harmful across several categories. Related guide: Moderations.
/v1/moderationsClassifies if text and/or image inputs are potentially harmful. Use this to check whether content complies with OpenAI's usage policies.
| Name | Type | Description | |
|---|---|---|---|
input | string | array | Required | Input (or inputs) to classify. Can be a single string, an array of strings, or an array of multi-modal input objects. |
model | string | Optional | The content moderation model you would like to use. Defaults to "omni-moderation-latest". |
curl https://api.openai.com/v1/moderations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"input": "I want to kill them."
}'{
"id": "modr-abc123",
"model": "omni-moderation-latest",
"results": [
{
"flagged": true,
"categories": {
"sexual": false,
"hate": false,
"harassment": false,
"self-harm": false,
"sexual/minors": false,
"hate/threatening": false,
"violence/graphic": false,
"self-harm/intent": false,
"self-harm/instructions": false,
"harassment/threatening": true,
"violence": true
},
"category_scores": {
"sexual": 1.2282071e-06,
"hate": 0.010696256,
"harassment": 0.29842457,
"self-harm": 1.5236925e-08,
"violence": 0.99011886,
"harassment/threatening": 0.63055265,
"violence/graphic": 1.0209e-05
}
}
]
}Manage fine-tuning jobs to tailor a model to your specific training data. Related guide: Fine-tuning models.
/v1/fine_tuning/jobsCreates a fine-tuning job which begins the process of creating a new model from a given dataset.
| Name | Type | Description | |
|---|---|---|---|
training_file | string | Required | The ID of an uploaded file that contains training data. Your dataset must be formatted as a JSONL file. You can upload files with the Files API. |
model | string | Required | The name of the model to fine-tune. You can select one of the supported models. |
hyperparameters | object | Optional | The hyperparameters used for the fine-tuning job. |
suffix | string | null | Optional | A string of up to 18 characters that will be added to your fine-tuned model name. |
validation_file | string | null | Optional | The ID of an uploaded file that contains validation data. |
curl https://api.openai.com/v1/fine_tuning/jobs \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"training_file": "file-BK7bzQj3FfZFXr7DbL6xJwfo",
"model": "gpt-4o-mini-2024-07-18"
}'{
"object": "fine_tuning.job",
"id": "ftjob-abc123",
"model": "gpt-4o-mini-2024-07-18",
"created_at": 1692661014,
"finished_at": null,
"fine_tuned_model": null,
"organization_id": "org-abc123",
"result_files": [],
"status": "validating_files",
"validation_file": null,
"training_file": "file-BK7bzQj3FfZFXr7DbL6xJwfo",
"hyperparameters": {
"n_epochs": "auto"
},
"trained_tokens": null,
"error": {}
}/v1/fine_tuning/jobsList your organization's fine-tuning jobs.
| Name | Type | Description | |
|---|---|---|---|
after | string | Optional | Identifier for the last job from the previous pagination request. |
limit | integer | Optional | Number of fine-tuning jobs to retrieve. Defaults to 20. |
curl https://api.openai.com/v1/fine_tuning/jobs?limit=2 \
-H "Authorization: Bearer $OPENAI_API_KEY"{
"object": "list",
"data": [
{
"object": "fine_tuning.job",
"id": "ftjob-abc123",
"model": "gpt-4o-mini-2024-07-18",
"created_at": 1692661014,
"finished_at": 1692661190,
"fine_tuned_model": "ft:gpt-4o-mini-2024-07-18:my-org::7q8XXYZY",
"status": "succeeded",
"trained_tokens": 5768
}
],
"has_more": true
}Files are used to upload documents that can be used with features like Assistants, Fine-tuning, and Batch API.
/v1/filesUpload a file that can be used across various endpoints. Individual files can be up to 512 MB, and the size of all files uploaded by one organization can be up to 100 GB.
| Name | Type | Description | |
|---|---|---|---|
file | file | Required | The File object to be uploaded. |
purpose | string | Required | The intended purpose of the uploaded file. Use "assistants" for Assistants and Message files, "fine-tune" for Fine-tuning, "vision" for Assistants image file inputs, and "batch" for Batch API. |
curl https://api.openai.com/v1/files \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F purpose="fine-tune" \
-F file="@mydata.jsonl"{
"id": "file-BK7bzQj3FfZFXr7DbL6xJwfo",
"object": "file",
"bytes": 120000,
"created_at": 1677610602,
"filename": "mydata.jsonl",
"purpose": "fine-tune"
}/v1/filesReturns a list of files that belong to the user's organization.
| Name | Type | Description | |
|---|---|---|---|
purpose | string | Optional | Only return files with the given purpose. |
curl https://api.openai.com/v1/files \
-H "Authorization: Bearer $OPENAI_API_KEY"{
"data": [
{
"id": "file-abc123",
"object": "file",
"bytes": 175,
"created_at": 1613677385,
"filename": "train.jsonl",
"purpose": "fine-tune"
},
{
"id": "file-def456",
"object": "file",
"bytes": 140,
"created_at": 1613779121,
"filename": "puppy.jsonl",
"purpose": "fine-tune"
}
],
"object": "list"
}/v1/files/{file_id}Delete a file.
| Name | Type | Description | |
|---|---|---|---|
file_id | string | Required | The ID of the file to use for this request. |
curl https://api.openai.com/v1/files/file-abc123 \
-X DELETE \
-H "Authorization: Bearer $OPENAI_API_KEY"{
"id": "file-abc123",
"object": "file",
"deleted": true
}List and describe the various models available in the API. You can refer to the Models documentation to understand which models are available and the differences between them.
/v1/modelsLists the currently available models, and provides basic information about each one such as the owner and availability.
curl https://api.openai.com/v1/models \
-H "Authorization: Bearer $OPENAI_API_KEY"{
"object": "list",
"data": [
{
"id": "gpt-4o",
"object": "model",
"created": 1686935002,
"owned_by": "system"
},
{
"id": "gpt-4o-mini",
"object": "model",
"created": 1721172741,
"owned_by": "system"
},
{
"id": "dall-e-3",
"object": "model",
"created": 1698785189,
"owned_by": "system"
}
]
}/v1/models/{model}Retrieves a model instance, providing basic information about the model such as the owner and permissioning.
| Name | Type | Description | |
|---|---|---|---|
model | string | Required | The ID of the model to use for this request. |
curl https://api.openai.com/v1/models/gpt-4o \
-H "Authorization: Bearer $OPENAI_API_KEY"{
"id": "gpt-4o",
"object": "model",
"created": 1686935002,
"owned_by": "system"
}/v1/models/{model}Delete a fine-tuned model. You must have the Owner role in your organization to delete a model.
| Name | Type | Description | |
|---|---|---|---|
model | string | Required | The model to delete. |
curl https://api.openai.com/v1/models/ft:gpt-4o-mini:acemeco:suffix:abc123 \
-X DELETE \
-H "Authorization: Bearer $OPENAI_API_KEY"{
"id": "ft:gpt-4o-mini:acemeco:suffix:abc123",
"object": "model",
"deleted": true
}Build assistants that can call models and use tools to perform tasks. Related guide: Assistants overview.
/v1/assistantsCreate an assistant with a model and instructions.
| Name | Type | Description | |
|---|---|---|---|
model | string | Required | ID of the model to use. |
name | string | null | Optional | The name of the assistant. The maximum length is 256 characters. |
description | string | null | Optional | The description of the assistant. The maximum length is 512 characters. |
instructions | string | null | Optional | The system instructions that the assistant uses. The maximum length is 256,000 characters. |
tools | array | Optional | A list of tools enabled on the assistant. There can be a maximum of 128 tools per assistant. Tools can be of types code_interpreter, file_search, or function. |
metadata | object | null | Optional | Set of 16 key-value pairs that can be attached to an object. This can be useful for storing additional information about the object in a structured format. |
curl https://api.openai.com/v1/assistants \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "OpenAI-Beta: assistants=v2" \
-d '{
"instructions": "You are a personal math tutor.",
"name": "Math Tutor",
"tools": [{"type": "code_interpreter"}],
"model": "gpt-4o"
}'{
"id": "asst_abc123",
"object": "assistant",
"created_at": 1698984975,
"name": "Math Tutor",
"description": null,
"model": "gpt-4o",
"instructions": "You are a personal math tutor.",
"tools": [
{ "type": "code_interpreter" }
],
"metadata": {},
"top_p": 1.0,
"temperature": 1.0,
"response_format": "auto"
}/v1/assistantsReturns a list of assistants.
| Name | Type | Description | |
|---|---|---|---|
limit | integer | Optional | A limit on the number of objects to be returned. Limit can range between 1 and 100. Defaults to 20. |
order | string | Optional | Sort order by the created_at timestamp of the objects. "asc" for ascending order and "desc" for descending order. Defaults to "desc". |
after | string | Optional | A cursor for use in pagination. after is an object ID that defines your place in the list. |
before | string | Optional | A cursor for use in pagination. before is an object ID that defines your place in the list. |
curl https://api.openai.com/v1/assistants?order=desc&limit=20 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "OpenAI-Beta: assistants=v2"{
"object": "list",
"data": [
{
"id": "asst_abc123",
"object": "assistant",
"created_at": 1698982736,
"name": "Coding Helper",
"description": null,
"model": "gpt-4o",
"instructions": "You are a helpful coding assistant.",
"tools": [],
"metadata": {}
},
{
"id": "asst_abc456",
"object": "assistant",
"created_at": 1698982718,
"name": "My Assistant",
"description": null,
"model": "gpt-4o",
"instructions": "You are a helpful assistant.",
"tools": [],
"metadata": {}
}
],
"first_id": "asst_abc123",
"last_id": "asst_abc456",
"has_more": false
}