John Baker
  • About
  • Work
  • Contact

On this page

  • Introduction
  • What You’ll Learn
  • Understanding API Security: Why Secrets Matter
  • Calling the OpenAI API from an IDE
    • Step 1: Get Your API Key 🔑
    • Step 2: Set Up Your Project Folder 📂
    • Step 3: Add Your API Key to the .env File
    • Step 4: Install the Necessary Libraries
    • Step 5: Write the Python Code 💻
  • Troubleshooting Common Issues
  • Next Steps

Calling the OpenAI API from an IDE

Educational Technology
Python Programming
API Development
Tutorial/How-To
A Student’s Guide to Secure Development
Published

October 1, 2025

Modified

October 4, 2025

Introduction

You can use Python scripting to call the OpenAI API and get a short, focused response from a chat model. While there are cloud-based environments like Google Colab, which is a hosted Jupyter Notebook service, I prefer to use a traditional software application installed directly on my computer called an integrated development environment (IDE). There are several reasons I like to work with an IDE, including having complete control over my environment, seamless integration of local files, and the ability to work offline. There are many IDEs to choose from, such as Visual Studio Code (often referred to as VS Code) and PyCharm, which offer built-in support for Jupyter Notebooks. I like to use Positron, which takes the modern, extensible foundation of VS Code and combines it with data-centric features. If you’re interested in learning how I called the OpenAI API from Positron, please read on.

What You’ll Learn

By following this guide, you’ll master several crucial skills for working with APIs in educational contexts. You’ll learn how to securely store and access API keys using environment variables, which is a fundamental practice in professional software development. You’ll learn to structure your code using functions, making your programs more reusable and maintainable. Most importantly, you’ll discover how to implement proper error handling so your programs respond gracefully when things go wrong, rather than crashing unexpectedly.

Understanding API Security: Why Secrets Matter

Before we start, it’s crucial to understand the process of storing your secret API key in a separate file, having your code securely load it without exposing the key in the script itself, and why this process is necessary. Think of your API key like a credit card number that allows you to make purchases from OpenAI’s services. Hard-coding your API key directly into your script is akin to writing your credit card number on a sticky note and leaving it on your desk, where anyone can see it.

If you accidentally share your code or upload it to a public repository like GitHub, your secret key will be exposed, allowing anyone to use your account and incur charges on your behalf. We need a strategy to keep our API keys secure.

Here is where environment variables come into play. Environment variables are like labeled containers that store information your program can access without that information being visible in your code itself. Think of them as a secure filing cabinet that only your program can open. The information lives separately from your code, so you can safely share your scripts without exposing your secrets.

Fortunately, there are several ways we can manage this securely. We’ll use a tool called dotenv, which helps you manage application secrets and configuration by loading them from a special file into your program’s environment variables.

Calling the OpenAI API from an IDE

Step 1: Get Your API Key 🔑

First, you need to get your API key from the OpenAI platform.

  1. Go to platform.openai.com and log in.
  2. Navigate to Dashboard > API keys.
  3. Click “Create new secret key” and create one if you haven’t already.
  4. Copy the key immediately and save it somewhere safe.
    1. I used 1Password for this, but any secure password manager will work.
Important

You won’t be able to see your secret key again after you close the window.

Step 2: Set Up Your Project Folder 📂

Create a new project folder. Inside that folder, create two files:

  1. main.py: This will be your Python script.
    1. You can name this file anything you want, such as chatbot.py or api_test.py.
  2. .env: This is where you’ll store your secret key. The . at the beginning makes it a hidden file on many systems, which provides an additional layer of protection.

Your folder should look like this:

my_project/
├── main.py
└── .env

Step 3: Add Your API Key to the .env File

Open the .env file you created and add your API key in the following format. Notice there are no quotes around the value, just the variable name, an equals sign, and the key itself:

OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Replace sk-xxxx… with the actual secret key you copied from OpenAI. You can name the OPENAI_API_KEY variable anything you want, but using a descriptive name helps you remember what it contains. Some developers may prefer a name like EDUC6192_OPENAI_API_KEY to indicate which project or course the key belongs to.

The .env file acts like a secure configuration file that stays on your computer but never gets shared with your code. When you run your program, the dotenv library will read this file and make these variables available to your Python script.

Step 4: Install the Necessary Libraries

You’ll need two Python libraries: openai to interact with the API and python-dotenv to load your .env file. Think of these as specialized tools that handle the complex work of communicating with OpenAI’s servers and managing your environment variables, respectively.

Open your terminal in the IDE and run:

pip install openai python-dotenv

If you’re new to using pip, this command tells Python’s package manager to download and install these libraries so your code can use them. The openai library contains pre-written functions that handle all the technical details of making API requests, while python-dotenv provides a simple way to load configuration from files.

Step 5: Write the Python Code 💻

Open your main.py file and write the Python code. It will first load the variables from the .env file and then use the key to make an API call.

NoteClick here to view the code I wrote with detailed explanations.
# Import necessary libraries
import os  # For accessing environment variables
from dotenv import load_dotenv  # For loading .env file
from openai import OpenAI  # For making API calls

# Load environment variables from .env file
# This must be called before trying to access any variables
load_dotenv()

def get_chatbot_response(user_prompt, model="gpt-4o-mini"):
    """
    Function to get a response from OpenAI's chat models.
    
    This function encapsulates the API logic in a reusable way
    that can be called multiple times with different prompts.
    
    Args:
        user_prompt (str): The question or prompt to send to the AI
        model (str): The specific AI model to use (defaults to gpt-4o-mini)
    
    Returns:
        str: The AI's response or an error message
    """
    
    # Load your API key from an environment variable
    api_key = os.getenv("OPENAI_API_KEY")
    
    # Error handling: Check if the API key was successfully loaded
    # This prevents crashes and gives helpful feedback to the user
    if not api_key:
        return "Error: OPENAI_API_KEY environment variable not set. Check your .env file."

    try:
        # Initialize the OpenAI client with our API key
        # This creates a connection object that handles all the technical details
        client = OpenAI(api_key=api_key)

        # Create a chat completion request
        # This is where we actually send our prompt to OpenAI's servers
        response = client.chat.completions.create(
            model=model,  # Specify which AI model to use
            messages=[
                {
                    # System message: Sets the AI's role and behavior
                    # This is like giving the AI a job description
                    "role": "system",
                    "content": (
                        "You are a teaching assistant for an LLM Applications in Education course. "
                        "You provide insightful answers about implementing large language models "
                        "in educational contexts and explain technical concepts clearly with "
                        "practical, real-world examples."
                    ),
                },
                {
                    # User message: The actual question or prompt
                    "role": "user",
                    "content": user_prompt,
                },
            ],
            max_tokens=50,  # Limit response length (1 token ≈ 0.75 words)
            temperature=0.2  # Low temperature = more focused, consistent responses
                           # Higher values (up to 1.0) = more creative, varied responses
        )
        
        # Extract and return the AI's response
        # The response object contains metadata, but we just want the text
        return response.choices[0].message.content

    except Exception as e:
        # Comprehensive error handling: Catch any other problems
        # This could be network issues, API errors, or authentication problems
        return f"An error occurred: {e}"

# Main execution block
# This Python convention allows the script to run directly or be imported as a module
if __name__ == "__main__":
    # Define your prompt
    # You can easily change this or make it interactive by using input()
    # to ask the user for questions
    prompt = (
        "What are the most effective prompt engineering techniques for creating "
        "educational chatbots that can adapt to different student learning levels "
        "in the same classroom?"
    )
    
    # Get and print the assistant's reply
    assistant_reply = get_chatbot_response(prompt)
    print(f"AI Response: {assistant_reply}")

Understanding the Code Structure

NoteClick here to learn about the design principles and rationale behind my code.

My implementation uses a modular approach with a reusable function, get_chatbot_response(user_prompt, model). This function encapsulates all the logic for making the API call, which means you can easily call it multiple times with different prompts without rewriting code.

The code includes robust error handling that first checks if the API key was found and returns a helpful message if not. It also wraps the API call in a try...except block to gracefully catch any potential issues, like network problems or API errors, and report them without crashing.

The use of if __name__ == "__main__": is a standard Python convention that allows the script to be run directly or imported as a module into a larger application.

I’ve chosen gpt-4o-mini as the default model for several important reasons. It’s cost-effective, which is crucial for educational projects where budget constraints are a concern; you want to maximize experimentation within limited resources. gpt-4o-mini offers excellent performance across reasoning, instruction-following, and code-generation tasks while being efficient. By using this modern model, you’re leveraging current technology, which demonstrates good practice in educational technology.

Troubleshooting Common Issues

When working with APIs, you’ll likely encounter some common problems. Here’s how to identify and solve them:

“Error: OPENAI_API_KEY environment variable not set”: This means your .env file isn’t being read properly by the Python code. Check that the file is named exactly .env (with the dot at the beginning), is in the same folder as your Python script, and contains the line OPENAI_API_KEY=your_actual_key_here with no extra spaces.

“An error occurred: Incorrect API key provided”: Your API key is loaded into the Python code, but it isn’t valid. Double-check that you copied the entire key correctly from the OpenAI dashboard, including the sk- prefix.

“An error occurred: Connection error”: This usually indicates a network problem. Check your internet connection, and if you’re on a school or corporate network, you might need to configure proxy settings.

Import errors for openai or dotenv: Make sure you’ve installed the required libraries using pip install openai python-dotenv.

Next Steps

This foundation prepares you for more advanced API work.

Understanding environment variables and error handling will serve you well, as these are fundamental practices in professional software development. The modular approach you’ve learned here scales to much larger applications and team projects.

The principles of secure key management and robust error handling apply to virtually any API you’ll work with in your career.

Back to top

Made with and Quarto