Home › OpenAI API course: 3 beginner lessons

OpenAI API course: 3 beginner lessons

Last checked

What it does

The OpenAI API lets your own code use OpenAI models for text, images, audio and agents, through the Responses API, billed per token.

Who it's for: Developers who want OpenAI models inside their own app, website or script.

Made by OpenAI.

Sign up and log in
  1. Sign in to the OpenAI developer platform and open the API keys page in the dashboard.
  2. Create an API key and store it somewhere safe.
  3. Export it as an environment variable: export OPENAI_API_KEY="your_api_key_here" (macOS/Linux) or setx OPENAI_API_KEY "your_api_key_here" (Windows).
  4. Install the official SDK: npm install openai, or pip install openai.
  5. The SDK reads the key from the environment automatically.

Per million tokens on openai.com/api (standard rates under 272K context): GPT-6.1 Sol US$2.00 input / US$10.00 output, GPT-6.1 Terra US$1.00 / US$5.00, GPT-6 Luna US$0.10 / US$0.50, GPT-6 Astra US$10.00 / US$50.00.

Lesson 1: Create a key and make your first call

Every OpenAI API project starts the same way: a key in an environment variable, the official SDK, and one call to the Responses API..

  1. Create an API key in the dashboard and export it as OPENAI_API_KEY.
  2. Install the SDK: npm install openai (JavaScript) or pip install openai (Python).
  3. Create a client: const client = new OpenAI(); it picks up the key by itself.
  4. Call client.responses.create({ model: "gpt-6-astra", input: "…" }).
  5. Print response.output_text, then change the input and run it again.

Open lesson 1 →

Lesson 2: Live answers with web search and structured output

Two built-in features make answers usable inside an app: the web_search tool for current information with citations, and Structured Outputs so the reply always matches your JSON schema..

  1. Add tools: [{ type: "web_search" }] to a Responses API call.
  2. Read the url_citation annotations (URL, title, location) and show the sources to your users.
  3. For JSON, define a schema with Pydantic (Python) or Zod (JavaScript).
  4. Pass it as text_format (Python parse helper) or with zodTextFormat (JavaScript).
  5. Use the parsed object in your code instead of parsing free text.

Open lesson 2 →

Lesson 3: Cut costs in half with the Batch API

When you don't need answers right away (tagging thousands of reviews, overnight reports, evaluations), the Batch API runs the requests for you at a 50% discount..

  1. Write one request per line in a .jsonl file, each with a custom_id so you can match results later.
  2. Upload the file with purpose "batch".
  3. Create the batch with the file ID, the endpoint (for example /v1/responses) and completion_window "24h".
  4. Check the batch status until it is completed.
  5. Download the output file and match each result to its custom_id.

Open lesson 3 →

Sources

Start Lesson 1 →

Also: All OpenAI products · ChatGPT course · ChatGPT features and prices · All tutorials

Report an error on this page