0

I am delving into spotify and javascript is not my main programming language so I managed to get some snippets together from a code that uses ajax (which I would rather not use) but still it returns nothing so I am wondering if some more experienced people out there could help me get started with a template to call the api.

My goal for this test is to search an artist name and get the first result (I expect many names will return multiple artists)

Most of what is in the documentation is curl and I didn't find the demos very helpful.

What I have so far is something like this:

function getArtistName (artistName) {
    var artistID;
    var searchArtists = function (query) {
        $.ajax({
            url: 'https://api.spotify.com/v1/search',
            data: {
                q: query,
                type: 'artist',
                'accessToken': 'BQBvW70gHJ20Flc8cHErqg8s72bfTePbssblED-gpEuHFr_Yezesbthok8qaKBmjzo2WjWo9J7ZcTpSwvV8MZ_cW_E7UkrG_HF2R6gFQcqfdupgYGmoYsdRdt1q3tq2NU3pPgauuzmFLkUpdAuNp3shdVXJz2SzvnA',
                'query': artistName,
                limit: '1.'
            },
            success: function (response) {
                //resultsPlaceholder.innerHTML = template(response);
            }
        });
    };
      console.log(searchArtists);

      return artistID;
    }

Some points of confusion:

  1. The key seems to expire. I have a client ID on my profile but I am not sure where I can generate this token other than the "try it out" demo on the site.

  2. What does this actually return, an ID or a JSON?

badner
  • 768
  • 2
  • 10
  • 31
  • this is javascript, not java. You can do it all on the server if you rather not use AJAX – Paul Fitzgerald Jan 08 '18 at 16:22
  • If you want json, use `$.getJSON` or use the `type` (?) property of `$.ajax` – Jonas Wilms Jan 08 '18 at 16:24
  • sorry for the slip up, I tagged it as javascript. How can I do this on the server, do you have a helpful example? – badner Jan 08 '18 at 16:24
  • @badner now you are mixing it up. Thats another question not related to this one... – Jonas Wilms Jan 08 '18 at 16:26
  • And you should read [about js's asynchronity](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Jonas Wilms Jan 08 '18 at 16:28
  • @JonasW.All I want to do is call spotify and query an artist name and get an ID – badner Jan 08 '18 at 16:28
  • Spotify API examples - https://developer.spotify.com/web-api/code-examples/ - plenty for Java, Go, Python, Ruby etc – timothyclifford Jan 08 '18 at 16:35
  • @timothyclifford yes and that is where I found this guy http://jsfiddle.net/JMPerez/0u0v7e1b/ which I followed here... but thank you anyway – badner Jan 08 '18 at 16:36
  • That's a JS Fiddle which is JavaScript on the client. If you want on the server, check out one of the server side options. Even the official Spotify example is written in Node.js which is server side... https://github.com/spotify/web-api-auth-examples – timothyclifford Jan 08 '18 at 16:49

1 Answers1

2

Here is a demo app that searches tracks using Node.js, or server-side Javascript: https://spotify-quicksearch.glitch.me/

If you click the "Remix this on Glitch" link on the page, you can see and edit the source.

The call to the API is made in server.js. First, we set the client ID and client secret, which are from the dashboard, as you've noted. In this example, we use those to get an access token using the Client Credentials Flow. You can read about all the authentication flows here: https://beta.developer.spotify.com/documentation/general/guides/authorization-guide/

This particular example uses an API wrapper called spotify-web-api-node, which just makes it easier to interact with the API through Javascript functions. To search for artists instead, just change searchTracks to searchArtists.

To answer your second question - all calls to the Spotify API return JSON. You can see the format of the full JSON response here: https://beta.developer.spotify.com/documentation/web-api/reference/search/search/. Roughly, it looks like this:

artists: {
  items: [
    {
      id: <id>,
      name: <name>,
      ...
    }
    ...
  ]
}

To get the ID from the JSON, you need to parse the JSON object. You can see how I do this in the example in line 21 of client.js. You can modify that code to get just the ID of the first artist like this:

data.artists.items[0].id

Update: made an example that should be even more relevant:

https://spotify-search-artist.glitch.me/

Community
  • 1
  • 1
arirawr
  • 1,255
  • 9
  • 22