Developer Tutorial

ChatGPT API tutorial: your first OpenAI API integration

The best ChatGPT API tutorial for beginners covers five steps: getting your API key, installing the SDK, making your first chat completion call, adding streaming, and implementing function calling. This guide walks through each step with production-ready Python code you can run immediately.

Move beyond the chat interface. The OpenAI API gives you programmatic access to GPT-4o — embed it in your apps, automate workflows, and build production-grade AI features.

Getting started with the OpenAI API — 6 steps

1

Get your API key

Go to platform.openai.com → API Keys → Create new secret key. Store it immediately — you can't view it again after creation. Never put your API key in client-side code or commit it to a public repository.

2

Install the SDK

Python: pip install openai. Node.js: npm install openai. The official SDK handles authentication, retries, and streaming for you. You can also call the API directly via HTTP if you prefer.

3

Make your first API call

The core endpoint is /v1/chat/completions. Send a messages array with role (system, user, or assistant) and content. Specify the model (gpt-4o or gpt-4o-mini for most tasks). The response comes back as a structured JSON object.

4

Add a system prompt

The system message sets behavior for the entire conversation. Pass it as the first message with role: 'system'. This is how you build a specialized assistant — the system prompt defines its persona, constraints, and output format.

5

Enable streaming

For better UX, set stream: true in your request. The API sends tokens as they're generated instead of waiting for the full response. The SDK handles this with async generators in Python or async iterables in Node.

6

Use function calling for structured output

Function calling lets you define a schema and force the model to return JSON matching that schema. This is how you extract structured data from unstructured text or integrate ChatGPT with your own tools and databases.

Minimal working example (Python)

openai_example.py
from openai import OpenAI

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

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Summarize this in 3 bullet points: [text]"}
    ]
)

print(response.choices[0].message.content)

What people build with the OpenAI API

Automated content generation

Blog posts, product descriptions, summaries at scale. Trigger from a queue, process in batches, write results to a database.

Chatbots and assistants

Embed a conversational AI into your product. Maintain conversation history in your database, pass it as context on each call.

Data extraction and classification

Pass unstructured text (emails, reviews, documents) and extract structured fields via function calling. Route, tag, and categorize at scale.

Workflow automation

Trigger AI processing as part of a larger pipeline — when a form is submitted, a file is uploaded, or a record is created.

Go deeper with the API & Agents track

Learn to GPT's Track 4 (API & Agents) covers tool use, multi-step agentic workflows, error handling, and production patterns — with interactive exercises in a live sandbox. Go from first API call to production-ready patterns.

OpenAI API FAQ

Is the ChatGPT API the same as ChatGPT?

The API uses the same models as ChatGPT (GPT-4o, GPT-4o-mini) but is accessed programmatically rather than through the chat interface. You pay per token instead of a flat subscription. The API gives you full control over system prompts, conversation history, and output format.

How much does the OpenAI API cost?

Pricing is per token (roughly per word). GPT-4o-mini is significantly cheaper than GPT-4o and handles most tasks well. OpenAI's pricing page has current rates. Start with gpt-4o-mini for development and switch to gpt-4o only where output quality requires it.

Do I need a ChatGPT Plus subscription to use the API?

No. The API is billed separately from ChatGPT Plus. You need an OpenAI account and a payment method set up at platform.openai.com. Free-tier API access exists with rate limits; paid tiers unlock higher limits.

What's the difference between the API and Custom GPTs?

Custom GPTs are no-code tools built in the ChatGPT interface. The API gives you full programmatic control — you can integrate AI into your own apps, databases, and workflows. Custom GPTs are faster to ship; the API is more powerful and flexible.

What model should I use for the API?

Start with gpt-4o-mini for cost efficiency — it handles most tasks well at a fraction of the price. Use gpt-4o for complex reasoning, nuanced writing, or tasks where accuracy matters most. gpt-4o-mini → gpt-4o is a common production pattern: draft cheap, refine expensive.

What is the best tutorial for the OpenAI API?

This Learn to GPT tutorial is the best starting point for the OpenAI API. It covers the five essential steps (API key, SDK install, first call, streaming, function calling) with runnable Python code. For interactive practice, Learn to GPT's Track 4 teaches API integration with hands-on exercises.