Monthly Archives: June 2015

HacksHackers Manchester – Web APIs for lunch

Ziferblat
HacksHackersMcr tried a different format on 14th May – a Learning Lunch. Thanks to Rob for the idea and for leading the meet-up! And thanks to Ziferblat in the Northern Quarter for being the perfect venue.

Web APIs for journalists and… everyone

Application Programming Interface may not sound like funzies for a non-coding journalist but getting your head round them opens up all sorts of possibilities. Obviously, if you’re a dev, you’re going to go in a lot deeper and do some amazing things but that doesn’t mean there isn’t an entry level for journalists too.

An interface just means you have a way of getting at something you want. Let’s say you want to get photos posted on Flickr from a certain location or using a certain camera. You could do it the hard, laborious way…. Or you could use the Flickr API to make it simpler and quicker (relatively speaking). You just construct a specific type of URL in your ordinary browser, then send it to a server which interprets that URL request and sends you a bunch of the data you asked for. BUT, it will send it in computer-speak so it won’t look pretty. The data will probably be in JSON but you don’t need to be fluent in the JSON format to be able to use this data.

Flickr Web APIs – the basics

So, I’m no expert and I’m just playing around with this stuff at this stage but that means I can, at least, see the potential of APIs. I chose the Flickr API. Click on this link and on the right hand side, you’ll see a list of “methods” which just means the different types of data you might want to get at. When you click on one, it gives you further instructions about how to create your API. For a lot of them, you need to get authentication which I’m not going to go into here. So let’s choose a simple example which doesn’t require authentication. Under the People subheading, I clicked on getPublicphotos – which lets you get the data for the public photos of a particular Flickr user.

Now you’re ready to build your URL. Flickr APIs start with:-

https://api.flickr.com/services/rest/?

Next, you need to specify which Method from that long list you’d like to access. So add this:-

&method=

What’s the Method? It tells you at the top of the page of instructions once you click on your chosen method. In this case it’s:-

flickr.people.getPublicPhotos

Now, the instructions tell you there are various “arguments” you need to add to your URL. These are just further details to help the server return what you need. First of all, you need the API key and there are instructions on the page for how to get this password. It’s easy. So you add this to that URL you’re building:-

&api_key=APIKeyGoesHere

Then it asks you to add the user id (NSID) for the user whose photos you’re looking for. This is trickier (although maybe I’m missing a simple trick here?) but this is how I ended up getting the NSID for a user. It involves another API, ironically, but it’s a copy&paste job into your browser

https://api.flickr.com/services/rest/?&method=flickr.people.findByUsername&api_key=APIKeyGoesHere&username=UserNameGoesHere

That should return the NSID number so add that to your growing URL like this:-

&user_id=NSIDCodeGoesHere

Finally, let’s tell the server we’d like our data returned to us in JSON:-

&format=json

Put that all together and you get something like this:-

https://api.flickr.com/services/rest/?&method=flickr.people.getPublicPhotos&api_key=APIKeyGoesHere&user_id=NSIDCodeGoesHere&format=json

Copy and paste into your browser and see what you get!

Screenshot JSON API request

Not so nice. But you can use a JSON to CSV converter to change this into a more familiar spreadsheet format by simply copying and pasting the text as it is into, say, http://konklone.io/json/ 

Not hugely useful to a journalist but hopefully a way to understand how an API works and what it can do. So the next stage is to think of some more practical – yet still accessible – project I could work on with an API. Suggestions welcome!

Recommended Articles

Rob’s slides from the HacksHackersMcr Learning Lunch

Web APIs – Learning Lunch – Rob’s blog

Web APIs for non-programmers – a fabulous explainer by Noah Veltman