Enterprise DNA

Omni by Enterprise DNA

Enterprise DNA Resources

Step-by-step how-tos. Practical AI operating-system thinking for owners, operators, and teams doing real work.

220k+

Data professionals

Omni

AI agents and apps

Audit

Map the manual work

Guide Intermediate General

OpenAI API Tutorial A Practical Beginners Walkthrough

A working OpenAI API tutorial for developers and analysts. Real setup steps, real code, and the settings that actually matter for a first integration.

Sam McKay |
OpenAI API Tutorial A Practical Beginners Walkthrough

What The OpenAI API Actually Is

Strip away the marketing and the OpenAI API is a hosted HTTP endpoint that takes input from your application and returns model-generated output. You send text (or in newer models, images and audio), OpenAI runs it through a large language model on their infrastructure, and you get a response back. You pay per token consumed on both the input and the output.

The same family of models that powers ChatGPT is available through the API, but the two surfaces are different products. ChatGPT is a polished chat app with a UI, memory features, and tool integrations built in. The API gives you raw access to the underlying models so you can build features inside your own software. You are not “calling ChatGPT” when you use the API, you are calling a specific model like gpt-4o or one of the o-series reasoning models.

At the time of writing the API exposes a handful of endpoints worth knowing:

  • Chat completions for conversational and instruction following tasks
  • Responses, the newer endpoint that is gradually replacing chat completions for many use cases
  • Embeddings for turning text into vectors for search and similarity
  • Audio transcription and speech generation
  • Image generation through a separate model
  • Fine tuning endpoints for adapting a base model to your own data

If you only learn one endpoint on day one, learn chat completions or responses. The other surfaces build on the same authentication, the same SDK, and the same billing model.

Setup And Authentication

The setup path is short but it has a few gotchas that waste hours if you miss them.

Step 1, create an account and grab a key. Sign up at platform.openai.com, then go to the API keys section under settings. Generate a new secret key and copy it immediately. OpenAI only shows the full key once. Store it in a password manager or a secrets vault, not in a config file that gets committed to git.

Step 2, set up billing. New accounts get a small credit but you need to add a payment method before you can use paid models. Set a usage limit in the billing dashboard, this is the single best protection against an accidental bill from a runaway script.

Step 3, install the SDK. The official SDKs are thin wrappers around the REST API. Pick the one that matches your stack.

For Python: pip install openai

For Node: npm install openai

For most other languages there are community maintained clients, or you can hit the REST endpoints directly with whatever HTTP library you already use.

Step 4, set the API key as an environment variable. Never hardcode it. On macOS and Linux: export OPENAI_API_KEY=“sk-…”

On Windows PowerShell: $env:OPENAI_API_KEY=“sk-…”

If you want persistence across shells, add the export line to your .zshrc, .bashrc, or the equivalent on your platform.

Step 5, verify the connection. A quick sanity check before writing any real code:

curl https://api.openai.com/v1/models -H “Authorization: Bearer $OPENAI_API_KEY”

If you get back a JSON list of model IDs, your key works and your network path is clear. If you get a 401, the key is wrong. If you get a 429, you are being rate limited or you have not added billing yet.

Your First Working Example

Here is a minimal Python script that calls the chat completions endpoint and prints the response. Run it as is and you should get a coherent answer back.

import os from openai import OpenAI

client = OpenAI()

response = client.chat.completions.create( model=“gpt-4o-mini”, messages=[ {“role”: “system”, “content”: “You are a concise assistant.”}, {“role”: “user”, “content”: “Explain what an API key does in two sentences.”} ] )

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

A few things to notice in this example.

The OpenAI client picks up the OPENAI_API_KEY environment variable automatically. You do not need to pass it explicitly.

The model is gpt-4o-mini, which is the cheap workhorse model. It costs a fraction of larger models and is the right starting point for almost any prototype. You can swap in gpt-4o or an o-series reasoning model later when you know you need the extra capability.

The messages array is a list of turns in the conversation. The system role sets behaviour and tone, the user role is the actual prompt, and assistant is what the model previously said. You can include multiple user and assistant messages to give the model context from a longer conversation.

The response object contains more than just the text. response.choices[0].message.content is the generated text. response.usage tells you how many prompt and completion tokens you consumed, which is what gets billed. response.model tells you which model actually answered, useful when you have configured fallbacks.

If the call fails you get an exception, not a silent empty string. Wrap the call in a try block and log the error. Network blips, rate limits, and content policy rejections are all things you want surfaced, not swallowed.

Settings That Actually Matter

The API exposes a long list of parameters and most of them you can ignore for the first month. A small handful change the behaviour in ways you will actually feel--- title: “OpenAI API Tutorial A Practical Beginners Walkthrough” description: “A working OpenAI API tutorial for developers and analysts. Real setup steps, real code, and the settings that actually matter for a first integration.” publishDate: “2026-06-13” author: “Sam McKay” difficulty: “intermediate” service: “general” tags:

  • ai-tools
  • tutorial draft: false

What The OpenAI API Actually Is

Strip away the marketing and the OpenAI API is a hosted HTTP endpoint that takes input from your application and returns model-generated output. You send text (or in newer models, images and audio), OpenAI runs it through a large language model on their infrastructure, and you get a response back. You pay per token consumed on both the input and the output.

The same family of models that powers ChatGPT is available through the API, but the two surfaces are different products. ChatGPT is a polished chat app with a UI, memory features, and tool integrations built in. The API gives you raw access to the underlying models so you can build features inside your own software. You are not “calling ChatGPT” when you use the API, you are calling a specific model like gpt-4o or one of the o-series reasoning models.

At the time of writing the API exposes a handful of endpoints worth knowing:

  • Chat completions for conversational and instruction following tasks
  • Responses, the newer endpoint that is gradually replacing chat completions for many use cases
  • Embeddings for turning text into vectors for search and similarity
  • Audio transcription and speech generation
  • Image generation through a separate model
  • Fine tuning endpoints for adapting a base model to your own data

If you only learn one endpoint on day one, learn chat completions or responses. The other surfaces build on the same authentication, the same SDK, and the same billing model.

Setup And Authentication

The setup path is short but it has a few gotchas that waste hours if you miss them.

Step 1, create an account and grab a key. Sign up at platform.openai.com, then go to the API keys section under settings. Generate a new secret key and copy it immediately. OpenAI only shows the full key once. Store it in a password manager or a secrets vault, not in a config file that gets committed to git.

Step 2, set up billing. New accounts get a small credit but you need to add a payment method before you can use paid models. Set a usage limit in the billing dashboard, this is the single best protection against an accidental bill from a runaway script.

Step 3, install the SDK. The official SDKs are thin wrappers around the REST API. Pick the one that matches your stack.

For Python: pip install openai

For Node: npm install openai

For most other languages there are community maintained clients, or you can hit the REST endpoints directly with whatever HTTP library you already use.

Step 4, set the API key as an environment variable. Never hardcode it. On macOS and Linux: export OPENAI_API_KEY=“sk-…”

On Windows PowerShell: $env:OPENAI_API_KEY=“sk-…”

If you want persistence across shells, add the export line to your .zshrc, .bashrc, or the equivalent on your platform.

Step 5, verify the connection. A quick sanity check before writing any real code:

curl https://api.openai.com/v1/models -H “Authorization: Bearer $OPENAI_API_KEY”

If you get back a JSON list of model IDs, your key works and your network path is clear. If you get a 401, the key is wrong. If you get a 429, you are being rate limited or you have not added billing yet.

Your First Working Example

Here is a minimal Python script that calls the chat completions endpoint and prints the response. Run it as is and you should get a coherent answer back.

import os from openai import OpenAI

client = OpenAI()

response = client.chat.completions.create( model=“gpt-4o-mini”, messages=[ {“role”: “system”, “content”: “You are a concise assistant.”}, {“role”: “user”, “content”: “Explain what an API key does in two sentences.”} ] )

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

A few things to notice in this example.

The OpenAI client picks up the OPENAI_API_KEY environment variable automatically. You do not need to pass it explicitly.

The model is gpt-4o-mini, which is the cheap workhorse model. It costs a fraction of larger models and is the right starting point for almost any prototype. You can swap in gpt-4o or an o-series reasoning model later when you know you need the extra capability.

The messages array is a list of turns in the conversation. The system role sets behaviour and tone, the user role is the actual prompt, and assistant is what the model previously said. You can include multiple user and assistant messages to give the model context from a longer conversation.

The response object contains more than just the text. response.choices[0].message.content is the generated text. response.usage tells you how many prompt and completion tokens you consumed, which is what gets billed. response.model tells you which model actually answered, useful when you have configured fallbacks.

If the call fails you get an exception, not a silent empty string. Wrap the call in a try block and log the error. Network blips, rate limits, and content policy rejections are all things you want surfaced, not swallowed.

Settings That Actually Matter

The API exposes a long list of parameters and most of them you can ignore for the first month. A small handful change the behaviour in ways you will actually feel