Full access is free during Beta. A paid subscription will be offered after Beta.

OpenAI API — User Guide

OpenAI API; GPT-4o.

Visit website
Freemium
Strengths
  • Provide top models such as GPT-4o, o1, DALL·E 3, Whisper, etc.
  • The API documentation is complete and the community ecosystem is the richest
  • Supports advanced functions such as Function Calling and JSON Mode
  • Assistants API to build AI assistants with memory
  • Pay by token, flexible billing method
Best for
  • Build AI chatbots and conversational apps
  • NLP tasks such as text generation, summarization, and translation
  • Image generation (DALL·E 3)
  • Speech to text (Whisper)
  • Build AI Agents and automated workflows

quick start

After registering an account and obtaining the API Key, you can call GPT-4o within a few minutes.

Scenario

First API call

Prompt example
# Install the OpenAI Python library
pip install openai

# Python code example
from openai import OpenAI

client = OpenAI(api_key="your-api-key")

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": "You are a helpful assistant"},
        {"role": "user", "content": "Explain what machine learning is in one sentence"}
    ]
)

print(response.choices[0].message.content)
Output / what to expect

The output is similar to: “Machine learning allows computers to automatically learn patterns from data. without the need for explicit programming techniques. "

The response object also contains:

  • usage.prompt_tokens: enter the number of tokens
  • usage.completion_tokens: output token number
  • model: model version used
Tips

The API Key should be kept properly and should not be submitted to the Git repository. It is recommended to use environment variable storage.

Scenario

Streaming

Prompt example
from openai import OpenAI

client = OpenAI(api_key="your-api-key")

# Streaming output, displayed verbatim
stream = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Write a poem about spring"}],
    stream=True
)

for chunk in stream:
    if chunk.choices[0].delta.content is not None:
        print(chunk.choices[0].delta.content, end="", flush=True)
Output / what to expect
The text will be output word by word like a typewriter, Better user experience, Suitable for scenarios such as chat interfaces that require real-time response.
Tips

Streaming output can significantly improve user experience, especially in long text generation scenarios.

Starter & above

The rest of this guide

Additional scenarios and the full comparison table are included with Starter and above. Sign in with an eligible account to load them.

You're on the Free plan. Upgrade to Starter or higher to unlock the rest of this guide—additional scenarios and the full comparison table.

Loading full guide…