Developer quickstart
The OpenAI API provides a simple 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
Create an API KeyBefore you begin, create an API key in the dashboard, which you'll use to securely access the API. Store the key in a safe location, like a .zshrc file or another text file on your computer. Once you've generated an API key, export it as an environment variable in your terminal.
1
export OPENAI_API_KEY="your_api_key_here"1
setx OPENAI_API_KEY "your_api_key_here"OpenAI SDKs are configured to automatically read your API key from the system environment.
Install the OpenAI SDK and Run an API Call
To use the OpenAI API in server-side JavaScript environments like Node.js, Deno, or Bun, you can use the official OpenAI SDK for TypeScript and JavaScript. Get started by installing the SDK using npm or your preferred package manager:
1
npm install openaiWith the OpenAI SDK installed, create a file called example.mjs and copy the example code into it:
1
2
3
4
5
6
7
8
9
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5.2",
input: "Write a one-sentence bedtime story about a unicorn."
});
console.log(response.output_text);Execute the code with node example.mjs (or the equivalent command for Deno or Bun). In a few moments, you should see the output of your API request.
Discover more SDK capabilities and options on the library's GitHub README.
To use the OpenAI API in Python, you can use the official OpenAI SDK for Python. Get started by installing the SDK using pip:
1
pip install openaiWith the OpenAI SDK installed, create a file called example.py and copy the example code into it:
1
2
3
4
5
6
7
8
9
from openai import OpenAI
client = OpenAI()
response = client.responses.create(
model="gpt-5.2",
input="Write a one-sentence bedtime story about a unicorn."
)
print(response.output_text)Execute the code with python example.py. In a few moments, you should see the output of your API request.
Discover more SDK capabilities and options on the library's GitHub README.
In collaboration with Microsoft, OpenAI provides an officially supported API client for C#. You can install it with the .NET CLI from NuGet.
dotnet add package OpenAIA simple API request to the Responses API would look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Threading.Tasks;
using OpenAI;
class Program
{
static async Task Main()
{
var client = new OpenAIClient(
Environment.GetEnvironmentVariable("OPENAI_API_KEY")
);
var response = await client.Responses.CreateAsync(new ResponseCreateRequest
{
Model = "gpt-5.2",
Input = "Say 'this is a test.'"
});
Console.WriteLine($"[ASSISTANT]: {response.OutputText()}");
}
}To learn more about using the OpenAI API in .NET, check out the GitHub repo linked below!
Discover more SDK capabilities and options on the library's GitHub README.
OpenAI provides an API helper for the Java programming language, currently in beta. You can include the Maven dependency using the following configuration:
1
2
3
4
5
<dependency>
<groupId>com.openai</groupId>
<artifactId>openai-java</artifactId>
<version>4.0.0</version>
</dependency>A simple API request to Responses API would look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.responses.Response;
import com.openai.models.responses.ResponseCreateParams;
public class Main {
public static void main(String[] args) {
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
ResponseCreateParams params = ResponseCreateParams.builder()
.input("Say this is a test")
.model("gpt-5.2")
.build();
Response response = client.responses().create(params);
System.out.println(response.outputText());
}
}To learn more about using the OpenAI API in Java, check out the GitHub repo linked below!
Discover more SDK capabilities and options on the library's GitHub README.
OpenAI provides an API helper for the Go programming language, currently in beta. You can import the library using the code below:
1
2
3
import (
"github.com/openai/openai-go" // imported as openai
)A simple API request to the Responses API would look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package main
import (
"context"
"fmt"
"github.com/openai/openai-go/v3"
"github.com/openai/openai-go/v3/option"
"github.com/openai/openai-go/v3/responses"
)
func main() {
client := openai.NewClient(
option.WithAPIKey("My API Key"), // or set OPENAI_API_KEY in your env
)
resp, err := client.Responses.New(context.TODO(), openai.ResponseNewParams{
Model: "gpt-5.2",
Input: responses.ResponseNewParamsInputUnion{OfString: openai.String("Say this is a test")},
})
if err != nil {
panic(err.Error())
}
fmt.Println(resp.OutputText())
}To learn more about using the OpenAI API in Go, check out the GitHub repo linked below!
Discover more SDK capabilities and options on the library's GitHub README.
Start building with the Responses API.
Learn more about prompting, message roles, and building conversational apps.
Add credits to keep building
Go to billingCongrats on running a free test API request! Start building real applications with higher limits and use our models to generate text, audio, images, videos and more.
Build & test conversational prompts and embed them in your app.
Build, deploy, and optimize agent workflows.
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5",
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);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5",
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);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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",
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);Learn to use image inputs to the model and extract meaning from images.
Learn to use file inputs to the model and extract meaning from documents.
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.
1
2
3
4
5
6
7
8
9
10
11
12
import OpenAI from "openai";
const client = new OpenAI();
const response = await client.responses.create({
model: "gpt-5",
tools: [
{ type: "web_search" },
],
input: "What was a positive news story from today?",
});
console.log(response.output_text);1
2
3
4
5
6
7
8
9
10
11
12
13
14
import OpenAI from "openai";
const openai = new OpenAI();
const response = await openai.responses.create({
model: "gpt-4.1",
input: "What is deep research by OpenAI?",
tools: [
{
type: "file_search",
vector_store_ids: ["<vector_store_id>"],
},
],
});
console.log(response);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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",
input: [
{ role: "user", content: "What is the weather like in Paris today?" },
],
tools,
});
console.log(response.output[0].to_json());1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import OpenAI from "openai";
const client = new OpenAI();
const resp = await client.responses.create({
model: "gpt-5",
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/sse",
require_approval: "never",
},
],
input: "Roll 2d4+1",
});
console.log(resp.output_text);Learn about powerful built-in tools like web search and file search.
Learn to enable the model to call your own custom code.
Stream responses and build realtime apps
Use server‑sent streaming events to show results as they’re generated, or the Realtime API for interactive voice and multimodal apps.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { OpenAI } from "openai";
const client = new OpenAI();
const stream = await client.responses.create({
model: "gpt-5",
input: [
{
role: "user",
content: "Say 'double bubble bath' ten times fast.",
},
],
stream: true,
});
for await (const event of stream) {
console.log(event);
}Use server-sent events to stream model responses to users fast.
Use WebRTC or WebSockets for super fast speech-to-speech AI apps.
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 for Python or TypeScript to create orchestration logic on the backend.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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);Learn how to use the OpenAI platform to build powerful, capable AI agents.