{ "name": "Generate an OpenAI Speech MP3", "tags": [], "nodes": [ { "id": "generate-an-openai-speech-mp3-0", "name": "When clicking \"Test workflow\"", "type": "n8n-nodes-base.manualTrigger", "position": [ 380, 380 ], "parameters": {}, "typeVersion": 1 }, { "id": "generate-an-openai-speech-mp3-1", "name": "Set input text and TTS voice", "type": "n8n-nodes-base.set", "position": [ 760, 380 ], "parameters": { "assignments": { "assignments": [ { "id": "speech-text", "name": "input_text", "type": "string", "value": "The quick brown fox jumped over the lazy dog." }, { "id": "speech-voice", "name": "voice", "type": "string", "value": "alloy" } ] }, "options": {} }, "typeVersion": 3.4 }, { "id": "generate-an-openai-speech-mp3-2", "name": "Send HTTP Request to OpenAI's TTS Endpoint", "type": "n8n-nodes-base.httpRequest", "position": [ 1120, 380 ], "parameters": { "url": "https://api.openai.com/v1/audio/speech", "method": "POST", "options": { "response": { "response": { "responseFormat": "file", "outputPropertyName": "data" } } }, "sendBody": true, "sendHeaders": false, "authentication": "predefinedCredentialType", "bodyParameters": { "parameters": [ { "name": "model", "value": "tts-1" }, { "name": "input", "value": "={{ $json.input_text }}" }, { "name": "voice", "value": "={{ $json.voice }}" }, { "name": "response_format", "value": "mp3" } ] }, "nodeCredentialType": "openAiApi" }, "typeVersion": 4.2 }, { "id": "generate-an-openai-speech-mp3-3", "name": "Sticky Note", "type": "n8n-nodes-base.stickyNote", "position": [ 280, 256 ], "parameters": { "width": 273, "height": 339, "content": "## Workflow Trigger\nYou can replace this manual trigger with another trigger type as required by your use case." }, "typeVersion": 1 }, { "id": "generate-an-openai-speech-mp3-4", "name": "Sticky Note1", "type": "n8n-nodes-base.stickyNote", "position": [ 660, 257 ], "parameters": { "width": 273, "height": 335, "content": "## Manually Set OpenAI TTS Configuration\n" }, "typeVersion": 1 }, { "id": "generate-an-openai-speech-mp3-5", "name": "Sticky Note2", "type": "n8n-nodes-base.stickyNote", "position": [ 1020, 260 ], "parameters": { "width": 302, "height": 335, "content": "## Send Request to OpenAI TTS API\n" }, "typeVersion": 1 }, { "id": "generate-an-openai-speech-mp3-6", "name": "Sticky Note3", "type": "n8n-nodes-base.stickyNote", "position": [ 660, 620 ], "parameters": { "color": 4, "width": 273, "height": 278, "content": "### Input\n\nSet input_text to at most 4096 characters and voice to a supported tts-1 voice, initially alloy. Keep your OpenAI key in the credential. The text is sent to OpenAI for speech generation." }, "typeVersion": 1 }, { "id": "generate-an-openai-speech-mp3-7", "name": "Sticky Note4", "type": "n8n-nodes-base.stickyNote", "position": [ 1020, 620 ], "parameters": { "color": 4, "width": 299, "height": 278, "content": "### Output and availability\n\nThe HTTP node returns an MP3 in binary field data. No automatic retries are configured. Disclose that the recording is AI-generated when sharing it. OpenAI has announced tts-1 shutdown on 2027-01-06; plan migration before then. https://developers.openai.com/api/docs/guides/text-to-speech" }, "typeVersion": 1 }, { "id": "generate-an-openai-speech-mp3-8", "name": "Validate Speech Input", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 940, 380 ], "parameters": { "mode": "runOnceForAllItems", "jsCode": "const prepare = function prepare({input_text,voice}){if(typeof input_text!=='string'||!input_text.trim()||Array.from(input_text).length>4096)throw Error('Supply text with 1–4096 characters');const selected=voice||'alloy';if(!['alloy','ash','coral','echo','fable','onyx','nova','sage','shimmer'].includes(selected))throw Error('Unsupported voice');return{input_text,voice:selected}}\n\nreturn $input.all().map((item,index)=>({json:prepare(item.json),pairedItem:{item:index}}));" } }, { "id": "generate-an-openai-speech-mp3-9", "name": "Validate Audio File", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 1320, 380 ], "parameters": { "mode": "runOnceForAllItems", "jsCode": "const items = $input.all();\nconst result = [];\nfor (let index = 0; index < items.length; index++) {\n const item = items[index];\n const audio = item.binary?.data;\n const type = typeof audio?.mimeType === 'string' ? audio.mimeType.split(';')[0].trim().toLowerCase() : '';\n if (!['audio/mpeg', 'audio/mp3', 'application/octet-stream'].includes(type)) throw new Error('The speech response is not MP3 audio.');\n const bytes = await this.helpers.getBinaryDataBuffer(index, 'data');\n if (!bytes.length) throw new Error('The speech response is empty.');\n result.push({...item, binary:{...item.binary,data:{...audio,fileName:'speech.mp3',mimeType:'audio/mpeg'}},pairedItem:{item:index}});\n}\nreturn result;\n" } } ], "active": false, "pinData": {}, "settings": { "executionOrder": "v1" }, "connections": { "Set input text and TTS voice": { "main": [ [ { "node": "Validate Speech Input", "type": "main", "index": 0 } ] ] }, "When clicking \"Test workflow\"": { "main": [ [ { "node": "Set input text and TTS voice", "type": "main", "index": 0 } ] ] }, "Validate Speech Input": { "main": [ [ { "node": "Send HTTP Request to OpenAI's TTS Endpoint", "type": "main", "index": 0 } ] ] }, "Send HTTP Request to OpenAI's TTS Endpoint": { "main": [ [ { "node": "Validate Audio File", "type": "main", "index": 0 } ] ] } } }