{
  "name": "Create Spotify Playlist",
  "nodes": [
    {
      "parameters": {
        "method": "POST",
        "url": "https://accounts.spotify.com/api/token",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBasicAuth",
        "sendBody": true,
        "contentType": "form-urlencoded",
        "bodyParameters": {
          "parameters": [
            {
              "name": "grant_type",
              "value": "refresh_token"
            },
            {
              "name": "refresh_token",
              "value": "={{ $json['Refresh Token'] }}"
            },
            {
              "name": "client_id",
              "value": "={{ $json['Client ID'] }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        448,
        -48
      ],
      "id": "89b67129-76c9-4a4f-8748-9952b1665f6b",
      "name": "Spotify Refresh Token",
      "credentials": {
        "httpBasicAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "playlist",
        "id": "=spotify:playlist:{{ $('Settings').item.json['Playlist ID'] }}",
        "trackID": "={{ $json.uri }}",
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.spotify",
      "typeVersion": 1,
      "position": [
        1344,
        0
      ],
      "id": "687a9ef5-bd18-42f2-8537-1945a4e942e5",
      "name": "Add an Item to a playlist",
      "credentials": {
        "spotifyOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "fieldToSplitOut": "items",
        "options": {}
      },
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        896,
        -48
      ],
      "id": "6cf9e97a-511f-45a3-97f5-35045b6abd9d",
      "name": "Split Out"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        1120,
        -48
      ],
      "id": "152a0d29-4328-46a7-afea-5058cb123b09",
      "name": "Loop Over Items"
    },
    {
      "parameters": {
        "inputSource": "passthrough"
      },
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [
        0,
        -48
      ],
      "id": "a08a70f1-ec20-4e6a-b222-8f3b1c384f61",
      "name": "When Executed by Another Workflow"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "1c45d5e4-ede7-41f3-b0b5-c16043669b57",
              "name": "Dynamic Name",
              "value": true,
              "type": "boolean"
            },
            {
              "id": "11d57b9f-25a0-49f8-8d4f-cbf1bac8b9d8",
              "name": "Client ID",
              "value": "",
              "type": "string"
            },
            {
              "id": "003904ce-ee5a-4b72-815f-4d3d4e1a8d7f",
              "name": "Refresh Token",
              "value": "",
              "type": "string"
            },
            {
              "id": "ef3c561b-b2ea-414b-84dc-230a91e2d4c8",
              "name": "Playlist ID",
              "value": "",
              "type": "string"
            },
            {
              "id": "40ad8f64-4b88-4ba2-9cea-2ede8cb3b2c2",
              "name": "Limit",
              "value": "25",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        224,
        -48
      ],
      "id": "a617619b-9fe6-465c-a6b0-2da708f38ee7",
      "name": "Settings"
    },
    {
      "parameters": {
        "url": "https://api.spotify.com/v1/me/top/tracks",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "time_range",
              "value": "short_term"
            },
            {
              "name": "limit",
              "value": "={{ $('Settings').item.json.Limit }}"
            }
          ]
        },
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $json.access_token }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        672,
        -48
      ],
      "id": "cb4ce1c8-8a55-41b1-ba8e-eeec6cb0c142",
      "name": "Spotify Top Tracks"
    },
    {
      "parameters": {
        "operation": "formatDate",
        "date": "={{ $now }}",
        "format": "=MM/dd",
        "options": {}
      },
      "type": "n8n-nodes-base.dateTime",
      "typeVersion": 2,
      "position": [
        1568,
        -192
      ],
      "id": "76bc63fd-6ac3-45eb-8a8a-7d0b9ae7b2a2",
      "name": "Date & Time"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "e1c3a350-1e05-4dcc-a605-f27ef552deb8",
              "leftValue": "={{ $('Settings').item.json['Dynamic Name'] }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        1344,
        -192
      ],
      "id": "25958b73-ab68-40db-9b8d-58cb1a1b583c",
      "name": "If",
      "executeOnce": true
    },
    {
      "parameters": {
        "method": "PUT",
        "url": "=https://api.spotify.com/v1/playlists/{{ $('Settings').item.json['Playlist ID'] }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "=Bearer {{ $('Spotify Refresh Token').item.json.access_token }}"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "name",
              "value": "=Weekly Recap {{ $json.formattedDate }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.3,
      "position": [
        1792,
        -192
      ],
      "id": "3de7fd21-ba6b-43a8-ba4c-d4b1c44140f3",
      "name": "Spotify Change Playlist Name"
    }
  ],
  "connections": {
    "Spotify Refresh Token": {
      "main": [
        [
          {
            "node": "Spotify Top Tracks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Add an Item to a playlist",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add an Item to a playlist": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When Executed by Another Workflow": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Spotify Refresh Token",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Spotify Top Tracks": {
      "main": [
        [
          {
            "node": "Split Out",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Date & Time": {
      "main": [
        [
          {
            "node": "Spotify Change Playlist Name",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Date & Time",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "bd59e267-649a-46a0-8a1c-84f02a7a170c",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "id": "qNcMHuh607fFqY9z",
  "tags": []
}