3. Objectives
• Appreciate the benefits of leveraging public
APIs
• Understand basics of calling a REST APIs
• Can make payments using the PayPal API
• Understand the benefits of Asynchronous
programming
• Create a mashup of PayPal APIs leverging the
Async library
4. What is a REST API?
• REpresentational State Transfer
• Context of Web Services
• URLs (e.g:
https://api.github.com/users/aeshanw)
• Interface to Resources over the Internet
Credit: http://www.robert-drummond.com/
6. Being RESTful
• APIs are Resource-centric
• REST is built-atop the HTTP protocol
• HTTP is the infrastructure of the Internet
• REST APIs (a.k.a web services) support
– GET
– POST
– PUT
– DELETE
8. Calling a simple API
• Fetch github user details
• https://api.github.com/users/aeshanw
• Response can be viewed on a web browser
• This is a GET operation
• What about POST/PUT/DELETE?
– We need a more advanced tool to work with REST
APIs
9. Introducing POSTMAN
• Tool for working with REST APIs
• As a developer you can
– Customize the HTTP Headers of request
– Authenticate your requests to protected APIs
• Supports Oauth for this purpose
10. Using POSTMAN
• Import the POSTMAN collection:
– https://www.getpostman.com/collections/b0038e
d98853ba7e1b10
• Call the github URI with POSTMAN
• Note the status code and response headers
11. PayPal APIs
• A huge collection of APIs handling
– Payments
– Invoicing
– Billing Agreements
• Our focus will be on
– Storing a credit card on the Vault API
– Making a payment with the Vaulted card
12. PayPal Secured by OAuth
• Similar to log-in with username & password
• You need a PayPal account (personal)
• Authenticate via v1/oauth2/token API
• Get your clientID & secret
• POSTMAN Authorization tab
– Select `Basic Auth` Type
– Username clientID
– Password secret
14. Add credit card
• Using /v1/vault/credit-cards
• Request Body is in a JSON-format
• Request Headers include the AccessToken
15. credit cards for testing
• PayPal sandbox is for testing only
• Allows use of dummy cards for testing
• Use this site for generating credit card
numbers for testing
• http://www.freeformatter.com/credit-card-
number-generator-validator.html
16. Pay with a Vaulted card
v1/oauth2/token
v1/vault/credit-cards
v1/payments/payment
AccessToken
CardToken
PaymentID
17. Try this in POSTMAN
• Use the POSTMAN collection provided earlier
• Use POSTMAN global variables to store the
AccessToken to be used in subsequent
requests
• Exercise
– Get Token (Authenticate)
– Add a card to vault
– Make a payment with the vaulted card
18. Getting started with Node.js
• Let’s make the calls from our application
• Using node.js as an example, but concepts
valid in any language.
• Exercise : Run the node.js app locally
– $ npm install
– $ npm start
– Goto http://localhost:8000/
19. API calls from node.js
• Use the request module as REST
Clienthttps://www.npmjs.com/package/reque
st
• Look for Lab 1.0a in
demoapp/controllers/index.js
20. Optimization
• What if you need to add several cards at
once?
addCard1 payWithCard1 addCard2 payWithCard2
• Is there a faster way?
• Yes, Parallelization
23. Asynchronous Complexity
• Many languages support Asynchronous
libraries. E.g: Python, Java, Javascript, Scala,
Erlang…
• But, Developing code that is Asynchronous
can be difficult.
• Thankfully most modern languages provide
libraries to manage the complexity
• Async is popular among Javascript developers
24. Using Async
• Docs here
https://www.npmjs.com/package/async
• Async can execute your code
– In series
– In parallel
25. Async series
• Call the APIs in sequence one after the other
• Run nodeserv dummy service to better
understand the difference
– $ cd nodeserv
– $ npm install
– $ node index.js
• Look for Lab 1.1a in
demoapp/controllers/index.js
26. Async series workshop
• Nodeserv has a 5 sec time-delay to simulate
I/O latency
• How long would this lab code take to call the
task API twice?
27. Async parallel
• Call the APIs in parallel
• Look for Lab 1.1b in
demoapp/controllers/index.js
• Did you notice any difference in the time
taken to call the API twice?
28. PayPal Integration
• Let’s integrate the vaulted-card solution into
our node.js application
• First, we need to get the access_token
• Look for Lab 2.0 in
demoapp/controllers/index.js
• Test the api-call by hitting
localhost:8000/gettoken
• Ensure you get the access_token
29. Vault the card
• Pass the access_token to a funtion which adds
the card to vault
• Look for Lab 2.1 in
demoapp/controllers/index.js
• Ensure you get the card_token from the vault
API response
• Test the api-call by hitting
localhost:8000/addcard
30. Make Payment
• Use case: We need to split a big payment into
2 credit cards due to some credit-limits on
each card
• Parallelize the vaulting of 2 cards
• Parallelize the 2 payments using the card
tokens
• Look for Lab 2.2 in
demoapp/controllers/index.js
31. Fix the code
• Fix the payment api call in payWithToken
• Parallelize the functions addCardToVault &
payWithToken in order to complete the tasks
as efficiently as possible.
• Test the calls by hitting
localhost:8000/paywithvault
– Check the logs to verify the result
32. Bonus lab (optional)
• Notice that the Card Tokens are logged to
console
• Can we lookup the Vaulted Card details?
• Lookup the PayPal Vault API to do this
33. Recap
• Appreciate the use of APIs in applications
• Call a RESTful API using POSTMAN
• Call a RESTful API from an application
• Debug API Integrations using logs
• Parallelize API calls with Async libraries
34. Beyond Javascript
• Many languages support Async
– Python
– Java
So what’s stopping you from using it in your
next application?