How to start using OpenAI API — Step-by-Step

Here’s a quick guide how to get your OpenAI API key, set a usage limit, and implement it securely in your code.

Ofir Ziv
3 min readOct 26, 2023

This guide includes the following steps:
1. Open an Account in OpenAI
2. Creating the OpenAI API Key
3. Save the key as an Environment Variable
4. Manage the Billing and Usage

Let’s dive in…

1. Open an Account in OpenAI

  • Visit OpenAI’s official website.
  • Click on the “Sign Up” or “Get Started” button.
  • Follow the on-screen instructions to provide necessary information.
  • After successful registration, you’ll be directed to the OpenAI dashboard or platform.

2. Creating the OpenAI API Key

  • Once you’re on the OpenAI dashboard, navigate to the API section by pressing on your profile icon
  • Then press “view API keys”.
  • Click on “Create new secret key” or a similar button.
  • Name the API key if it asks for a description (helpful if you plan on creating multiple keys).
    Note: once you have a key, if you want to delete it — you must have at least another one active before deleting it.
    The key has the format “sk-…..”
  • You will be presented with your new API key. Make sure to copy and store it in a safe place as you won’t be able to see it again. If you lose it, you’ll have to generate a new one.

3. Save the key as an Environment Variable

To keep your API key secure, it’s a good practice to store it as an environment variable instead of hard-coding it into your scripts. Here’s how you can set it up:

3.1 Setting up the Environment Variable in your terminal:
• On Windows:

setx OPENAI_API_KEY "your_api_key_here"
  • On Mac/Linux:
export OPENAI_API_KEY=your_api_key_here
  • google colab:
!export OPENAI_API_KEY=your_api_key_here
  • Using python on Mac/Linux/colab:
import os
os.environ['OPENAI_API_KEY'] = "sk-...."

3.2. Accessing the API key in Python:

import os

api_key = os.environ.get("OPENAI_API_KEY")
if not api_key:
raise ValueError("OPENAI_API_KEY not found in environment variables")

# Now you can use `api_key` to make requests to OpenAI

4. Manage the Billing and Usage

  • You can manage your billing throw this page:
  • Now you just have to charge your credit wallet and start use it :)
    You can enable Auto recharge with a usage limit
  • Ensure you have an up-to-date payment method linked to your OpenAI account. Review your billing details regularly to understand your spending.
  • OpenAI dashboard typically has a section that shows your usage metrics. This can include the number of requests you’ve made, total tokens processed, and more.
  • Setting Alerts - It’s a good idea to set up alerts if the platform allows it. This way, you get notified when you’re close to hitting any limits or if there’s any unusual activity.
  • Consider Budgeting - If OpenAI provides any form of budgeting or capping functionality, consider setting it up to ensure you don’t unintentionally go over your intended spending.
  • Pricing — you can find the pricing table here!

--

--

Ofir Ziv
Ofir Ziv

Written by Ofir Ziv

Lead you through AI ✨🤖🚀 Data Scientist | NLP expert | Entrepreneur 👉🏻 Reach out by ofir@madata.tech