Introduction to APIs

Introduction to APIs in Python

Chris Ramakers

Engineering Manager

What is an API?

  • Application Programming Interface
  • Set of communication rules and abilities
  • Enables interactions between software applications

A diagram showing API interactions between an email client and an email server for sending an email to john@acme.com.

Introduction to APIs in Python

Web APIs, clients and servers

  • Web APIs communicate over the internet using HTTP
  • Client sends a request message to a Server
  • Server returns a response message to the Client

A diagram showing client-server interaction over the internet via an API, with labeled request and response messages.

  • Request/Response cycle
Introduction to APIs in Python

Types of Web APIs

  • SOAP
    • Focus on strict and formal API design
    • Enterprise applications
  • REST
    • Focus on simplicity & scalability
    • Most common API architecture
  • GraphQL
    • Focus on flexibility
    • Optimized for performance
1 https://www.postman.com/state-of-api/api-technologies/#api-technologies
Introduction to APIs in Python

Working with APIs in Python

urllib

  • Bundled with Python
  • Powerful but not very developer-friendly
from urllib.request import urlopen
api = "http://api.music-catalog.com/"

with urlopen(api) as response:
  data = response.read()
  string = data.decode()
  print(string)

requests

  • Many powerful built-in features
  • Easier to use
import requests
api = "http://api.music-catalog.com/"

response = requests.get(api)
print(response.text)
Introduction to APIs in Python

Let's code!

Introduction to APIs in Python

Preparing Video For Download...