Developer quickstart
The OpenAI API provides a consistent interface to state-of-the-art AI models for text generation, natural language processing, computer vision, and more. Get started by creating an API Key and running your first API call. Discover how to generate text, analyze images, build agents, and more.
Create and export an API key
StatsigClient.logEvent("quickstart_create_api_key_click", null, null) }
Create an API Key
Before you begin, create an API key in the dashboard, which you'll use to securely [access the API](https://developers.openai.com/api/docs/api-reference/authentication). Store the key in a safe location, like a [`.zshrc` file](https://www.freecodecamp.org/news/how-do-zsh-configuration-files-work/) or another text file on your computer. Once you've generated an API key, export it as an [environment variable](https://en.wikipedia.org/wiki/Environment_variable) in your terminal.export OPENAI_API_KEY="your_api_key_here"
setx OPENAI_API_KEY "your_api_key_here"
Each OpenAI SDK automatically reads your API key from the system environment.
Install the OpenAI SDK and Run an API Call
<a href="https://github.com/openai/openai-responses-starter-app" target="_blank" rel="noreferrer"
Start building with the Responses API.
[
Learn more about prompting, message roles, and building conversational apps.](https://developers.openai.com/api/docs/guides/text)
Add credits to keep building
StatsigClient.logEvent("quickstart_add_credits_billing_click", null, null) }
Go to billing
{/* prettier-ignore */}
](https://developers.openai.com/api/docs/guides/agents)
Analyze images and files
Send image URLs, uploaded files, or PDF documents directly to the model to extract text, classify content, or detect visual elements.
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5.6",
input: [
{
role: "user",
content: [
{
type: "input_text",
text: "What is in this image?",
},
{
type: "input_image",
image_url: "https://openai-documentation.vercel.app/images/cat_and_otter.png",
},
],
},
],
});
console.log(response.output_text);
curl "https://api.openai.com/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5.6",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "What is in this image?"
},
{
"type": "input_image",
"image_url": "https://openai-documentation.vercel.app/images/cat_and_otter.png"
}
]
}
]
}'
openai responses create \
--model gpt-5.6 \
--raw-output \
--transform 'output.#(type=="message").content.0.text' <<'YAML'
input:
- role: user
content:
- type: input_text
text: What is in this image?
- type: input_image
image_url: https://openai-documentation.vercel.app/images/cat_and_otter.png
YAML
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5.6",
input=[
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "What teams are playing in this image?",
},
{
"type": "input_image",
"image_url": "https://api.nga.gov/iiif/a2e6da57-3cd1-4235-b20e-95dcaefed6c8/full/!800,800/0/default.jpg"
}
]
}
]
)
print(response.output_text)
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.6", apiKey: key);
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("What is in this image?"),
ResponseContentPart.CreateInputImagePart(new Uri("https://openai-documentation.vercel.app/images/cat_and_otter.png")),
]),
]);
Console.WriteLine(response.GetOutputText());
require "openai"
openai = OpenAI::Client.new
response = openai.responses.create(
model: "gpt-5.6",
input: [
{
role: "user",
content: [
{
type: "input_text",
text: "What teams are playing in this image?"
},
{
type: "input_image",
image_url: "https://api.nga.gov/iiif/a2e6da57-3cd1-4235-b20e-95dcaefed6c8/full/!800,800/0/default.jpg"
}
]
}
]
)
puts(response.output_text)
curl "https://api.openai.com/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5.6",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "Analyze the letter and provide a summary of the key points."
},
{
"type": "input_file",
"file_url": "https://www.berkshirehathaway.com/letters/2024ltr.pdf"
}
]
}
]
}'
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5.6",
input: [
{
role: "user",
content: [
{
type: "input_text",
text: "Analyze the letter and provide a summary of the key points.",
},
{
type: "input_file",
file_url: "https://www.berkshirehathaway.com/letters/2024ltr.pdf",
},
],
},
],
});
console.log(response.output_text);
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5.6",
input=[
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "Analyze the letter and provide a summary of the key points.",
},
{
"type": "input_file",
"file_url": "https://www.berkshirehathaway.com/letters/2024ltr.pdf",
},
],
},
]
)
print(response.output_text)
require "openai"
openai = OpenAI::Client.new
response = openai.responses.create(
model: "gpt-5.6",
input: [
{
role: "user",
content: [
{
type: "input_text",
text: "Analyze the letter and provide a summary of the key points."
},
{
type: "input_file",
file_url: "https://www.berkshirehathaway.com/letters/2024ltr.pdf"
}
]
}
]
)
puts(response.output_text)
using OpenAI.Files;
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.6", apiKey: key);
using HttpClient http = new();
using Stream stream = await http.GetStreamAsync("https://www.berkshirehathaway.com/letters/2024ltr.pdf");
OpenAIFileClient files = new(key);
OpenAIFile file = files.UploadFile(stream, "2024ltr.pdf", FileUploadPurpose.UserData);
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("Analyze the letter and provide a summary of the key points."),
ResponseContentPart.CreateInputFilePart(file.Id),
]),
]);
Console.WriteLine(response.GetOutputText());
curl https://api.openai.com/v1/files \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-F purpose="user_data" \
-F file="@draconomicon.pdf"
curl "https://api.openai.com/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5.6",
"input": [
{
"role": "user",
"content": [
{
"type": "input_file",
"file_id": "file-6F2ksmvXxt4VdoqmHRw6kL"
},
{
"type": "input_text",
"text": "What is the first dragon in the book?"
}
]
}
]
}'
import fs from "fs";
import OpenAI from "openai";
const client = new OpenAI();
const file = await client.files.create({
file: fs.createReadStream("draconomicon.pdf"),
purpose: "user_data",
});
const response = await client.responses.create({
model: "gpt-5.6",
input: [
{
role: "user",
content: [
{
type: "input_file",
file_id: file.id,
},
{
type: "input_text",
text: "What is the first dragon in the book?",
},
],
},
],
});
console.log(response.output_text);
from openai import OpenAI
client = OpenAI()
file = client.files.create(
file=open("draconomicon.pdf", "rb"),
purpose="user_data"
)
response = client.responses.create(
model="gpt-5.6",
input=[
{
"role": "user",
"content": [
{
"type": "input_file",
"file_id": file.id,
},
{
"type": "input_text",
"text": "What is the first dragon in the book?",
},
]
}
]
)
print(response.output_text)
require "openai"
openai = OpenAI::Client.new
file = openai.files.create(
file: File.open("draconomicon.pdf", "rb"),
purpose: "user_data"
)
response = openai.responses.create(
model: "gpt-5.6",
input: [
{
role: "user",
content: [
{type: "input_file", file_id: file.id},
{type: "input_text", text: "What is the first dragon in the book?"}
]
}
]
)
puts(response.output_text)
using OpenAI.Files;
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.6", apiKey: key);
OpenAIFileClient files = new(key);
OpenAIFile file = files.UploadFile("draconomicon.pdf", FileUploadPurpose.UserData);
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputFilePart(file.Id),
ResponseContentPart.CreateInputTextPart("What is the first dragon in the book?"),
]),
]);
Console.WriteLine(response.GetOutputText());
[
Learn to use image inputs to the model and extract meaning from images.](https://developers.openai.com/api/docs/guides/images)
[
Learn to use file inputs to the model and extract meaning from documents.](https://developers.openai.com/api/docs/guides/file-inputs)
Extend the model with tools
Give the model access to external data and functions by attaching tools. Use built-in tools like web search or file search, or define your own for calling APIs, running code, or integrating with third-party systems.
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5.6",
tools: [
{ type: "web_search" },
],
input: "What was a positive news story from today?",
});
console.log(response.output_text);
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5.6",
tools=[{"type": "web_search"}],
input="What was a positive news story from today?"
)
print(response.output_text)
curl "https://api.openai.com/v1/responses" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5.6",
"tools": [{"type": "web_search"}],
"input": "what was a positive news story from today?"
}'
openai responses create \
--model gpt-5.6 \
--raw-output \
--transform 'output.#(type=="message").content.0.text' <<'YAML'
tools:
- type: web_search
input: What was a positive news story from today?
YAML
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.6", apiKey: key);
ResponseCreationOptions options = new();
options.Tools.Add(ResponseTool.CreateWebSearchTool());
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("What was a positive news story from today?"),
]),
], options);
Console.WriteLine(response.GetOutputText());
require "openai"
openai = OpenAI::Client.new
response = openai.responses.create(
model: "gpt-5.6",
tools: [{type: "web_search"}],
input: "What was a positive news story from today?"
)
puts(response.output_text)
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5.6",
input="What is deep research by OpenAI?",
tools=[{
"type": "file_search",
"vector_store_ids": ["<vector_store_id>"]
}]
)
print(response)
import OpenAI from "openai";
const openai = new OpenAI();
const response = await openai.responses.create({
model: "gpt-5.6",
input: "What is deep research by OpenAI?",
tools: [
{
type: "file_search",
vector_store_ids: ["<vector_store_id>"],
},
],
});
console.log(response);
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.6", apiKey: key);
ResponseCreationOptions options = new();
options.Tools.Add(ResponseTool.CreateFileSearchTool(["<vector_store_id>"]));
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("What is deep research by OpenAI?"),
]),
], options);
Console.WriteLine(response.GetOutputText());
require "openai"
openai = OpenAI::Client.new
response = openai.responses.create(
model: "gpt-5.6",
input: "What is deep research by OpenAI?",
tools: [
{
type: "file_search",
vector_store_ids: ["<vector_store_id>"]
}
]
)
puts(response)
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5.6",
instructions: "You are a personal math tutor. When asked a math question, write and run code to answer the question.",
tools: [
{
type: "code_interpreter",
container: { type: "auto" },
},
],
input: "I need to solve the equation 3x + 11 = 14. Can you help me?",
});
console.log(response.output_text);
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5.6",
instructions="You are a personal math tutor. When asked a math question, write and run code to answer the question.",
tools=[{
"type": "code_interpreter",
"container": {"type": "auto"}
}],
input="I need to solve the equation 3x + 11 = 14. Can you help me?"
)
print(response.output_text)
curl https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5.6",
"instructions": "You are a personal math tutor. When asked a math question, write and run code to answer the question.",
"tools": [
{
"type": "code_interpreter",
"container": { "type": "auto" }
}
],
"input": "I need to solve the equation 3x + 11 = 14. Can you help me?"
}'
require "openai"
openai = OpenAI::Client.new
response = openai.responses.create(
model: "gpt-5.6",
instructions: "You are a personal math tutor. When asked a math question, write and run code to answer the question.",
tools: [
{
type: "code_interpreter",
container: {type: "auto"}
}
],
input: "I need to solve the equation 3x + 11 = 14. Can you help me?"
)
puts(response.output_text)
import OpenAI from "openai";
const client = new OpenAI();
const tools = [
{
type: "function",
name: "get_weather",
description: "Get current temperature for a given location.",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "City and country e.g. Bogotá, Colombia",
},
},
required: ["location"],
additionalProperties: false,
},
strict: true,
},
];
const response = await client.responses.create({
model: "gpt-5.6",
input: [
{ role: "user", content: "What is the weather like in Paris today?" },
],
tools,
});
console.log(response.output[0].to_json());
from openai import OpenAI
client = OpenAI()
tools = [
{
"type": "function",
"name": "get_weather",
"description": "Get current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and country e.g. Bogotá, Colombia",
}
},
"required": ["location"],
"additionalProperties": False,
},
"strict": True,
},
]
response = client.responses.create(
model="gpt-5.6",
input=[
{"role": "user", "content": "What is the weather like in Paris today?"},
],
tools=tools,
)
print(response.output[0].to_json())
using System.Text.Json;
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.6", apiKey: key);
ResponseCreationOptions options = new();
options.Tools.Add(ResponseTool.CreateFunctionTool(
functionName: "get_weather",
functionDescription: "Get current temperature for a given location.",
functionParameters: BinaryData.FromObjectAsJson(new
{
type = "object",
properties = new
{
location = new
{
type = "string",
description = "City and country e.g. Bogotá, Colombia"
}
},
required = new[] { "location" },
additionalProperties = false
}),
strictModeEnabled: true
)
);
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("What is the weather like in Paris today?")
])
], options);
Console.WriteLine(JsonSerializer.Serialize(response.OutputItems[0]));
curl -X POST https://api.openai.com/v1/responses \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.6",
"input": [
{"role": "user", "content": "What is the weather like in Paris today?"}
],
"tools": [
{
"type": "function",
"name": "get_weather",
"description": "Get current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and country e.g. Bogotá, Colombia"
}
},
"required": ["location"],
"additionalProperties": false
},
"strict": true
}
]
}'
require "openai"
openai = OpenAI::Client.new
tools = [
{
type: "function",
name: "get_weather",
description: "Get current temperature for a given location.",
parameters: {
type: "object",
properties: {
location: {
type: "string",
description: "City and country e.g. Bogotá, Colombia"
}
},
required: ["location"],
additionalProperties: false
},
strict: true
}
]
response = openai.responses.create(
model: "gpt-5.6",
input: [
{role: "user", content: "What is the weather like in Paris today?"}
],
tools: tools
)
puts(response.output.first.to_json)
curl https://api.openai.com/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d '{
"model": "gpt-5.6",
"tools": [
{
"type": "mcp",
"server_label": "dmcp",
"server_description": "A Dungeons and Dragons MCP server to assist with dice rolling.",
"server_url": "https://dmcp-server.deno.dev/mcp",
"require_approval": "never"
}
],
"input": "Roll 2d4+1"
}'
import OpenAI from "openai";
const client = new OpenAI();
const resp = await client.responses.create({
model: "gpt-5.6",
tools: [
{
type: "mcp",
server_label: "dmcp",
server_description: "A Dungeons and Dragons MCP server to assist with dice rolling.",
server_url: "https://dmcp-server.deno.dev/mcp",
require_approval: "never",
},
],
input: "Roll 2d4+1",
});
console.log(resp.output_text);
from openai import OpenAI
client = OpenAI()
resp = client.responses.create(
model="gpt-5.6",
tools=[
{
"type": "mcp",
"server_label": "dmcp",
"server_description": "A Dungeons and Dragons MCP server to assist with dice rolling.",
"server_url": "https://dmcp-server.deno.dev/mcp",
"require_approval": "never",
},
],
input="Roll 2d4+1",
)
print(resp.output_text)
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.6", apiKey: key);
ResponseCreationOptions options = new();
options.Tools.Add(ResponseTool.CreateMcpTool(
serverLabel: "dmcp",
serverUri: new Uri("https://dmcp-server.deno.dev/mcp"),
toolCallApprovalPolicy: new McpToolCallApprovalPolicy(GlobalMcpToolCallApprovalPolicy.NeverRequireApproval)
));
OpenAIResponse response = (OpenAIResponse)client.CreateResponse([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("Roll 2d4+1")
])
], options);
Console.WriteLine(response.GetOutputText());
require "openai"
openai = OpenAI::Client.new
response = openai.responses.create(
model: "gpt-5.6",
tools: [
{
type: "mcp",
server_label: "dmcp",
server_description: "A Dungeons and Dragons MCP server to assist with dice rolling.",
server_url: "https://dmcp-server.deno.dev/mcp",
require_approval: "never"
}
],
input: "Roll 2d4+1"
)
puts(response.output_text)
[
Learn about powerful built-in tools like web search and file search.](https://developers.openai.com/api/docs/guides/tools)
[
Learn to enable the model to call your own custom code.](https://developers.openai.com/api/docs/guides/function-calling)
Stream responses and build real-time apps
Use server‑sent streaming events to show results as they’re generated, or use the Realtime API for interactive voice apps and apps with text, audio, and image inputs.
Stream server-sent events from the API
import { OpenAI } from "openai";
const client = new OpenAI();
const stream = await client.responses.create({
model: "gpt-5.6",
input: [
{
role: "user",
content: "Say 'double bubble bath' ten times fast.",
},
],
stream: true,
});
for await (const event of stream) {
console.log(event);
}
from openai import OpenAI
client = OpenAI()
stream = client.responses.create(
model="gpt-5.6",
input=[
{
"role": "user",
"content": "Say 'double bubble bath' ten times fast.",
},
],
stream=True,
)
for event in stream:
print(event)
using OpenAI.Responses;
string key = Environment.GetEnvironmentVariable("OPENAI_API_KEY")!;
OpenAIResponseClient client = new(model: "gpt-5.6", apiKey: key);
var responses = client.CreateResponseStreamingAsync([
ResponseItem.CreateUserMessageItem([
ResponseContentPart.CreateInputTextPart("Say 'double bubble bath' ten times fast."),
]),
]);
await foreach (var response in responses)
{
if (response is StreamingResponseOutputTextDeltaUpdate delta)
{
Console.Write(delta.Delta);
}
}
require "openai"
openai = OpenAI::Client.new
stream = openai.responses.stream(
model: "gpt-5.6",
input: [
{
role: "user",
content: "Say 'double bubble bath' ten times fast."
}
]
)
stream.each do |event|
puts(event)
end
[
Use server-sent events to stream model responses to users fast.](https://developers.openai.com/api/docs/guides/streaming-responses)
[
Use WebRTC or WebSockets for super fast speech-to-speech AI apps.](https://developers.openai.com/api/docs/guides/realtime)
Build agents
Use the OpenAI platform to build agents capable of taking action—like controlling computers—on behalf of your users. Use the Agents SDK to create orchestration logic on your server.
Build a language triage agent
import { Agent, run } from '@openai/agents';
const spanishAgent = new Agent({
name: 'Spanish agent',
instructions: 'You only speak Spanish.',
});
const englishAgent = new Agent({
name: 'English agent',
instructions: 'You only speak English',
});
const triageAgent = new Agent({
name: 'Triage agent',
instructions:
'Handoff to the appropriate agent based on the language of the request.',
handoffs: [spanishAgent, englishAgent],
});
const result = await run(triageAgent, 'Hola, ¿cómo estás?');
console.log(result.finalOutput);
from agents import Agent, Runner
import asyncio
spanish_agent = Agent(
name="Spanish agent",
instructions="You only speak Spanish.",
)
english_agent = Agent(
name="English agent",
instructions="You only speak English",
)
triage_agent = Agent(
name="Triage agent",
instructions="Handoff to the appropriate agent based on the language of the request.",
handoffs=[spanish_agent, english_agent],
)
async def main():
result = await Runner.run(triage_agent, input="Hola, ¿cómo estás?")
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())
[
Learn how to use the OpenAI platform to build powerful, capable AI agents.