Audio Inputs
Send audio files to compatible models for transcription, analysis, and processing. Audio input requests use the Responses API (/api/v1/responses) with the input_audio content type. Audio files must be base64-encoded and include the format specification.
Note: Audio files must be base64-encoded - direct URLs are not supported for audio content.
You can search for models that support audio input by filtering to audio input modality on our Models page.
Sending Audio Files
Here’s how to send an audio file for processing:import { OpenRouter } from '@openrouter/sdk';
import fs from "fs/promises";
const openRouter = new OpenRouter({
apiKey: '<OPENROUTER_API_KEY>',
});
async function encodeAudioToBase64(audioPath: string): Promise<string> {
const audioBuffer = await fs.readFile(audioPath);
return audioBuffer.toString("base64");
}
// Read and encode the audio file
const audioPath = "path/to/your/audio.wav";
const base64Audio = await encodeAudioToBase64(audioPath);
const result = await openRouter.responses.send({
responsesRequest: {
model: "google/gemini-2.5-flash",
input: [
{
role: "user",
content: [
{
type: "input_text",
text: "Please transcribe this audio file.",
},
{
type: "input_audio",
inputAudio: {
data: base64Audio,
format: "wav",
},
},
],
},
],
stream: false,
},
});
console.log(result);
import requests
import json
import base64
def encode_audio_to_base64(audio_path):
with open(audio_path, "rb") as audio_file:
return base64.b64encode(audio_file.read()).decode('utf-8')
url = "https://openrouter.ai/api/v1/responses"
headers = {
"Authorization": f"Bearer {API_KEY_REF}",
"Content-Type": "application/json"
}
# Read and encode the audio file
audio_path = "path/to/your/audio.wav"
base64_audio = encode_audio_to_base64(audio_path)
input_items = [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "Please transcribe this audio file."
},
{
"type": "input_audio",
"input_audio": {
"data": base64_audio,
"format": "wav"
}
}
]
}
]
payload = {
"model": "google/gemini-2.5-flash",
"input": input_items
}
response = requests.post(url, headers=headers, json=payload)
print(response.json())
import fs from "fs/promises";
async function encodeAudioToBase64(audioPath: string): Promise<string> {
const audioBuffer = await fs.readFile(audioPath);
return audioBuffer.toString("base64");
}
// Read and encode the audio file
const audioPath = "path/to/your/audio.wav";
const base64Audio = await encodeAudioToBase64(audioPath);
const response = await fetch("https://openrouter.ai/api/v1/responses", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "google/gemini-2.5-flash",
input: [
{
role: "user",
content: [
{
type: "input_text",
text: "Please transcribe this audio file.",
},
{
type: "input_audio",
input_audio: {
data: base64Audio,
format: "wav",
},
},
],
},
],
}),
});
const data = await response.json();
console.log(data);
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
func main() {
// Read and encode the audio file
audioPath := "path/to/your/audio.wav"
audioBytes, err := os.ReadFile(audioPath)
if err != nil {
log.Fatal(err)
}
base64Audio := base64.StdEncoding.EncodeToString(audioBytes)
body, _ := json.Marshal(map[string]any{
"model": "google/gemini-2.5-flash",
"input": []any{
map[string]any{
"role": "user",
"content": []any{
map[string]any{"type": "input_text", "text": "Please transcribe this audio file."},
map[string]any{
"type": "input_audio",
"input_audio": map[string]any{"data": base64Audio, "format": "wav"},
},
},
},
},
"stream": false,
})
req, _ := http.NewRequest("POST", "https://openrouter.ai/api/v1/responses", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("OPENROUTER_API_KEY"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
result, _ := io.ReadAll(resp.Body)
fmt.Println(string(result))
}
AUDIO_PATH="path/to/your/audio.wav"
BASE64_AUDIO=$(base64 < "$AUDIO_PATH" | tr -d '\n')
curl -s https://openrouter.ai/api/v1/responses \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-d @- <<EOF | jq .
{
"model": "google/gemini-2.5-flash",
"input": [
{
"role": "user",
"content": [
{
"type": "input_text",
"text": "Please transcribe this audio file."
},
{
"type": "input_audio",
"input_audio": {
"data": "$BASE64_AUDIO",
"format": "wav"
}
}
]
}
],
"stream": false
}
EOF
Output
{
background: false,
completedAt: 1794950375,
createdAt: 1794950371,
error: null,
frequencyPenalty: 0,
id: "gen-1794950371-66aSkH2jJjcJz83iT5Xk",
incompleteDetails: null,
instructions: null,
maxOutputTokens: null,
maxToolCalls: null,
metadata: {},
model: "google/gemini-2.5-flash",
object: "response",
output: [
{
content: [Array ...],
id: "msg_tmp_kulh56l5ue",
role: "assistant",
status: "completed",
type: "message",
}
],
parallelToolCalls: true,
presencePenalty: 0,
previousResponseId: null,
promptCacheKey: null,
promptCacheOptions: null,
reasoning: null,
safetyIdentifier: null,
serviceTier: "default",
status: "completed",
store: false,
temperature: 1,
text: {
format: {
type: "text",
},
},
toolChoice: "auto",
Supported Audio Input Formats
The Responses API (/api/v1/responses) accepts two format values for input_audio:
wav- WAV audiomp3- MP3 audio
/api/v1/chat/completions) accepts any format string the provider supports, such as aiff, aac, ogg, flac, m4a, pcm16, or pcm24, in the input_audio.format field of its content parts.
Note: Check your model’s documentation to confirm which audio formats it supports. Not all models support all formats.
Audio Output
OpenRouter supports receiving audio responses from models that have audio output capabilities. To request audio output, include themodalities and audio parameters in your request. Audio output uses the Chat Completions API (/api/v1/chat/completions), because the Responses API does not accept the audio parameter.
You can search for models that support audio output by filtering to audio output modality on our Models page.
Requesting Audio Output
To receive audio output, setmodalities to ["text", "audio"] and provide the audio configuration with your desired voice and format:
import os
import requests
import json
import base64
url = "https://openrouter.ai/api/v1/chat/completions"
headers = {
"Authorization": f"Bearer {os.environ['OPENROUTER_API_KEY']}",
"Content-Type": "application/json"
}
payload = {
"model": "openai/gpt-audio",
"messages": [
{
"role": "user",
"content": "Say hello in a friendly tone."
}
],
"modalities": ["text", "audio"],
"audio": {
"voice": "alloy",
"format": "pcm16"
},
"stream": True
}
# Audio output requires streaming — the response is delivered as SSE chunks
response = requests.post(url, headers=headers, json=payload, stream=True)
if not response.ok:
raise RuntimeError(f"Request failed: {response.status_code} {response.text}")
# SSE is UTF-8. Set it explicitly so a response without a charset is not decoded as ISO-8859-1
response.encoding = "utf-8"
audio_data_chunks = []
transcript_chunks = []
for line in response.iter_lines(decode_unicode=True):
if not line or not line.startswith("data: "):
continue
data = line[len("data: "):]
if data.strip() == "[DONE]":
break
chunk = json.loads(data)
choices = chunk.get("choices") or []
if not choices:
continue
delta = choices[0].get("delta") or {}
audio = delta.get("audio") or {}
if audio.get("data"):
audio_data_chunks.append(audio["data"])
if audio.get("transcript"):
transcript_chunks.append(audio["transcript"])
transcript = "".join(transcript_chunks)
print(f"Transcript: {transcript}")
# Combine and decode the base64 audio chunks (raw PCM16), then save
full_audio_b64 = "".join(audio_data_chunks)
audio_bytes = base64.b64decode(full_audio_b64)
with open("output.pcm", "wb") as f:
f.write(audio_bytes)
print(f"Saved {len(audio_bytes)} bytes of PCM16 audio to output.pcm")
const response = await fetch("https://openrouter.ai/api/v1/chat/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OPENROUTER_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "openai/gpt-audio",
messages: [
{
role: "user",
content: "Say hello in a friendly tone.",
},
],
modalities: ["text", "audio"],
audio: {
voice: "alloy",
format: "pcm16",
},
stream: true,
}),
});
if (!response.ok) {
throw new Error(`Request failed: ${response.status} ${await response.text()}`);
}
// Audio output requires streaming — parse the SSE chunks
const reader = response.body!.getReader();
const decoder = new TextDecoder();
const audioDataChunks: string[] = [];
const transcriptChunks: string[] = [];
let buffer = "";
stream: while (true) {
const { done, value } = await reader.read();
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop()!; // keep incomplete line in buffer
for (const line of lines) {
if (!line.startsWith("data: ")) continue;
const data = line.slice("data: ".length).trim();
if (data === "[DONE]") break stream;
const chunk = JSON.parse(data);
const audio = chunk.choices?.[0]?.delta?.audio;
if (audio?.data) audioDataChunks.push(audio.data);
if (audio?.transcript) transcriptChunks.push(audio.transcript);
}
}
const transcript = transcriptChunks.join("");
console.log(`Transcript: ${transcript}`);
// audioDataChunks joined together is the full base64-encoded audio (raw PCM16)
const fullAudioB64 = audioDataChunks.join("");
console.log(`Received ${fullAudioB64.length} base64 characters of audio`);
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"strings"
)
func main() {
body, _ := json.Marshal(map[string]any{
"model": "openai/gpt-audio",
"messages": []any{
map[string]any{"role": "user", "content": "Say hello in a friendly tone."},
},
"modalities": []string{"text", "audio"},
"audio": map[string]any{"voice": "alloy", "format": "pcm16"},
"stream": true,
})
req, _ := http.NewRequest("POST", "https://openrouter.ai/api/v1/chat/completions", bytes.NewReader(body))
req.Header.Set("Authorization", "Bearer "+os.Getenv("OPENROUTER_API_KEY"))
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
msg, _ := io.ReadAll(resp.Body)
log.Fatalf("Request failed: %d %s", resp.StatusCode, msg)
}
// Audio output requires streaming — parse the SSE chunks
var audioData, transcript strings.Builder
scanner := bufio.NewScanner(resp.Body)
scanner.Buffer(make([]byte, 1024*1024), 16*1024*1024)
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "data: ") {
continue
}
data := strings.TrimSpace(strings.TrimPrefix(line, "data: "))
if data == "[DONE]" {
break
}
var chunk struct {
Choices []struct {
Delta struct {
Audio struct {
Data string `json:"data"`
Transcript string `json:"transcript"`
} `json:"audio"`
} `json:"delta"`
} `json:"choices"`
}
if err := json.Unmarshal([]byte(data), &chunk); err != nil || len(chunk.Choices) == 0 {
continue
}
audio := chunk.Choices[0].Delta.Audio
audioData.WriteString(audio.Data)
transcript.WriteString(audio.Transcript)
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
fmt.Printf("Transcript: %s\n", transcript.String())
// audioData is the full base64-encoded audio (raw PCM16)
fmt.Printf("Received %d base64 characters of audio\n", audioData.Len())
}
RESPONSE=$(curl -sSfN https://openrouter.ai/api/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-d '{
"model": "openai/gpt-audio",
"messages": [
{
"role": "user",
"content": "Say hello in a friendly tone."
}
],
"modalities": ["text", "audio"],
"audio": {
"voice": "alloy",
"format": "pcm16"
},
"stream": true
}')
# Audio output requires streaming: keep the JSON from each SSE "data:" line
CHUNKS=$(printf '%s\n' "$RESPONSE" | sed -n 's/^data: //p' | grep -v '^\[DONE\]')
TRANSCRIPT=$(printf '%s\n' "$CHUNKS" | jq -rj '.choices[0]?.delta?.audio?.transcript // empty')
echo "Transcript: $TRANSCRIPT"
# The joined data chunks are the full base64-encoded audio (raw PCM16)
AUDIO_B64=$(printf '%s\n' "$CHUNKS" | jq -rj '.choices[0]?.delta?.audio?.data // empty')
echo "Received ${#AUDIO_B64} base64 characters of audio"
Output
Transcript: Hello! It's great to meet you. How are you doing today?
Received 252800 base64 characters of audio
Streaming Chunk Format
Audio output requires streaming (stream: true). Audio data and transcript are delivered incrementally via the delta.audio field in each chunk:
{
"choices": [
{
"delta": {
"audio": {
"data": "<base64-encoded audio chunk>",
"transcript": "Hello"
}
}
}
]
}
Audio Configuration Options
Theaudio parameter accepts the following options:
| Option | Description |
|---|---|
voice | The voice to use for audio generation (e.g., alloy, echo, fable, onyx, nova, shimmer). Available voices vary by model. |
format | The audio format for the output (e.g., wav, mp3, flac, opus, pcm16). Available formats vary by model. |