0

as I'm following some tutorials, I dont see curl examples (in Authorization guide for example), I think because spotify api guide changed? Isn't possibile to find the old guide?

Thank you

Aidoru
  • 555
  • 1
  • 5
  • 16

1 Answers1

0

Using curl in bash terminal for Spotify APIs call.

Assign environment variables

CLIENT_ID='*************** your client ID ***************'
CLIENT_SECRET='*********** your client secret ***********'
PLAYLIST_ID='37i9dQZF1DX2Zjg8kBuEsQ'
TRACK_ID='2TpxZ7JUBn3uw46aR7qd6V'
CLIENT_ID_SECRET=$(echo -n $CLIENT_ID:$CLIENT_SECRET | base64 -w 0)

Get an access-token with Client Credentials Flow

If you want to get it by Code Flow, in here

enter image description here

ACCESS_TOKEN=$(curl --location --request POST 'https://accounts.spotify.com/api/token' \
--header 'Authorization: Basic '$CLIENT_ID_SECRET \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=client_credentials' | jq -r '.access_token')
echo 'ACCESS_TOKEN = '$ACCESS_TOKEN

get track API call

enter image description here

curl --request GET \
    'https://api.spotify.com/v1/tracks/'$TRACK_ID \
     --header "Authorization: Bearer "$ACCESS_TOKEN

Result

  "album" : {
    "album_type" : "album",
    "artists" : [ {
      "external_urls" : {
        "spotify" : "https://open.spotify.com/artist/08td7MxkoHQkXnWAYD8d6Q"
      },
      "href" : "https://api.spotify.com/v1/artists/08td7MxkoHQkXnWAYD8d6Q",
      "id" : "08td7MxkoHQkXnWAYD8d6Q",
      "name" : "Tania Bowra",
      "type" : "artist",
      "uri" : "spotify:artist:08td7MxkoHQkXnWAYD8d6Q"
    } ],
    "available_markets" : [ "AD", "AE", "AG", "AL", "AM", "AO", "AR", "AT", "AU", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BN", "BO", "BR", "BS", "BT", "BW", "BY", "BZ", "CA", "CD", "CG", "CH", "CI", "CL", "CM", "CO", "CR", "CV", "CW", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FJ", "FM", "FR", "GA", "GB", "GD", "GE", "GH", "GM", "GN", "GQ", "GR", "GT", "GW", "GY", "HK", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IN", "IQ", "IS", "IT", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KR", "KW", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MG", "MH", "MK", "ML", "MN", "MO", "MR", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NE", "NG", "NI", "NL", "NO", "NP", "NR", "NZ", "OM", "PA", "PE", "PG", "PH", "PK", "PL", "PS", "PT", "PW", "PY", "QA", "RO", "RS", "RW", "SA", "SB", "SC", "SE", "SG", "SI", "SK", "SL", "SM", "SN", "SR", "ST", "SV", "SZ", "TD", "TG", "TH", "TJ", "TL", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "US", "UY", "UZ", "VC", "VE", "VN", "VU", "WS", "XK", "ZA", "ZM", "ZW" ],
    "external_urls" : {
      "spotify" : "https://open.spotify.com/album/6akEvsycLGftJxYudPjmqK"
    },
    "href" : "https://api.spotify.com/v1/albums/6akEvsycLGftJxYudPjmqK",
    "id" : "6akEvsycLGftJxYudPjmqK",
    "images" : [ {
      "height" : 640,
      "url" : "https://i.scdn.co/image/ab67616d0000b2731ae2bdc1378da1b440e1f610",
      "width" : 640
    }, {
      "height" : 300,
      "url" : "https://i.scdn.co/image/ab67616d00001e021ae2bdc1378da1b440e1f610",
      "width" : 300
    }, {
      "height" : 64,
      "url" : "https://i.scdn.co/image/ab67616d000048511ae2bdc1378da1b440e1f610",
      "width" : 64
    } ],
    "name" : "Place In The Sun",
    "release_date" : "2004-02-02",
    "release_date_precision" : "day",
    "total_tracks" : 11,
    "type" : "album",
    "uri" : "spotify:album:6akEvsycLGftJxYudPjmqK"
  },
  "artists" : [ {
    "external_urls" : {
      "spotify" : "https://open.spotify.com/artist/08td7MxkoHQkXnWAYD8d6Q"
    },
    "href" : "https://api.spotify.com/v1/artists/08td7MxkoHQkXnWAYD8d6Q",
    "id" : "08td7MxkoHQkXnWAYD8d6Q",
    "name" : "Tania Bowra",
    "type" : "artist",
    "uri" : "spotify:artist:08td7MxkoHQkXnWAYD8d6Q"
  } ],
  "available_markets" : [ "AD", "AE", "AG", "AL", "AM", "AO", "AR", "AT", "AU", "AZ", "BA", "BB", "BD", "BE", "BF", "BG", "BH", "BI", "BJ", "BN", "BO", "BR", "BS", "BT", "BW", "BY", "BZ", "CA", "CD", "CG", "CH", "CI", "CL", "CM", "CO", "CR", "CV", "CW", "CY", "CZ", "DE", "DJ", "DK", "DM", "DO", "DZ", "EC", "EE", "EG", "ES", "FI", "FJ", "FM", "FR", "GA", "GB", "GD", "GE", "GH", "GM", "GN", "GQ", "GR", "GT", "GW", "GY", "HK", "HN", "HR", "HT", "HU", "ID", "IE", "IL", "IN", "IQ", "IS", "IT", "JM", "JO", "JP", "KE", "KG", "KH", "KI", "KM", "KN", "KR", "KW", "KZ", "LA", "LB", "LC", "LI", "LK", "LR", "LS", "LT", "LU", "LV", "LY", "MA", "MC", "MD", "ME", "MG", "MH", "MK", "ML", "MN", "MO", "MR", "MT", "MU", "MV", "MW", "MX", "MY", "MZ", "NA", "NE", "NG", "NI", "NL", "NO", "NP", "NR", "NZ", "OM", "PA", "PE", "PG", "PH", "PK", "PL", "PS", "PT", "PW", "PY", "QA", "RO", "RS", "RW", "SA", "SB", "SC", "SE", "SG", "SI", "SK", "SL", "SM", "SN", "SR", "ST", "SV", "SZ", "TD", "TG", "TH", "TJ", "TL", "TN", "TO", "TR", "TT", "TV", "TW", "TZ", "UA", "UG", "US", "UY", "UZ", "VC", "VE", "VN", "VU", "WS", "XK", "ZA", "ZM", "ZW" ],
  "disc_number" : 1,
  "duration_ms" : 276773,
  "explicit" : false,
  "external_ids" : {
    "isrc" : "AUCR10410001"
  },
  "external_urls" : {
    "spotify" : "https://open.spotify.com/track/2TpxZ7JUBn3uw46aR7qd6V"
  },
100  4464  100  4464    0     0  14679      0 --:--:-- --:--:-- --:--:-- 14929otify.com/v1/tracks/2TpxZ7JUBn3uw46aR7qd6V",
  "id" : "2TpxZ7JUBn3uw46aR7qd6V",
  "is_local" : false,
  "name" : "All I Want",
  "popularity" : 1,
  "preview_url" : "https://p.scdn.co/mp3-preview/e732f39d38a88261132e5a959c051f2888c3383d?cid=32b3c5ccc31f4ba7b90b7c0d50f5bc7b",
  "track_number" : 1,
  "type" : "track",
  "uri" : "spotify:track:2TpxZ7JUBn3uw46aR7qd6V"
}

enter image description here

get playlist API call

curl --request GET \
    'https://api.spotify.com/v1/playlists/'$PLAYLIST_ID \
     --header "Authorization: Bearer "$ACCESS_TOKEN

enter image description here

Result

{
    "collaborative": false,
    "description": "       Cover: Lizzo",
    "external_urls": {
        "spotify": "https://open.spotify.com/playlist/37i9dQZF1DX2Zjg8kBuEsQ"
    },
    "followers": {
        "href": null,
        "total": 14094
    },
    "href": "https://api.spotify.com/v1/playlists/37i9dQZF1DX2Zjg8kBuEsQ",
    "id": "37i9dQZF1DX2Zjg8kBuEsQ",
    "images": [
        {
            "height": null,
            "url": "https://i.scdn.co/image/ab67706f00000003b60f10418f22555435b8b773",
            "width": null
        }
    ],
    "name": "Best of !!! 2022",
    "owner": {
        "display_name": "Spotify",
        "external_urls": {
            "spotify": "https://open.spotify.com/user/spotify"
        },
        "href": "https://api.spotify.com/v1/users/spotify",
        "id": "spotify",
        "type": "user",
        "uri": "spotify:user:spotify"
    },
    "primary_color": "#ffffff",
    "public": false,
    "snapshot_id": "MTY2OTgxNTk2MCwwMDAwMDAwMDdjZTJlYmM5ODM2ZjhiMzZiZGYzMGEyMGZlNzg1MTRk",
    "tracks": {
        "href": "https://api.spotify.com/v1/playlists/37i9dQZF1DX2Zjg8kBuEsQ/tracks?offset=0&limit=100",
        "items": [
            {
                "added_at": "2022-11-30T13:46:00Z",
                "added_by": {
                    "external_urls": {
                        "spotify": "https://open.spotify.com/user/"
                    },
                    "href": "https://api.spotify.com/v1/users/",
                    "id": "",
                    "type": "user",
                    "uri": "spotify:user:"
                },
                "is_local": false,
                "primary_color": null,
                "track": {
                    "album": {
                        "album_type": "album",
                        "artists": [
                            {
                                "external_urls": {
                                    "spotify": "https://open.spotify.com/artist/56oDRnqbIiwx4mymNEv7dS"
                                },
                                "href": "https://api.spotify.com/v1/artists/56oDRnqbIiwx4mymNEv7dS",
                                "id": "56oDRnqbIiwx4mymNEv7dS",
                                "name": "Lizzo",
                                "type": "artist",
                                "uri": "spotify:artist:56oDRnqbIiwx4mymNEv7dS"
                            }
                        ],
                        "available_markets": [
                            "AD",
                            "AE",
                            "AG",
                            "AL",
                            "AM",
                            "AO",
                            "AR",
                            "AT",
                            "AU",
                            "AZ",
                            "BA",
                            "BB",
                            "BD",
                            "BE",
                            "BF",
                            "BG",
                            "BH",
                            "BI",
                            "BJ",
                            "BN",
                            "BO",
                            "BR",
                            "BS",
                            "BT",
                            "BW",
                            "BZ",
                            "CA",
                            "CD",
                            "CG",
                            "CH",
                            "CI",
                            "CL",
                            "CM",
                            "CO",
                            "CR",
                            "CV",
                            "CW",
                            "CY",
                            "CZ",
                            "DE",
                            "DJ",
                            "DK",
                            "DM",
                            "DO",
                            "DZ",
                            "EC",
                            "EE",
                            "EG",
                            "ES",
                            "FI",
                            "FJ",
                            "FM",
                            "FR",
                            "GA",
                            "GB",
                            "GD",
                            "GE",
                            "GH",
                            "GM",
                            "GN",
                            "GQ",
                            "GR",
                            "GT",
                            "GW",
                            "GY",
                            "HK",
                            "HN",
                            "HR",
                            "HT",
                            "HU",
                            "ID",
                            "IE",
                            "IL",
                            "IN",
                            "IQ",
                            "IS",
                            "IT",
                            "JM",
                            "JO",
                            "JP",
                            "KE",
                            "KG",
                            "KH",
                            "KI",
                            "KM",
                            "KN",
                            "KR",
                            "KW",
                            "KZ",
                            "LA",
                            "LB",
                            "LC",
                            "LI",
                            "LK",
                            "LR",
                            "LS",
                            "LT",
                            "LU",
                            "LV",
                            "LY",
                            "MA",
                            "MC",
                            "MD",
                            "ME",
                            "MG",
                            "MH",
                            "MK",
                            "ML",
                            "MN",
                            "MO",
                            "MR",
                            "MT",
                            "MU",
                            "MV",
                            "MW",
                            "MX",
                            "MY",
                            "MZ",
                            "NA",
                            "NE",
                            "NG",
                            "NI",
                            "NL",
                            "NO",
                            "NP",
                            "NR",
                            "NZ",
                            "OM",
                            "PA",
                            "PE",
                            "PG",
                            "PH",
                            "PK",
                            "PL",
                            "PS",
                            "PT",
                            "PW",
                            "PY",
                            "QA",
                            "RO",
                            "RS",
                            "RW",
                            "SA",
                            "SB",
                            "SC",
                            "SE",
                            "SG",
                            "SI",
                            "SK",
                            "SL",
                            "SM",
                            "SN",
                            "SR",
                            "ST",
                            "SV",
                            "SZ",
                            "TD",
                            "TG",
                            "TH",
                            "TJ",
                            "TL",
                            "TN",
                            "TO",
                            "TR",
                            "TT",
                            "TV",
                            "TW",
                            "TZ",
                            "UA",
                            "UG",
                            "US",
                            "UY",
                            "UZ",
                            "VC",
                            "VE",
                            "VN",
                            "VU",
                            "WS",
                            "XK",
                            "ZA",
                            "ZM",
                            "ZW"
                        ],
                        "external_urls": {
                            "spotify": "https://open.spotify.com/album/1KtDsGsSRGbnmH07v5hB1I"
                        },
                        "href": "https://api.spotify.com/v1/albums/1KtDsGsSRGbnmH07v5hB1I",
                        "id": "1KtDsGsSRGbnmH07v5hB1I",
                        "images": [
                            {
                                "height": 640,
                                "url": "https://i.scdn.co/image/ab67616d0000b273caa75a1b27530c05d8b76675",
                                "width": 640
                            },
                            {
                                "height": 300,
                                "url": "https://i.scdn.co/image/ab67616d00001e02caa75a1b27530c05d8b76675",
                                "width": 300
                            },
                            {
                                "height": 64,
                                "url": "https://i.scdn.co/image/ab67616d00004851caa75a1b27530c05d8b76675",
                                "width": 64
                            }
                        ],
                        "name": "Special",
                        "release_date": "2022-07-14",
                        "release_date_precision": "day",
                        "total_tracks": 12,
                        "type": "album",
                        "uri": "spotify:album:1KtDsGsSRGbnmH07v5hB1I"
                    },
                    "artists": [
                        {
                            "external_urls": {
                                "spotify": "https://open.spotify.com/artist/56oDRnqbIiwx4mymNEv7dS"
                            },
                            "href": "https://api.spotify.com/v1/artists/56oDRnqbIiwx4mymNEv7dS",
                            "id": "56oDRnqbIiwx4mymNEv7dS",
                            "name": "Lizzo",
                            "type": "artist",
                            "uri": "spotify:artist:56oDRnqbIiwx4mymNEv7dS"
                        }
                    ],
                    "available_markets": [
                        "AD",
                        "AE",
                        "AG",
                        "AL",
                        "AM",
                        "AO",
                        "AR",
                        "AT",
                        "AU",
                        "AZ",
                        "BA",
                        "BB",
                        "BD",
                        "BE",
                        "BF",
                        "BG",
                        "BH",
                        "BI",
                        "BJ",
                        "BN",
                        "BO",
                        "BR",
                        "BS",
                        "BT",
                        "BW",
                        "BZ",
                        "CA",
                        "CD",
                        "CG",
                        "CH",
                        "CI",
                        "CL",
                        "CM",
                        "CO",
                        "CR",
                        "CV",
                        "CW",
                        "CY",
                        "CZ",
                        "DE",
                        "DJ",
                        "DK",
                        "DM",
                        "DO",
                        "DZ",
                        "EC",
                        "EE",
                        "EG",
                        "ES",
                        "FI",
                        "FJ",
                        "FM",
                        "FR",
                        "GA",
                        "GB",
                        "GD",
                        "GE",
                        "GH",
                        "GM",
                        "GN",
                        "GQ",
                        "GR",
                        "GT",
                        "GW",
                        "GY",
                        "HK",
                        "HN",
                        "HR",
                        "HT",
                        "HU",
                        "ID",
                        "IE",
                        "IL",
                        "IN",
                        "IQ",
                        "IS",
                        "IT",
                        "JM",
                        "JO",
                        "JP",
                        "KE",
                        "KG",
                        "KH",
                        "KI",
                        "KM",
                        "KN",
                        "KR",
                        "KW",
                        "KZ",
                        "LA",
                        "LB",
                        "LC",
                        "LI",
                        "LK",
                        "LR",
                        "LS",
                        "LT",
                        "LU",
                        "LV",
                        "LY",
                        "MA",
                        "MC",
                        "MD",
                        "ME",
                        "MG",
                        "MH",
                        "MK",
                        "ML",
                        "MN",
                        "MO",
                        "MR",
                        "MT",
                        "MU",
                        "MV",
                        "MW",
                        "MX",
                        "MY",
                        "MZ",
                        "NA",
                        "NE",
                        "NG",
                        "NI",
                        "NL",
                        "NO",
                        "NP",
                        "NR",
                        "NZ",
                        "OM",
                        "PA",
                        "PE",
                        "PG",
                        "PH",
                        "PK",
                        "PL",
                        "PS",
                        "PT",
                        "PW",
                        "PY",
                        "QA",
                        "RO",
                        "RS",
                        "RW",
                        "SA",
                        "SB",
                        "SC",
                        "SE",
                        "SG",
                        "SI",
                        "SK",
                        "SL",
                        "SM",
                        "SN",
                        "SR",
                        "ST",
                        "SV",
                        "SZ",
                        "TD",
                        "TG",
                        "TH",
                        "TJ",
                        "TL",
                        "TN",
                        "TO",
                        "TR",
                        "TT",
                        "TV",
                        "TW",
                        "TZ",
                        "UA",
                        "UG",
                        "US",
                        "UY",
                        "UZ",
                        "VC",
                        "VE",
                        "VN",
                        "VU",
                        "WS",
                        "XK",
                        "ZA",
                        "ZM",
                        "ZW"
                    ],
                    "disc_number": 1,
                    "duration_ms": 191937,
                    "episode": false,
                    "explicit": false,
                    "external_ids": {
                        "isrc": "USAT22203696"
                    },
                    "external_urls": {
                        "spotify": "https://open.spotify.com/track/4a3D8glGZxhlJfOs1Czc3c"
                    },
                    "href": "https://api.spotify.com/v1/tracks/4a3D8glGZxhlJfOs1Czc3c",
                    "id": "4a3D8glGZxhlJfOs1Czc3c",
                    "is_local": false,
                    "name": "About Damn Time",
                    "popularity": 60,
                    "preview_url": "https://p.scdn.co/mp3-preview/62c968640b6841d1204c7dcb17a06f0fc6e2db1a?cid=32b3c5ccc31f4ba7b90b7c0d50f5bc7b",
                    "track": true,
                    "track_number": 2,
                    "type": "track",
                    "uri": "spotify:track:4a3D8glGZxhlJfOs1Czc3c"
                },
                "video_thumbnail": {
                    "url": null
                }
            },

...

enter image description here

Bench Vue
  • 5,257
  • 2
  • 10
  • 14
  • Aidoru asked for [Authorization](https://developer.spotify.com/documentation/general/guides/authorization/code-flow/) (for example), not Client Credentials. – Ximzend Dec 30 '22 at 09:46
  • 1
    @Ximzend, code flow in [here](https://stackoverflow.com/questions/74651517/expo-auth-session-spotify-get-refresh-token/74655243#74655243), I update my answer to add link. Thanks! – Bench Vue Dec 30 '22 at 13:09
  • @Aidoru, I made [`autorization_code`](https://stackoverflow.com/questions/75286588/spotify-web-api-call-gives-wrong-code-python/75292843#75292843) flow by Python. It will help your question. – Bench Vue Jan 31 '23 at 05:02