APIs

ProvoJS - July 2015

API:

Application

Programming

Interface

Interfaces

$wamp.subscribe('com.myapp.hello', eventHandler);
GET https://api.spotify.com/v1/search?q=david+bowie&type=artist

GUI

Code

Web API

HTTP

Hypertext Transfer Protocol

A client sends a request to a server.

The server sends a response back.

Your web browser:

The HTTP Request:

GET / HTTP/1.1
Host: provojs.org
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8,th;q=0.6

The HTTP Response:

HTTP/1.1 200 OK
Server: nginx/1.4.6 (Ubuntu)
Date: Tue, 14 Jul 2015 06:42:32 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: Express
Content-Encoding: gzip

<!doctype html>
<html>
  <head>
    <title>ProvoJS</title>
  </head>
  <body>
    <h1>ProvoJS</h1>
    <p><a href="/slack">Join the Slack community</a></p>
    <p><a href="http://meetup.com/provojs">Meetup.com</a></p>
    <p><a href="http://github.com/provojs">Github.com</a></p>
  </body>
</html>

A web API is a URL that returns data instead of documents.

Calling the Spotify API

HTTP/1.1 200 OK
Server: nginx
Date: Tue, 14 Jul 2015 07:07:37 GMT
Content-Type: application/json; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive

{
  "artists" : {
    "href" : "https://api.spotify.com/v1/search?query=david+bowie&offset=0&limit=20&type=artist",
    "items" : [ {
      "external_urls" : {
        "spotify" : "https://open.spotify.com/artist/0oSGxfWSnnOXhD2fKuz2Gy"
      },
      "followers" : {
        "href" : null,
        "total" : 803332
      },
      "genres" : [ "art rock", "glam rock", "permanent wave" ],
      "href" : "https://api.spotify.com/v1/artists/0oSGxfWSnnOXhD2fKuz2Gy",
      "id" : "0oSGxfWSnnOXhD2fKuz2Gy",
      "images" : [ {
        "height" : 1369,
        "url" : "https://i.scdn.co/image/8067482fe4a138a90caa1ebf6f9d4e4dc44e9885",
        "width" : 1000
      }, {
        "height" : 876,
        "url" : "https://i.scdn.co/image/8346e5f95cf3bd27501e6cd18293846b811d80df",
        "width" : 640
      } ],
      "name" : "David Bowie",
      "popularity" : 81,
      "type" : "artist",
      "uri" : "spotify:artist:0oSGxfWSnnOXhD2fKuz2Gy"
    } ],
    "limit" : 20,
    "next" : "https://api.spotify.com/v1/search?query=david+bowie&offset=20&limit=20&type=artist",
    "offset" : 0,
    "previous" : null,
    "total" : 22
  }
}
GET https://api.spotify.com/v1/search?q=david+bowie&type=artist

Everyday APIs

  • Twitter
  • Facebook
  • Spotify
  • Gmail
  • Google Search
  • Google Maps
  • Slack
  • YouTube

Awesome APIs

  • Star Wars: https://swapi.co/
  • Pokemon: http://pokeapi.co/
  • Memes: https://api.imgflip.com/
  • Reaction GIFs: https://api.giphy.com/
  • Deck of Cards: http://deckofcardsapi.com/

Find cool APIs at

https://www.mashape.com/

Making our own API

https://github.com/ProvoJS/node-api-server

A Simple NodeJS API Server

// Set up the server, using the express module.
var express = require('express');
var app = express();

// Define an API endpoint:  GET /info
app.get('/info', function (request, response) {
    //This function gets run every time a user visits the '/info' page.

    //Send this data back to the user.
    response.json({
        meetup: 'ProvoJS',
        awesomeness: '100%'
    });
});

// Start listening for incoming connections on port 4000.
var server = app.listen(4000, function() {
    console.log('API Server listening at http://localhost:4000/');
});

Run the server

git clone https://github.com/ProvoJS/node-api-server.git

cd node-api-server/

npm install

node example.js

# Now visit http://localhost:4000/info in your web browser!

API Server Project

Run the server:

npm start

 

Finish implementing the API:

api-server.js
database.js

APIs - ProvoJS, July 2015

By Andrew Jensen

APIs - ProvoJS, July 2015

  • 1,348