Struggling with API

This is the first time i am using api .I want to use weather api . I have open the mention link in fcc weather project , but i am unable to understand it very well .Do any one here know some basic or api free cources on web so that i can take help from those .

Yes, APIs are confusing. And there are some known problems with some of the APIs.

If you post the link to your pen and let us know exactly what the problem is you’re having, we can help you out.

If you want more general info, I’d go on youtube and search for things about AJAX, JSON, API, etc.

1 Like

From what I gathered, APIs mostly consist of type of HTTP method(GET,POST,PUT DELETE), a rootURL api.openweathermap.org, endpoints, and parameters that leads you to an endpoint that allow you access to certain data.

in the case of the Open Weather API, you get the data, by making a GET request to the rootURL http://api.openweathermap.org and endpoint /data/2.5/weather, and you put in the specific parameters like &q=London&key=[insert your own api key]

so if you go to http://api.openweathermap.org/data/2.5/weather?q=London&key=somekey, it sends you a JSON object that represents their data.

The function you use to access the api tend to be asynchronous, because you don’t know how long it’d take to fetch the data or even if you’d get the data at all, so you have to code in a way that rest of your program keeps on running, and deal with the data whenever it arrive, usually in a callback function. I think this is the hardest part about working with API because if you’re not familiar with the concept of callbacks, it’s difficult to escape the thinking of sequential programming.

1 Like

Basically most web API are designed to be RESTful (openweathermap.org is one example), which use various HTTP methods including POST, PUT, DELETE, HEAD, PATCH, GET etc. to send and receive information and URL parameters to set the options. In order for you to really understand RESTful, I think it’s better to understand how HTTP requests are made, you can actually play with it using nothing more than a browser and a developer tool. Or you can read more about HTTP requests here.

So what shall i do now to keep moving with API

"“I think it’s better to understand how HTTP requests are made, you can actually play with it using nothing more than a browser and developer tools”

Can you describe it in little bit detail ?

When you take a look at their API here, you will find a number of endpoints including Current weather data, 5 day / 3 hour forecast, 16 day / daily forecast etc… All of them require an API key, you need to include them in your GET param (you can find the documentation here), for example http://samples.openweathermap.org/data/2.5/forecast?id=524901&appid=b1b15e88fa797225412429c1c50c122a1, in this sample url here, http://samples.openweathermap.org/data/2.5/forecast is an endpoint, which will give you the data you require, anything after the ? is the GET params in key=value pair, if the URL contain more than 1 params, it’s separated by & so in our example here there are 2 params passed to the endpoint, id and appid.

Let say you wanted to try Current weather data endpoint (documentation here), after checking the doc, you started playing with their API, you construct the URL based on the city you are in and the API key you got from your openweathermap account, you will end up something like this: http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a1, you can open this URL in a browser, but probably the data you got looked very messy, it’s in JSON format, so I think it’s a good idea to install an extension like JSONView or you can use Google Chrome’s Developer tool, which will automatically parse JSON format for you (under Network tab, find your API request and click on it, a window will open on the right, then click on the Preview tab).

Note that http://samples.openweathermap.org is a sample API response, you need to change that URL to http://api.openweathermap.org (with an endpoint, api key and other params want to pass to the API) for the API to work.

I get your point .Does the same concept is used in all type of api whether it would be wiki pedia , twitter api etc .or every api has different concepts to be used .

Openweathermap.org only use one HTTP method, the GET method, other APIs like Wikipedia might use POST, PUT, GET, DELETE or HEAD, the concept is still the same, but there some API endpoints that don’t accept GET params, in that case you have to pass the params as JSON HTTP body. In more advanced cases, it might be required to add additional HTTP headers for authentication like Auth0 which uses Bearer Authentication Scheme, but I have never come across anything like that yet in FCC. Just check the API documentation, they have all the information there.

For simply interacting APIs, the openweathermap is a good place to start. You don’t have to start by building the page and rendering data, focus on retrieving and manipulating the data locally.

Start by logging the data to the console, get a feel of what JSON object looks like, how to access specific properties like temperature, descriptions…etc.

Move on to more complex api calls, like retrieving the current weather for several cities, see what parameter you can use and get different data.

After that try writing a simple callback function that does basic local manipulation, like logging the average temperature of 5 different cities, or only logging a message when it over 60 degrees.

After you have a grasp on the data scheme and using callbacks to get what you want out of the response, then you can move on to actually showing the data on your page.

JQuery’s $get is probably good enough for all 4 intermediate projects, none of them require you to manipulate data and send it back to the server, so GET is all you need. After you get that work out, you can try to migrate from JQuery to the Web API method, XMLHttpRequest(), which requires you to to learn more about HTTP status and JavaScript events.

If it helps you understanding from the other end, you can try looking into the back-end part of the curriculum that teaches you how to build RESTful APIs.

EDIT Please ignore. After working with the codecademy material for a few lesson I realize it is very buggy. I doesn’t look like codecademy udates the api material and the QA forum is locked. I will have to find some better api training material it seems.

I am also getting into API:s right now. I have just started with the api training from codecademy. This training is a bit tricky to find since it’s not listed in their catalog of courses.

As with pretty much all codecademy material there is a lot of hand holding and help along the way. They don’t go too deep but it’s a good introduction at least. I am taking this as a warm-up for the FCC api challenges. Maybe this can be helpful for you too.