Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Application Programming Interfaces (APIS)

Goal

  • Understand what APIs are and how they work.
  • HTTP in Depth
  • Calling APIs
  • Reading API documentation
  • Basic API Authentication
  • Server to server communication
  • JSON vs. XML, sending data over the wire.

What are APIs

  • Application Programming Interface (API)
    • is a set of commands, functions, protocols, and objects that programmers can use to create software or interact with an external system
    • it provides developers with standard commands for performing commond operations so they do not have write the code from scratch
  • APIs to Create Software
    • Apple API to interact to IOS operating system
    • Google API to interact to Android operating system
  • APIs to Interact with External System
    • tinder app section for shared friends and interests information came from facebook when you sign in using it. Tinder will have you facebook login details and able to go to facebook servers and interact to bring all this information back to the tinder app
  • cURL is a way of making an HTTP request through command line.
    • a "Client for URLs", or a way to interact with URL on CLI

Using the Request Module to Get Data from an API

  • ? everything that comes after it is a parameter or an input
  • 2 tiers of API
    • API accessible to the public
    • API only accessible internally (private)
  • 200 status code is request success and got a response from the server we are requesting
  • use the request module to make get request to another server.

Understanding the JSON Format and Working with JSON

  • JSON is a data interchange format
    • it makes it easier for servers to talk to each other on the internet and get data in a standardized format
    • is JavaScript Object Notation
    • data is formatted in a key value pair
      • key is similar to the variable name
      • each key value pair store a piece of data
  • XML eXtensible Markup Language
    • pretty similar to HTML where key is the name of the tag, and the value is the data contained between open and closing tags
    • longer and hard to read
  • JSON.stringify(js object) method to converts a JavaScript object to a JSON string
  • JSON.parse(string) method parses a JSON string, constructing the JavaScript value or object described by the string.
  • res.send() is the last thing you'd send to the browser, if you want to send multiple things, you have to use res.write() first then call res.send() at the end.

API Calls with Parameters

  • request module options
    • uri || url - fully qualified uri or a parsed url object from url.parse()
    • method - http method (default: "GET")
    • qs object containing querystring values to be appended to the uri
    • headers - http headers (default: {})
    • body - entity body for PATCH, POST and PUT requests. Must be a Buffer, String or ReadStream. If json is true, then body must be a JSON-serializable object.

Setting Up the Newsletter Page

  • use static which is a special function of express to serve a static files on the server

    • inside the parenthesis add the name of your static folder
    app.use(express.static('public'));

Heroku & Mailchimp

Resources