AI/ML

Using OpenAI’s API in Under 10 Minutes

This quick-start guide shows how to connect to OpenAI’s API and get your first GPT-4 response in under 10 minutes using a few lines of Python.

May 25, 2025

Photo by Visax on Unsplash

Want to get started with OpenAI's API but feel like it might be complicated?

Good news: you can get it working in under 10 minutes with just a few lines of Python.

Here's a quick guide to help you go from zero to your first response.

Step 1: Install the OpenAI Python Library

If you haven’t already, install the OpenAI client library:

pip install openai

Step 2: Get Your API Key

Head over to platform.openai.com and grab your secret API key.

You’ll need this to authenticate your requests.

Important: Never hardcode your API key into your script. Store it safely as an environment variable instead.

Step 3: Write Your First Script

Here’s a simple Python script to send a prompt and get a response using GPT-4:

import os
import openai

# Set your API key as an environment variable: OPENAI_API_KEY
openai.api_key = os.getenv("OPENAI_API_KEY")

response = openai.ChatCompletion.create(
    model="gpt-4",
    messages=[
        {"role": "user", "content": "Write a haiku about coffee."}
    ]
)

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

That’s it! Run the script and you’ll get a haiku about coffee back in seconds.

Step 4: Customize Your Prompt

You can change the prompt to anything you like:

{"role": "user", "content": "Summarize the plot of Star Wars in one sentence."}

Or even create a back-and-forth conversation by adding more message objects:

[
  {"role": "system", "content": "You are a helpful assistant."},
  {"role": "user", "content": "What is the capital of France?"},
  {"role": "assistant", "content": "The capital of France is Paris."},
  {"role": "user", "content": "What is the population of Paris?"}
]

Done in 10 Minutes

No fancy setup, no complicated config.

Just install the library, set your API key, and you’re ready to chat with one of the most powerful AI models out there.

Want to go deeper? Try tweaking temperature, max tokens, or adding function calling next.