• Python Snacks
  • Posts
  • A Beginner’s Guide to Using OpenAI API with Python

A Beginner’s Guide to Using OpenAI API with Python

Learn how to connect Python to OpenAI's API and build smart workflows in minutes.

OpenAI is a research organization and company that develops advanced AI systems. They created and maintain the large language models like: GPT-3, GPT-3.5, GPT-4o.

» The most recently launched model is GPT-5 and one of its key features is Dynamic routing. It enables the model to decide whether to respond instantly or engage in advanced reasoning – all depending upon the complexity of the prompt.

One of OpenAI’s most famous projects is ChatGPT. It is a user-friendly chat interface for interacting with models like GPT-4o.

Similarly, OpenAI has an API which you can access via different programming languages like Python, JAVA, and JavaScript. Using this API, you can: 

  • Generate ChatGPT-style responses programmatically

  • Automate tasks and workflows

  • Integrate language understanding into your existing apps

  • Build intelligent assistants, tools, bots, and more

Here, we’ll show you how to set up your OpenAI account, generate your API key, explain how credit work, and show a simple automation using their API.

Setting up an account:

Before you begin working with their API, you’re going to need to sign up for an account and set it up. You’ll also need to generate your API key, but be careful, as your API key can’t be viewed once you close out of the window giving you the key (but you can always generate a new one).

» Never hardcode your API key in your code. Here’s how to handle keys in a local development environment.

How Credits in Open AI work?

Next, you’ll need to buy credits to use their API. After adding your payment method, you will be asked to purchase the credits with a minimum purchase of $5.

OpenAI charges you for your token usage, and 1K tokens is roughly equal to 750 words.

With a minimum purchase, you’ll be able to generate around 1.4 million tokens, which is roughly 1 million words using GPT-3.5-Turbo.

To put this into perspective, a 1 million word book is roughly as long as 4 to 5 Harry Potter novels combined 🤯

You can monitor token usage via the billing dashboard.

Automate a simple workflow using Python and OpenAI

Products sold internationally receive feedback in a wide range of languages. Translating this feedback into English can be tedious, but it’s essential as it uncovers valuable customer insights that are critical for any growing business.

We can automate this task of translating feedback using the OpenAI’s API. The snippet below translates feedback from German to English.

To get started, install the OpenAI library via pip. Since the latest version of OpenAI’s Python SDK (openai>=1.0.0) requires additional setup, we’ll use the legacy version for simplicity.

pip install openai==0.28.0

» Always create a virtual environment to test new SDKs before integrating them into production.

Then, end a simple prompt and get a response using openai.ChatCompletion.create(). It is a method from OpenAI’s Python library to send a chat-style prompt to the API:

import openai
import os

openai_api_key = os.getenv("OPENAI_API_KEY")

text= 'Das Produkt kam schnell an, aber die Verpackung war beschädigt.'

prompt = f"Translate the following text from German to English:\n\n{text}"
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[{"role": "user", "content": prompt}],
    temperature=0.3
)

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

# Output: The product arrived quickly, but the packaging was damaged.

Here, we’re using model, messages, and temperature as parameters:

  • model: states which OpenAI model we want to use

  • messages: a list of chat history entries that tell the model who said what so it can respond in context

  • temperature: controls how deterministic or creative the model’s responses are, where lower values are more focused on consistent outputs.

Final thoughts: Build smarter workflows with simple Python and the OpenAI API. It’s powerful, and capable of handling tasks that once took hours with just a few lines of code.

Happy coding!

📧 Join the Python Snacks Newsletter! 🐍

Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:

  1. Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.

  2. Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.

  3. Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.

You can unsubscribe at any time.

Interested in starting a newsletter or a blog?

Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.

If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.

This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

Reply

or to participate.