How to get the API key for chat GPT 3.5 or 4.0

Felipe A. Moreno
LatinXinAI
Published in
4 min readJun 10, 2024

An API key for OpenAI is a unique identifier that acts as a secret token, allowing authorized access to OpenAI’s API services. When you have an API key, you can use it to authenticate your requests to the API, enabling you to access various functionalities provided by OpenAI, such as natural language processing, text generation, and other AI-driven services.

In this article, I’ll teach you how to obtain an API key to use chatGPT and how to test it!!

Getting your API Key

First, create an account at https://openai.com/.
Then, go to the products option at the top navigation bar and click on “API login“, you will be redirected.

You may choose “API” instead of ChatGPT.

Then, go to your profile and show the right-side menu bar:

right side menu

Select “User API keys”, here you can create your API key. This API key only works if you already add a minimum of 5 USD. You can check pricing and usage limitations there.

profile tabs

You should be able to create an “API key” and assign it to any project (if you want to).

This “API key” only works if you have a minimum of 5 USD in your account, sadly there is no model for free to test through the API. To add funds, just go to the “billing” option and add a payment method.
After that, your API key should work.

That’s all, good luck creating scripts to use chatGPT.

I highly recommend using the “gpt-3.5-turbo” model, with 40,000 TPM (token per minute) and 200,000 RPM (request per minute) with a low cost of ~ 0.5 USD — 1.5 USD.

Testing your API key

Here I’ll share a minimal code to try your API key in python 3.9.0. You should install openai==0.28. You can select any model available here but you should check each model price. To use chatGPT4, just change “gpt-3.5-turbo” to “gpt-4o”. Documentation here.

import openai
import pandas as pd

openai.api_key = 'API-Key'

def extract_information(text):

prompt_template = """
Extract the following information from the text:
1. Name
2. Date
3. Location
4. Description

Text: {text}

Extracted Information:
"""
prompt_request = prompt_template.format(text=text)
print(prompt_request, len(prompt_request))

response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # Use a supported model
messages=[
{"role": "system", "content": "You are an information extraction assistant."},
{"role": "user", "content": prompt_request},
],
max_tokens=500,
temperature=0.5,
)

return response, response['choices'][0]['message']['content'].strip()



texts = [
"John Doe attended a meeting on 5th June 2024 in New York. The meeting was about AI advancements.",
"Jane Smith gave a lecture on 10th May 2024 in San Francisco. The lecture was focused on machine learning trends."
]

data = []

for text in texts:
_, extracted_info = extract_information(text)
print(_)
name, date, location, description = extracted_info.split('\n')
data.append({
'Name': name.split(': ')[1],
'Date': date.split(': ')[1],
'Location': location.split(': ')[1],
'Description': description.split(': ')[1]
})

df = pd.DataFrame(data)
df.to_csv('extracted_information.csv', index=False)

This code will extract information from text and will save it in a CSV.

Thanks for reading!!!

LatinX in AI (LXAI) logo

Do you identify as Latinx and are working in artificial intelligence or know someone who is Latinx and is working in artificial intelligence?

Don’t forget to hit the 👏 below to help support our community — it means a lot!

--

--