Home › OpenAI API tutorial for beginners
OpenAI API tutorial for beginners
Every OpenAI API project starts the same way: a key in an environment variable, the official SDK, and one call to the Responses API.
Sign up and log in
- Sign in to the OpenAI developer platform and open the API keys page in the dashboard.
- Create an API key and store it somewhere safe.
- 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).
- Install the official SDK: npm install openai, or pip install openai.
- The SDK reads the key from the environment automatically.
Step by step
- Create an API key in the dashboard and export it as OPENAI_API_KEY.
- Install the SDK: npm install openai (JavaScript) or pip install openai (Python).
- Create a client: const client = new OpenAI(); it picks up the key by itself.
- Call client.responses.create({ model: "gpt-6-astra", input: "…" }).
- Print response.output_text, then change the input and run it again.
Try it: examples
- First input
Write a one-sentence bedtime story about a unicorn.
- A useful first task
Summarise this customer review in one line and label it positive, neutral or negative: [paste review]
Good to know
- Keep the key out of code you share or commit.
- Pick a smaller model (for example GPT-6 Luna) for simple, high-volume jobs; the price per token is much lower.
- Set billing and usage alerts in the dashboard so a bug can't run up a surprise bill.
Prices (US$)
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.
Sources (official pages, read Nov 8, 2026)
Written from OpenAI's official documentation and product pages; we don't run hands-on tests. Features and limits vary by plan and country.