{ "name": "Text to Speech (OpenAI)", "tags": [], "nodes": [ { "id": "938fedbd-e34c-40af-af2f-b9c669e1a6e9", "name": "When clicking \"Test workflow\"", "type": "n8n-nodes-base.manualTrigger", "position": [ 380, 380 ], "parameters": {}, "typeVersion": 1 }, { "id": "1d59db5d-8fe6-4292-a221-a0d0194c6e0c", "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": "9d54de1d-59b7-4c1f-9e88-13572da5292c", "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": "1ce72c9c-aa6f-4a18-9d5a-3971686a51ec", "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": "eb487535-5f36-465e-aeee-e9ff62373e53", "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": "36b380bd-0703-4b60-83cb-c4ad9265864d", "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": "ff35ff28-62b5-49c8-a657-795aa916b524", "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": "5f7ef80e-b5c8-41df-9411-525fafc2d910", "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": "validate-speech-input", "name": "Validate Speech Input", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 940, 380 ], "parameters": { "mode": "runOnceForAllItems", "jsCode": "const prepare = function prepareSpeech({ input_text, voice }) {\n if (typeof input_text !== 'string' || !input_text.trim()) throw new Error('Input text is required.');\n if (Array.from(input_text).length > 4096) throw new Error('Input text must contain at most 4096 characters.');\n const selected = voice == null || voice === '' ? 'alloy' : voice;\n if (!['alloy', 'ash', 'coral', 'echo', 'fable', 'onyx', 'nova', 'sage', 'shimmer'].includes(selected)) throw new Error('Choose a supported tts-1 voice.');\n return { input_text, voice: selected };\n}\n\nreturn $input.all().map((item,index)=>({json:prepare(item.json),pairedItem:{item:index}}));\n" } }, { "id": "validate-speech-file", "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 } ] ] } } }