#Free API
1 messages · Page 1 of 1 (latest)
Hi there! OpenAI offers a testing credit of 5 dollars to OpenAI accounts that are not older than 3 months. Some features may be disabled but most of them can still be used.
If you believe that you have free credits, visit: https://platform.openai.com/api-keys to create an api key.
You can visit https://platform.openai.com/docs/overview for documentation.
In the docs you'll see this:
import OpenAI from "openai";
const openai = new OpenAI();
const completion = await openai.chat.completions.create({
model: "gpt-4o",
messages: [
{"role": "user", "content": "write a haiku about ai"}
]
});
You have to change:
const openai = new OpenAI();
With:
const openai = new OpenAI({
apiKey: your-api-key,
organization: your-organization-id,
});
If you want to store your keys in an .env file, go to your terminal and execute the following commands:
npm install dotenv
touch .env
This will create an .env file in your directory. You can paste your api keys into the file now:
OPENAI_API_KEY=your-api-key-here
ORGANIZATION_ID=your-organization-id-here
If you are using git, do not forget to include .env in your .gitignore file.
Example .gitignore file:
# Ignore node_modules directory
node_modules/
# Ignore environment variables
.env
You can now call your environment variables:
If you did these, instead of using:
const openai = new OpenAI({
apiKey: your-api-key,
organization: your-organization-id,
});
you can now use:
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY, // Loaded from .env file
organization: process.env.ORGANIZATION_ID, // Loaded from .env file
});
I hope it helps! I hope you enjoy using OpenAI API!