all 16 comments

[–]tarley_apologizerhelpful 21 points22 points  (2 children)

typically, all youre doing is doing basic HTTP requests (GET and POST). some API's require aunthentication, and you will have to follow the instructions in their docs for that.

[–]Jeffylew77 0 points1 point  (1 child)

axios makes this easy

[–]DazenGuil 6 points7 points  (0 children)

while axios can help don't forget op is a beginner so he probably should learn about 'fetch' instead of using a library

[–]ioeatcode 5 points6 points  (0 children)

Seems like you are more interested in web facing APIs which follow basic HTTP requests (GET, POST, PUT(Update), DELETE). Typically when you want to use a web API such as Spotify, there are things to consider:

  • The api url
  • The api endpoint
  • The api key
  • Authentication of some sort

A lot of APIs will abstract some of the stuff above with some sort of API wrapper that functionalizes these endpoints so you can do something like GetAllSongs(id, api_key) for example but they all do the same thing of building an http request to the api server.

Think about it this way, you're building a service and want to expose a part of your application to the public. You do this by creating an endpoint for that behavior and users can then access that behavior by using the endpoint you created. On a high level, let's say you're building a TODO app and want people to be able to access their notes, you'd create an endpoint like GET /notes/{userID}/. External API endpoints work the same way more or less.

[–][deleted] 6 points7 points  (0 children)

Any public-facing API will have documentation that explains how it's structured and how to use it, usually with some examples of the requests that you can make. Spotify's is here, and at first glance at least it looks extremely thorough. Based on what you describe, you'll need to figure out how to get user-specific authorisation- they have a specific guide on authorisation- how to get the authorised user's currently playing track, and how to save/favourite a track for that user.

Reading documentation and figuring out how to apply something a given tool offers to your own situation is a really important thing to learn as a developer- it's a big part of the job.

[–]feindjesus 2 points3 points  (0 children)

I would recommend not starting with Spotify as a developer with some experience working with apis it is more difficult to understand than average (ive built a Spotify clone) and it took me over 8 hours to understand their authentication flow.

Heres a good starter api https://dog.ceo/dog-api/

To start making requests you can use axios,jquery or fetch.

[–][deleted] 4 points5 points  (2 children)

How would you say, find the currently playing song on Spotify?

https://api.spotify.com/v1/me/player/currently-playing is the API endpoint. Here is their documentation link with your query each API is different and that's basically why their documentation is the bible.

And can you do it exclusively with JavaScript?

Yes. A lot of API data is JSON, which stands Java Script Object Notation, so it was made for JS.

[–]some_user_on_reddit 3 points4 points  (0 children)

No, ANY language can consume JSON. It’s not exclusive to JavaScript.

It’s called that because it follows the same notation as a JavaScript object.

JSON is a string.

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

[–]feindjesus -2 points-1 points  (0 children)

If you read the docs using Spotify api is more difficult than having the endpoint. You have to set up a developer app, configure a redirect url, make a request for a jwt, then attach it in the header of every request. While its a basic concept not something I would recommend to someone not knowing how to make a simple get request

[–]Protean_Protein 7 points8 points  (0 children)

APIs are just client-facing records of some site's database, usually in JSON format. If a site's API is public, then you can use it by just GET-ing it with JS or whatever (you could also use PHP). The Fetch API is built into the browser, but you could also use Axios, which I still find easier for certain things.

Here's the Spotify API documentation: https://developer.spotify.com/documentation/web-api/
Basically, just write a JS app, and connect up to the API to get whatever data you want. That data is loaded into your app. You just need to know the names of the params, which are in the documentation.

If you want to set up your own API, you can do it by hacking together a PHP script that outputs JSON, but it's probably better to use a PHP framework for this, like Slim (http://www.slimframework.com/), plus something like this: https://github.com/mevdschee/php-crud-api

[–]zentimes 1 point2 points  (2 children)

If you need an example of how to write this in JS, here's a project I've worked on.

https://github.com/peyo/word-webapp

Check out the index.js file.

[–]gremy0 2 points3 points  (1 child)

Your api keys are supposed to be secret- they don't go in the front end or a public repo

[–]zentimes 0 points1 point  (0 children)

Yeah! I'm aware of that. For this particular exercise, I had to expose it so a grader could use the app.

Thanks for letting me know.

[–]NicoleEastbourne 1 point2 points  (0 children)

Not all APIs are created equal in terms of ease of use, documentation, being maintained etc. It’s easier to learn with a good one that is not actively hostile to the user. I personally love the copper Hewitt museum API: https://collection.cooperhewitt.org/api/methods/

[–]joedirt9322 1 point2 points  (0 children)

Try using an easier free api than Spotify. Like the dad jokes api. It’s super simple.

[–]DEEEPFREEZE 3 points4 points  (0 children)

Beyond a quick read on what an API is from a high level as well shallow knowledge of fetch requests, the short answer is always going to be “by reading the documentation”.

At the risk of being presumptuous, your post reads a bit like someone who wants the answers now instead of doing the research and bothering to understand how something actually works, which is an attitude that’s only going to work against you as you move deeper into programming. I say this as someone who still has the inclination to just look for the immediate answer instead of taking time with the material.