Loading...
Loading...
Complete guide for OpenAI APIs: Chat Completions (GPT-5.2, GPT-4o), Embeddings, Images (GPT-Image-1.5), Audio (Whisper + TTS + Transcribe), Moderation. Includes Node.js SDK and fetch approaches.
npx skill4agent add secondsky/claude-skills openai-apibun add openai
export OPENAI_API_KEY="sk-..."import OpenAI from 'openai';
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Hello!' }]
});const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [
{ role: 'system', content: 'You are a helpful assistant' },
{ role: 'user', content: 'Explain AI' }
],
temperature: 0.7,
max_tokens: 1000
});const stream = await client.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Tell a story' }],
stream: true
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content || '');
}const response = await client.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'What is the weather?' }],
tools: [{
type: 'function',
function: {
name: 'getWeather',
parameters: {
type: 'object',
properties: { location: { type: 'string' } },
required: ['location']
}
}
}]
});const response = await client.embeddings.create({
model: 'text-embedding-3-small',
input: 'Your text here'
});
const embedding = response.data[0].embedding; // 1536 dimensionsconst image = await client.images.generate({
model: 'gpt-image-1.5',
prompt: 'A serene landscape',
size: '1024x1024',
quality: 'standard' // or 'hd'
});const transcription = await client.audio.transcriptions.create({
file: fs.createReadStream('audio.mp3'),
model: 'whisper-1'
});const speech = await client.audio.speech.create({
model: 'tts-1',
voice: 'alloy',
input: 'Hello world'
});references/error-catalog.mdreferences/models-guide.mdreferences/function-calling-patterns.mdreferences/structured-output-guide.mdreferences/embeddings-guide.mdreferences/images-guide.mdreferences/audio-guide.mdreferences/cost-optimization.mdreferences/top-errors.mdreferences/error-catalog.mdtemplates/basic-usage.tstemplates/chat-completion-basic.tstemplates/chat-completion-nodejs.tstemplates/streaming-chat.tstemplates/streaming-fetch.tstemplates/function-calling.tstemplates/structured-output.tstemplates/vision-gpt4o.tstemplates/embeddings.tstemplates/image-generation.tstemplates/image-editing.tstemplates/audio-transcription.tstemplates/text-to-speech.tstemplates/moderation.tstemplates/rate-limit-handling.tstemplates/cloudflare-worker.ts