پرش لینک ها
مشاهده
بکشید

Developer quickstart

Learn how to make your first API request.

The OpenAI API provides a simple interface to state-of-the-art AI models for natural language processing, image generation, semantic search, and speech recognition. Follow this guide to learn how to generate human-like responses to natural language prompts, create vector embeddings for semantic search, and generate images from textual descriptions.

Create and export an API key

Create an API key in the dashboard here, 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.

macOS / Linux

Export an environment variable on macOS or Linux systems:

export OPENAI_API_KEY="your_api_key_here"

Windows

Export an environment variable in PowerShell:

setx OPENAI_API_KEY "your_api_key_here"

Make your first API request

With your OpenAI API key exported as an environment variable, you’re ready to make your first API request. You can either use the REST API directly with the HTTP client of your choice, or use one of our official SDKs as shown below.

JavaScript

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:

npm install openai

With the OpenAI SDK installed, create a file called example.mjs and copy one of the following examples into it:

Generate text

import OpenAI from "openai";
const openai = new OpenAI();

const completion = await openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [
        { role: "system", content: "You are a helpful assistant." },
        { role: "user", content: "Write a haiku about recursion in programming." },
    ],
    store: true,
});

console.log(completion.choices[0].message);

Generate an image

import OpenAI from "openai";
const openai = new OpenAI();

const image = await openai.images.generate({ prompt: "A cute baby sea otter" });

console.log(image.data[0].url);

Create vector embeddings

import OpenAI from "openai";
const openai = new OpenAI();

const embedding = await openai.embeddings.create({
    model: "text-embedding-3-large",
    input: "The quick brown fox jumped over the lazy dog",
});

console.log(embedding);

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!

Python

To use the OpenAI API in Python, install the SDK using pip:

pip install openai

With the OpenAI SDK installed, create a file called example.py and copy one of the following examples into it:

Generate text

from openai import OpenAI
client = OpenAI()

completion = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Write a haiku about recursion in programming."}
    ]
)

print(completion.choices[0].message)

Generate an image

from openai import OpenAI
client = OpenAI()

response = client.images.generate(
    prompt="A cute baby sea otter",
    n=2,
    size="1024x1024"
)

print(response.data[0].url)

Create vector embeddings

from openai import OpenAI
client = OpenAI()

response = client.embeddings.create(
    model="text-embedding-3-large",
    input="The food was delicious and the waiter..."
)

print(response)

Execute the code with python example.py. In a few moments, you should see the output of your API request!

Next steps

Now that you’ve made your first OpenAI API request, explore the following resources:

این وب سایت از کوکی ها برای بهبود تجربه وب شما استفاده می کند.