Communicating with External APIs

Lecture Goals

  • Explain what an API is
  • Explain the limitations of working with an external API
  • Observe how to parse API documentation
  • Observe how to send a GET request to an external API with / without an API key

The Restaurant Analogy

Restaurant Analogy for an API

https://medium.com/vody-techblog/understanding-what-apis-are-all-about-ff2513b76a55

Downsides of External APIs

Out of control

The main upside!

I know Kung Fu

Examples

Google Books API Checklist (part 1) - Creating your Project and API key

Google Books API Checklist (part 2) - Adding the API you want to use

  • Click the sidebar option for: Enable APIs and Services
  • In the search input at the top, type: Books API and select it from the Marketplace search results
  • Click the blue enable button to allow your API key to access the Books API

Google Books API Checklist (part 3) - Creating your API key

https://securityintelligence.com/articles/passwordless-authentication-risks-benefits/

  • Navigate to the Credentials page for your project and click on your API key
  • Click the clipboard button to copy the API key.
  • add the API key to a local file in your project. (const API_KEY="paste_your_key_here")
  • Make sure to tell git to ignore this file!

.gitignore

  • add a file in the root of your project called .gitignore (this should be inside of the 07_ folder)
  • this file will contain a list of files & folders for which git will not track changes
  • in our case, add src/keys.js on its own line within this file.
  • ✅⁇: check your version control tab within VSCode and make sure you don’t see your keys.js file there. If you do, then check for a mismatch between what you have in your .gitignore and the name/path to the keys.js file.
  • ✅ When keys.js doesn’t appear in the list of files with untracked changes, you’ve done this step correctly.

Using the API

anatomy of an API URL

View docs for more details.

fetch(`https://www.googleapis.com/books/v1/volumes?q=${encodeURI(query)}&key=${API_KEY}`)
  .then(res =>res.json())
  .then(console.log);