Public/New-SpotifyPlaylist.ps1

<#
.SYNOPSIS
Creates a new Spotify playlist

.DESCRIPTION
Creates a new playlist in your Spotify account. By default, the playlist is
private. Use -Public to create a public playlist. Returns the created playlist
object, including its 'id' which can be passed to Add-SpotifyPlaylistTracks.

.PARAMETER Name
The name of the new playlist.

.PARAMETER Description
Optional. A description for the playlist.

.PARAMETER Public
If specified, creates a public playlist. Default is private.

.PARAMETER ClientId
Optional. The ClientId of the app you registered in the Spotify developer
portal.

.PARAMETER RedirectURI
Optional. The redirect URI used for OAuth authentication.

.PARAMETER ConfigFile
Optional. The path to a JSON configuration file containing 'ClientId' and
'RedirectURI' properties.

.EXAMPLE
$playlist = New-SpotifyPlaylist -Name "My Playlist"
Add-SpotifyPlaylistTracks -PlaylistId $playlist.id -TrackUris "spotify:track:4iV5W9uYEdYUVa79Axb7Rh"

.EXAMPLE
New-SpotifyPlaylist -Name "My Public Playlist" -Description "Weekend vibes" -Public
#>

function New-SpotifyPlaylist {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string] $Name,

        [Parameter(Mandatory=$false)]
        [string] $Description,

        [Parameter(Mandatory=$false)]
        [switch] $Public,

        [Parameter(Mandatory=$false)]
        [string] $ClientId,

        [Parameter(Mandatory=$false)]
        [string] $RedirectURI,

        [ValidateScript({Test-Path $_})]
        [Parameter(Mandatory=$false)]
        [string] $ConfigFile
    )

    Set-StrictMode -Version 1.0
    $ErrorActionPreference = 'Stop'

    $scope = if ($Public) { 'playlist-modify-public' } else { 'playlist-modify-private' }
    $TokenParams = @{ Scopes = @($scope) }
    foreach ($param in @('ClientId', 'RedirectURI', 'ConfigFile')) {
        if ($PSBoundParameters.ContainsKey($param)) {
            $TokenParams.Add($param, $PSBoundParameters[$param])
        }
    }
    $token = Get-SpotifyToken @TokenParams
    $headers = @{ Authorization = "Bearer $token" }

    $body = @{
        name   = $Name
        public = [bool]$Public
    }
    if ($PSBoundParameters.ContainsKey('Description')) {
        $body.description = $Description
    }

    $response = (
        Invoke-SpotifyRequest `
            -Uri $script:MYPLAYLISTS_URI `
            -Method Post `
            -ContentType 'application/json' `
            -Body ($body | ConvertTo-Json) `
            -Headers $headers
    ).Content | ConvertFrom-Json

    return $response
}