0% found this document useful (0 votes)
4 views4 pages

Openai API

Uploaded by

mhamzashahid627
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Openai API

Uploaded by

mhamzashahid627
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

OpenAI API Key Creation and Usage

Guide
Muhammad Hamza Shahid
April 14, 2026

1 Introduction
This document provides a complete step-by-step guide to:

• Creating an OpenAI account

• Adding a payment method

• Enabling Pay-As-You-Go billing

• Generating an API key

• Using the API key in Python

• Understanding billing and common errors

This guide is intended for students who want to use OpenAI models (such as GPT-
4o-mini) in their programming projects.

2 Step 1: Create an OpenAI Account


1. Open a web browser and go to:

[Link]

2. Click on Sign Up.

3. Sign up using:

• Email and password, or


• Google / Microsoft account

4. Verify your email address if prompted.

1
3 Step 2: Access the OpenAI Dashboard
Once logged in:
• You will be redirected to the OpenAI dashboard.
• This dashboard is where you manage API keys, billing, and usage.

4 Step 3: Add a Payment Method


OpenAI APIs work on a Pay-As-You-Go model. You must add a payment method
before using the API.
1. Navigate to:

[Link]

2. Click on Add payment method.


3. Enter your:
• Debit or credit card details
4. Confirm the payment method.
Important Note:
• No money is charged immediately.
• Charges happen only after API usage.

5 Step 4: Enable Pay-As-You-Go


After adding a card:
• Your account is upgraded to Pay-As-You-Go.
• You do not need to purchase a subscription.
• You pay only for the API calls you make.

6 Step 5: Add Funds (Important)


Even with Pay-As-You-Go enabled, you must add a small balance to avoid quota errors.
1. Go to:

[Link]

2. Click Add funds.


3. Add a minimum amount (e.g., $5).
Without adding funds, API calls will fail with an “insufficient quota” error.

2
7 Step 6: Generate an API Key
1. Go to:

[Link]

2. Click Create new secret key.

3. Copy the key immediately.

Important Security Rules:

• The key starts with sk-.

• You will only see it once.

• Do not share it publicly.

• Do not upload it to GitHub.

8 Step 7: Install the OpenAI Python Library


Open a terminal or command prompt and run:
pip install openai

9 Step 8: Use the API Key in Python


9.1 Basic Example (GPT-4o-mini)
from openai import OpenAI

client = OpenAI ( api_key = " sk - yo ur_api _key_h ere " )

response = client . chat . completions . create (


model = " gpt -4 o - mini " ,
messages =[
{ " role " : " system " , " content " : " You ␣ are ␣ a ␣ helpful ␣
assistant . " } ,
{ " role " : " user " , " content " : " Explain ␣ factorial ␣ in ␣ Python .
"}
]
)

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

3
10 Recommended Model
For students and learning projects:

• Model: gpt-4o-mini

• Reason: Cheapest, fast, and accurate for summaries and coding

11 Understanding Costs
• Costs are based on tokens (input + output).

• Typical small requests cost fractions of a cent.

• You can monitor usage at:

[Link]

12 Common Error and Solution


12.1 Error: Insufficient Quota
Error Message:
openai . RateLimitError : i ns uf fi ci en t_ qu ot a

Solution:

• Add funds to your billing account.

• Ensure Pay-As-You-Go is active.

13 Conclusion
Once the API key is created and billing is active:

• The student can freely use OpenAI models in Python.

• Charges occur automatically based on usage.

• No monthly subscription is required.

You might also like