Getting started overview
Integrating with the OpenAI Whisper API involves a sequence of steps designed to get you from account creation to a functional API call. This guide focuses on the essentials: signing up for an OpenAI account, generating an API key, and executing your first audio transcription or translation request. The process is consistent whether you are using cURL, Python, or Node.js, with the core steps remaining the same across different programming environments.
The Whisper API offers two primary functionalities: transcribing audio into the original spoken language and translating audio into English text. Both operations utilize the same /v1/audio endpoint, with a different sub-path to specify the desired action. Understanding the required parameters, such as the audio file and model identifier, is key to making successful requests.
Before making any requests, ensure you have an active OpenAI account and a valid API key. OpenAI maintains detailed audio API reference documentation covering all available endpoints and parameters.
Here’s a quick reference for the getting started process:
| Step | What to do | Where |
|---|---|---|
| 1. Account Creation | Register for an OpenAI account. | OpenAI Signup Page |
| 2. API Key Generation | Generate a new secret API key. | OpenAI API Keys Page |
| 3. Environment Setup | Install necessary SDKs (Python/Node.js) or prepare cURL. | Local development environment |
| 4. First Request | Send an audio file for transcription or translation. | API endpoint /v1/audio/transcriptions or /v1/audio/translations |
| 5. Process Response | Handle the JSON response containing the transcribed/translated text. | Application logic |
Create an account and get keys
To begin using the OpenAI Whisper API, you must first create an OpenAI account. This account serves as your gateway to all OpenAI services, including the Whisper API. If you already have an account, you can proceed directly to API key generation.
Account Creation Steps:
- Navigate to the OpenAI signup page.
- Enter your email address and create a password, or sign up using your Google or Microsoft account.
- Follow the on-screen prompts to verify your email and complete any additional registration steps, which may include providing a phone number.
- Once registered, log in to your OpenAI account.
API Key Generation Steps:
After successfully creating and logging into your OpenAI account, you will need to generate an API key. This key authenticates your requests to the Whisper API and links them to your account for billing and usage tracking. OpenAI recommends creating a new key for each application or project to enhance security and simplify key management.
- Access the API keys section of your OpenAI dashboard.
- Click the "Create new secret key" button.
- A dialog box will display your new secret key. Copy this key immediately, as it will only be shown once. OpenAI does not store your secret keys, so if you lose it, you will need to generate a new one.
- Store your API key securely. It is best practice to use environment variables or a secrets management service to store API keys rather than hardcoding them directly into your application code. This prevents accidental exposure and makes it easier to manage keys across different environments. For example, you might set an environment variable like
OPENAI_API_KEY.
Your first request
With an OpenAI account created and an API key secured, you are ready to make your first request to the Whisper API. This example demonstrates how to transcribe an audio file using cURL, Python, and Node.js. For this example, we will assume you have a short audio file named audio.mp3 in the same directory as your script.
Prerequisites:
- An audio file (e.g.,
audio.mp3) to transcribe. The Whisper API supports various formats, including MP3, MP4, MPEG, M4A, WAV, WebM, and FLAC. The maximum file size is 25 MB. - Your OpenAI API key, stored securely (e.g., in an environment variable named
OPENAI_API_KEY). - For Python: Ensure you have
openailibrary installed (pip install openai). - For Node.js: Ensure you have
openailibrary installed (npm install openai).
Transcribing Audio with cURL
This command sends an MP3 file to the transcription endpoint.
curl https://api.openai.com/v1/audio/transcriptions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: multipart/form-data" \
-F file="@audio.mp3" \
-F model="whisper-1"
Transcribing Audio with Python
This Python script uses the official OpenAI Python library.
import openai
import os
# Ensure your API key is set as an environment variable
openai.api_key = os.getenv("OPENAI_API_KEY")
if not openai.api_key:
raise ValueError("OPENAI_API_KEY environment variable not set.")
# Path to your audio file
audio_file_path = "./audio.mp3"
def transcribe_audio(file_path):
with open(file_path, "rb") as audio_file:
transcript = openai.audio.transcriptions.create(
model="whisper-1",
file=audio_file
)
return transcript.text
if __name__ == "__main__":
try:
transcribed_text = transcribe_audio(audio_file_path)
print(f"Transcribed Text: {transcribed_text}")
except Exception as e:
print(f"An error occurred: {e}")
Transcribing Audio with Node.js
This Node.js script uses the official OpenAI Node.js library.
import OpenAI from 'openai';
import fs from 'fs';
// Ensure your API key is set as an environment variable
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
if (!openai.apiKey) {
throw new Error("OPENAI_API_KEY environment variable not set.");
}
// Path to your audio file
const audioFilePath = './audio.mp3';
async function transcribeAudio(filePath) {
const transcription = await openai.audio.transcriptions.create({
file: fs.createReadStream(filePath),
model: "whisper-1",
});
return transcription.text;
}
(async () => {
try {
const transcribedText = await transcribeAudio(audioFilePath);
console.log(`Transcribed Text: ${transcribedText}`);
} catch (error) {
console.error(`An error occurred: ${error.message}`);
}
})();
Expected Response
A successful request will return a JSON object containing the transcribed text. For example:
{
"text": "This is a test audio for OpenAI Whisper API."
}
Common next steps
After successfully completing your first API call, consider these next steps to further integrate and optimize your use of the OpenAI Whisper API:
- Error Handling: Implement robust error handling in your application. The API can return various HTTP status codes and error messages, which your application should gracefully manage.
- Audio Translation: Explore the audio translation endpoint (
/v1/audio/translations) if your application requires converting spoken language into English text. The process is similar to transcription, but the output is always English. - Supported Formats and Size Limits: Familiarize yourself with the supported audio formats and file size limits (25 MB) to ensure your audio inputs are compatible. For larger files, consider pre-processing by splitting them into smaller chunks.
- Prompting: Experiment with the
promptparameter. Providing an initial text prompt can guide the model, especially for unusual words or domain-specific terminology, improving transcription accuracy. - Response Formats: The Whisper API supports multiple response formats beyond plain text, such as JSON, SRT, VTT, and verbose JSON. Select the format that best suits your application's needs.
- Cost Management: Monitor your API usage and understand the OpenAI pricing model, which is typically pay-as-you-go per minute of audio processed. Set up usage limits and alerts in your OpenAI dashboard to prevent unexpected costs.
- Official Documentation: Regularly consult the official OpenAI Whisper API documentation for updates, new features, and best practices.
- Security Best Practices: Always keep your API keys secure. Rotate keys periodically and ensure they are not exposed in client-side code or public repositories. The OAuth 2.0 framework provides a standard for secure API authorization without exposing credentials directly.
Troubleshooting the first call
Encountering issues during your first API call is common. Here are some troubleshooting tips for the OpenAI Whisper API:
- Invalid API Key: Double-check that your
OPENAI_API_KEYis correct and has not expired. A common error is a401 Unauthorizedresponse. Ensure the key starts withsk-. If in doubt, generate a new key from your OpenAI API keys page. - Missing Authorization Header: Ensure the
Authorization: Bearer YOUR_API_KEYheader is correctly set in your HTTP request. This is a common oversight, especially with cURL. - File Not Found or Incorrect Path: Verify that the audio file specified (e.g.,
audio.mp3) exists at the given path and is accessible by your script. Use absolute paths if relative paths are causing issues. - Unsupported Audio Format or Size: The Whisper API supports specific audio formats (MP3, MP4, MPEG, M4A, WAV, WebM, FLAC) and has a 25 MB file size limit. If your file exceeds this, you will receive an error. Consider converting or splitting larger files.
- Network Issues: Check your internet connection. Temporary network interruptions can cause requests to fail or time out.
- Incorrect Endpoint: Confirm you are sending requests to the correct endpoint:
https://api.openai.com/v1/audio/transcriptionsfor transcription orhttps://api.openai.com/v1/audio/translationsfor translation. - Model Not Specified or Invalid: Ensure the
modelparameter is set to"whisper-1". This is currently the only model available for the Whisper API. - Insufficient Permissions/Quota: If you receive errors related to permissions or quota, check your OpenAI usage dashboard and billing settings. New accounts may have initial limits.
- Library-Specific Errors: If using Python or Node.js SDKs, ensure they are up to date. Consult the OpenAI Python library GitHub repository or OpenAI Node.js library GitHub repository for specific library issues or examples.
- Detailed Error Messages: Pay close attention to the error messages returned by the API. They often provide specific clues about what went wrong.