Powershell – Deezer API

Published by mail@bewi.at on

To get a Deezer API Token you must first create an application. You can do this under following link.

Deezer for developers

I don’t know how to get a refresh token, so in this example you must enter your code every 1 hour. If you are in the 1 hour the script will check if there is a valid token ($DeezerAccessToken) and will not ask for a code.

function Get-DeezerToken {

    $AppId = "Your App Id"
    $SecretKey = "Your App Secret"
    $RedirectURI = "http://localhost"
    $Permissions = "manage_library"

    # If the Token expired or no token present request a token
    If (((Get-Date) -gt $DeezerAccessTokenDateExpires) -or (-not $DeezerAccessToken))
        # URL for the Browser
        $URL = "https://connect.deezer.com/oauth/auth.php?app_id="+$AppId+"&redirect_uri="+$RedirectURI+"&perms="+$Permissions
        Write-Host "Please open the following URL in the browser and copy your code from the URL Bar (URL copyied to clipboard):" -ForegroundColor Yellow
        Set-Clipboard -Value $URL
        Write-Host $URL
        Write-Host ""
        $Code = Read-Host "Please enter the code: "

        $URI = "https://connect.deezer.com/oauth/access_token.php?app_id="+$AppId+"&secret="+$SecretKey+"&code="+$Code
        $Result = Invoke-WebRequest -Uri $URI

        $AccessToken = $Result.Content.Replace("access_token=","")
        # ATTENTION: The lenght may be false (-12 or -13)
        $DeezerTokenTemp = $AccessToken.Substring(0,$AccessToken.Length-12)

        Clear-Variable -Name DeezerAccessTokenHeader -ErrorAction SilentlyContinue
        Clear-Variable -Name DeezerAccessToken -ErrorAction SilentlyContinue
        Set-Variable -Name DeezerAccessTokenHeader -Value $Result.Content -Scope Global
        Set-Variable -Name DeezerAccessToken -Value $DeezerTokenTemp -Scope Global
        Set-Variable -Name DeezerAccessTokenDateExpires -Value (Get-Date).AddSeconds(3540) -Scope Global
    return $DeezerAccessToken

Now it’s time for an example what you can do. First of all save the above script to some locatain. E.g. C:\DeezerAPI\Get-DeezerToken.ps1

As you can see we import the Get-DeezerToken.ps1 so we can call the function Get-DeezerToken.

At the bottom you see the Function Script. Lets call it Get-Deezer-Functions.ps1 This is also an Module that you can import in the final script.

. C:\DeezerAPI\Get-DeezerToken.ps1

$Header =  @{'Authorization' = 'Bearer ' + $DeezerAccessTokenHeader}

function Search-Deezer-Library() {

    # Create Search Filter
    [uri]$URI = "https://api.deezer.com/search?q=artist:`""+$Artist+"`"track:`""+$Title+"`""

    # Get First Track
    try {
        $Tracks = Invoke-RestMethod -Uri $URI -Method Get -Headers $Header
    catch {
    [array]$TrackID = $Tracks.data.Id
    return $TrackID

function Add-Deezer-Track {
    param (
    [uri]$URIPlaylist = "http://api.deezer.com/playlist/"+$PlaylistID+"/tracks?access_token="+$DeezerAccessToken+"&request_method=post&songs="+$TrackID
    try {
        Invoke-RestMethod -UseBasicParsing $URIPlaylist -Method Post
    catch {

function New-Deezer-Playlist {
    param (
    # You can find your User Id if you open a playlist in your Deezer Acount in the browser
    [uri]$URIPlaylist = "https://api.deezer.com/user/your_user_id/playlists?access_token="+$DeezerAccessToken+"&request_method=post&title="+$PlaylistName

    try {
        $PlaylistId = Invoke-RestMethod -UseBasicParsing $URIPlaylist -Method Post
    catch {
    return $PlaylistId.id

And now the final script.

# Load Functions
. C:\DeezerAPI\Get-Deezer-Functions.ps1

# Create new Playlist
$PlaylistName = "Some cool Playlist"
$PlaylistID = New-Deezer-Playlist -PlaylistName $PlaylistName

# Search for a Track
$Artist = "Fear Factory"
$Title = "Linchpin"
[array]$TrackID = Search-Deezer-Library -Artist $Artist -Title $Title

# Add track to your Playlist (Match 0 in the array)
Add-Deezer-Track -TrackID $TrackID[0] -PlaylistID $PlaylistID
Categories: APIDeezer


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

This site uses Akismet to reduce spam. Learn how your comment data is processed.