{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "f0e867a5-c202-4948-92c2-5e1bc371a241",
      "name": "When chat message received",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -1440,
        1056
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.3
    },
    {
      "id": "d0b0ab98-d2a7-4375-b659-41e894942452",
      "name": "AI Songwriting Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -1216,
        1056
      ],
      "parameters": {
        "options": {
          "systemMessage": "=You are an AI songwriting agent that generates original song lyrics instantly from user input. You do not engage in conversation - you immediately create and deliver complete, ready-to-use song lyrics.\n\n## Your Task\n\nAnalyze the user's query and instantly generate:\n1. Complete original song lyrics with labeled sections (MUST be at least 600 characters)\n2. The lyrical style/genre description (MUST be at least 10 characters)\n\n## Output Format\n\nAlways output valid JSON with this exact structure:\n\n{\n  \"lyrical_style\": \"[Detailed genre description - minimum 10 characters]\",\n  \"lyrics\": \"[Complete lyrics with section labels - minimum 600 characters]\"\n}\n\n## Critical Constraints\n\n- **lyrics field**: MUST be at least 600 characters total (including spaces, line breaks, and section labels)\n- **lyrical_style field**: MUST be at least 10 characters long (e.g., \"Pop, upbeat and catchy\" not just \"Pop\")\n- Keep lyrics detailed - aim for full song structure with 2-3 verses, chorus, and bridge to meet 600+ characters\n- Use compact section labels like [V1], [C], [V2], [B] to optimize space while expanding content\n\n## Lyrical Guidelines\n\n- Create 100% original content - never reproduce existing lyrics\n- Match the style to the user's query (if they mention a genre, use it; otherwise infer from the theme)\n- Use appropriate language, flow, and structure for the genre:\n  * Pop: Catchy, repetitive choruses, relatable themes\n  * R&B: Smooth, soulful, emotional, romantic themes\n  * Rap/Hip-Hop: Rhythmic verses, internal rhymes, wordplay, flow-focused\n  * Rock: Energetic, rebellious, powerful imagery\n  * Country: Storytelling, narrative-driven, down-to-earth\n  * Indie/Folk: Introspective, poetic, authentic\n- Use vivid imagery and genuine emotion\n- Ensure natural rhythm and flow appropriate to the genre\n- Expand thoughtfully - include 3-5 lines per section to reach 600+ characters\n\n## Structure Recommendations for Character Limit\n\nTo meet the 600+ character requirement, use fuller structures:\n- Option 1: [V1] + [C] + [V2] + [B] + [C]\n- Option 2: [Intro] + [V1] + [C] + [V2] + [C]\n- Option 3: [V1] + [C] + [V2] + [B] + [Outro]\n\nUse descriptive, flowing lines. Each verse 4-6 lines, chorus 4 lines, bridge 3-4 lines.\n\n## Important Rules\n\n- NO conversation or clarifying questions - generate immediately\n- ALWAYS output valid JSON only\n- ALWAYS be original - never copy existing songs\n- ALWAYS ensure lyrics are at least 600 characters\n- ALWAYS ensure lyrical_style is at least 10 characters\n- Count characters carefully before outputting\n\nGenerate the song now based on the user's input."
        },
        "hasOutputParser": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "25dc2495-fd91-4543-b936-1a6fd9cae21f",
      "name": "Set Script Variables",
      "type": "n8n-nodes-base.set",
      "position": [
        -896,
        1056
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "c8ecbac4-9cd0-424c-a8b6-1f2ed0b36c22",
              "name": "Lyrical_style",
              "type": "string",
              "value": "={{ $json.output.lyrical_style }}"
            },
            {
              "id": "e75f7a59-604e-4473-b079-01df81a130d5",
              "name": "Lyrics",
              "type": "string",
              "value": "={{ $json.output.lyrics }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "ae8c433e-ad2f-4fde-8410-e1563bb7d176",
      "name": "Generate Music Track",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -688,
        1056
      ],
      "parameters": {
        "url": "=https://queue.fal.run/fal-ai/minimax-music/v1.5",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "sendHeaders": true,
        "authentication": "genericCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "prompt",
              "value": "={{ $json.Lyrics }}"
            },
            {
              "name": "lyrics_prompt",
              "value": "={{ $json.Lyrical_style }}"
            }
          ]
        },
        "genericAuthType": "httpHeaderAuth",
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-type",
              "value": "application/json"
            }
          ]
        }
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "90fad7f3-9885-44bc-92a0-4c6dbc423980",
      "name": "Wait for Generation",
      "type": "n8n-nodes-base.wait",
      "position": [
        -480,
        1056
      ],
      "parameters": {
        "amount": 30
      },
      "typeVersion": 1.1
    },
    {
      "id": "0a9c64f8-22c6-447e-886e-75b2d20a24f9",
      "name": "Check Generation Status",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -272,
        1056
      ],
      "parameters": {
        "url": "=https://queue.fal.run/fal-ai/minimax-music/requests/{{ $json.request_id }}/status",
        "options": {},
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "a221fe90-04d0-4baf-bc16-f9d10f30fe33",
      "name": "Route on Status",
      "type": "n8n-nodes-base.switch",
      "position": [
        -64,
        1056
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Done",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "d8b8dbdc-1ad9-4ab9-8b2d-e76fd5db0899",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.status }}",
                    "rightValue": "COMPLETED"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Progress",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "9c10982c-5f8c-4eec-9b8a-f4b42e99ecf9",
                    "operator": {
                      "type": "string",
                      "operation": "notEquals"
                    },
                    "leftValue": "={{ $json.status }}",
                    "rightValue": "COMPLETED"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "1971d68b-8295-45dd-978b-cc27a09e148d",
      "name": "Fetch Final Result",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        144,
        1040
      ],
      "parameters": {
        "url": "=https://queue.fal.run/fal-ai/minimax-music/requests/{{ $json.request_id }}",
        "options": {},
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "cbc0a955-da9c-49c2-97a9-cbf459516fac",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -1216,
        1216
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-5-chat-latest",
          "cachedResultName": "gpt-5-chat-latest"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "fa63b886-0761-404d-9d3a-e5cdad9a3eed",
      "name": "Note: Chat Trigger",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1568,
        816
      ],
      "parameters": {
        "color": 6,
        "width": 380,
        "height": 208,
        "content": "## \ud83d\udcac When chat message received\n\n**Purpose:** Triggers on incoming chat prompts for instant song requests.\n\n**Note:** Integrates with n8n chat; passes message to agent."
      },
      "typeVersion": 1
    },
    {
      "id": "72a4a665-4631-4f4f-9d47-5921941ea43a",
      "name": "Note: Songwriting Core",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1328,
        1344
      ],
      "parameters": {
        "color": 5,
        "width": 364,
        "height": 176,
        "content": "## \ud83e\udd16 AI Songwriting Agent + OpenAI Chat Model + Parse Output\n\n**Purpose:** Generates 600+ char lyrics/genre via OpenAI, parses JSON output.\n\n**Note:** Uses gpt-5-chat-latest; enforces schema for style & lyrics fields."
      },
      "typeVersion": 1
    },
    {
      "id": "416b5063-e4f7-4854-8b42-820eae4acaf6",
      "name": "Note: Variable Setup",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -992,
        816
      ],
      "parameters": {
        "color": 3,
        "width": 364,
        "height": 208,
        "content": "## \ud83d\udcdd Set Script Variables\n\n**Purpose:** Extracts parsed lyrics/style into variables for music gen.\n\n**Note:** Maps output.lyrical_style & output.lyrics via expressions."
      },
      "typeVersion": 1
    },
    {
      "id": "6ad96629-57ac-4458-b8cb-d6268e3395cd",
      "name": "Note: Music Queue",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -832,
        1344
      ],
      "parameters": {
        "color": 6,
        "width": 396,
        "height": 112,
        "content": "## \ud83c\udfb5 Generate Music Track\n\n**Purpose:** POSTs lyrics/style to Fal.ai minimax-music queue."
      },
      "typeVersion": 1
    },
    {
      "id": "49d527f2-54d3-4379-99a3-8b6724ddbb05",
      "name": "Note: Polling Loop",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -432,
        816
      ],
      "parameters": {
        "color": 2,
        "width": 364,
        "height": 208,
        "content": "## \u23f3 Wait for Generation + \ud83d\udcca Check Generation Status\n\n**Purpose:** Waits 30s, then polls status endpoint until 'COMPLETED'.\n\n**Note:** Loops on non-complete; uses request_id & Fal.ai auth."
      },
      "typeVersion": 1
    },
    {
      "id": "7246b4e6-2d27-4ff3-a66e-31e08508f60e",
      "name": "Note: Completion Handler",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -224,
        1296
      ],
      "parameters": {
        "color": 2,
        "width": 348,
        "height": 208,
        "content": "## \ud83d\udd00 Route on Status + \ud83d\udce5 Fetch Final Result\n\n**Purpose:** Routes to fetch on 'COMPLETED', else loops; gets audio URL.\n\n**Note:** Case-sensitive status check; outputs link for chat response."
      },
      "typeVersion": 1
    },
    {
      "id": "118aa11c-d7b0-4fbc-8f2c-8f1e745152be",
      "name": "Parse Output1",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        -1072,
        1216
      ],
      "parameters": {
        "schemaType": "manual",
        "inputSchema": "={\n  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n  \"type\": \"object\",\n  \"properties\": {\n    \"lyrical_style\": {\n      \"type\": \"string\",\n      \"description\": \"The genre or style of the song (e.g., Pop, R&B, Rap, Rock, Country, Hip-Hop, Indie, Folk)\"\n    },\n    \"lyrics\": {\n      \"type\": \"string\",\n      \"description\": \"Complete song lyrics with all sections labeled (e.g., [VERSE 1], [CHORUS], [BRIDGE])\"\n    }\n  },\n  \"required\": [\"lyrical_style\", \"lyrics\"],\n  \"additionalProperties\": false\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "4e2839c3-cda0-4704-915c-a19d7fe2e487",
      "name": "Overview Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2336,
        464
      ],
      "parameters": {
        "color": 4,
        "width": 712,
        "height": 1176,
        "content": "# \ud83e\udd16 AI Songwriter Chat: Lyrics to Full Music Tracks\n\n## \ud83d\udccb What This Template Does\nThis chat-triggered workflow uses AI to generate detailed song lyrics (600+ chars) from messages, then creates matching music via Fal.ai. It polls until ready, returning lyrics and audio in chat.\n\n## \ud83d\udd27 Prerequisites\n- n8n with chat enabled\n- OpenAI API access\n- Fal.ai account\n\n## \ud83d\udd11 Required Credentials\n\n### OpenAI API Setup\n1. platform.openai.com \u2192 API keys\n2. Create secret key (e.g., \"n8n Songwriter\")\n3. Add as \"OpenAI API\" in n8n\n4. Test chat completion\n\n### Fal.ai HTTP Header Auth Setup\n1. fal.ai \u2192 Dashboard \u2192 API Keys\n2. Generate key\n3. n8n: HTTP Header Auth, Name=\"Fal.ai\", Header=\"Authorization: Key [Key]\"\n4. Test queue endpoint\n\n## \u2699\ufe0f Configuration Steps\n1. Import JSON to n8n\n2. Assign OpenAI to \"OpenAI Chat Model\"\n3. Assign Fal.ai to music HTTP nodes\n4. Activate; use n8n chat\n5. Test: \"Upbeat pop road trip song\"\n\n## \ud83c\udfaf Use Cases\n- Content creators: Quick jingles for videos\n- Educators: Custom tunes for classes\n- Gifting: Personalized tracks from chats\n- Artists: Real-time prototyping\n\n## \u26a0\ufe0f Troubleshooting\n- Invalid JSON: Stress JSON in prompt; test agent\n- Gen fails (401): Check Fal.ai perms/quotas\n- Polling loops: Increase wait to 45s; check queue\n- Lyrics <600 chars: Enforce fuller prompt structures"
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Parse Output1": {
      "ai_outputParser": [
        [
          {
            "node": "AI Songwriting Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Route on Status": {
      "main": [
        [
          {
            "node": "Fetch Final Result",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Wait for Generation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Songwriting Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Wait for Generation": {
      "main": [
        [
          {
            "node": "Check Generation Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Songwriting Agent": {
      "main": [
        [
          {
            "node": "Set Script Variables",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate Music Track": {
      "main": [
        [
          {
            "node": "Wait for Generation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Script Variables": {
      "main": [
        [
          {
            "node": "Generate Music Track",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Generation Status": {
      "main": [
        [
          {
            "node": "Route on Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "AI Songwriting Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}