Skip to main content

🍽️ APIs

A waiter between you and the kitchen

The Restaurant Waiter Analogy

At a restaurant:

  • You (customer) want food
  • Kitchen has food
  • Waiter takes your order to kitchen
  • Waiter brings food back to you

You don't go into the kitchen. The waiter is the interface.

API is a waiter for software. It takes requests, gets data from somewhere, and brings back responses.


What Is an API?

API = Application Programming Interface

A way for software to talk to other software.

Your app → API → Another system's data/functionality
                 ↓
              Response back to your app

Why "Interface"?

Interface = standardized way to interact.

TV remote: Standard buttons (power, volume, channel)
USB port: Standard connection
API: Standard ways to request and receive data

You don't need to know how it works inside.

Types of APIs

Web APIs (Most Common)

Over the internet. HTTP-based.

Your app → HTTP request → Server
Your app ← HTTP response ← Server

REST, GraphQL, SOAP

Library APIs

In your code. Function calls.

import pandas
df = pandas.read_csv("data.csv")

pandas provides an API for data manipulation.

Operating System APIs

Your code talks to the OS.

file.open("document.txt")

OS provides file system API.

Hardware APIs

Software talks to hardware.

camera.capture()

Camera provides an API for taking photos.

How Web APIs Work

Request

Client sends request:
  - URL: Where to send it
  - Method: What to do (GET, POST, etc.)
  - Headers: Metadata (auth, content type)
  - Body: Data (for POST, PUT)

Example:
  GET https://api.weather.com/current?city=Sydney
  Authorization: <credentials>

Response

Server sends response:
  - Status code: Did it work?
  - Headers: Metadata
  - Body: The actual data

Example:
  200 OK
  { "temperature": 25, "condition": "sunny" }

Common Use Cases

Integration

Connect different systems.

Your e-commerce site → Stripe API → Payment processing
Your app → Google Maps API → Maps and directions
Your site → SendGrid API → Email sending

Data Access

Get data from external sources.

Twitter API → Tweets
Weather API → Forecasts
Stock API → Prices

Functionality

Use features you didn't build.

Twilio API → Send SMS
OpenAI API → AI completions
AWS S3 API → Store files

API Examples in Real Life

Travel booking site:
  → Airline APIs (flight availability)
  → Hotel APIs (room availability)
  → Car rental APIs (car availability)
  → Payment APIs (process payment)

One website, many APIs behind the scenes!

API Styles

Uses HTTP methods and URLs.
Stateless.
Resources-focused.

GET /users/123
POST /users
DELETE /users/123

GraphQL

Query language.
Ask for exactly what you need.
Single endpoint.

{ user(id: 123) { name, email } }

SOAP

XML-based. Enterprise.
Strict contracts (WSDL).
Older, but still used in banking/enterprise.

WebSocket

Real-time, bidirectional.
Server can push to client.
Chat, live updates.

Authentication

API Keys

Simple. One key per app.

Authorization: ApiKey your-key-here

Easy to implement. Share carefully.

OAuth

User authorizes your app.
App gets token to act on user's behalf.

"Log in using Google"

JWT

Token contains user info.
Signed. Self-contained.

Authorization: <token>

Rate Limiting

APIs limit how often you can call.

100 requests per minute.
Exceed? 429 Too Many Requests.

Be a good API citizen!

API Documentation

Good API documentation includes:
  - Available endpoints
  - Request format
  - Response format
  - Authentication method
  - Error codes
  - Examples

OpenAPI/Swagger is common standard.

Building vs Consuming

Consuming APIs

You use someone else's API.

Read the docs.
Get credentials.
Make requests.
Handle responses.

Building APIs

You create an API for others.

Design endpoints.
Implement logic.
Document it.
Handle errors gracefully.

Practical Tips

As API Consumer

✓ Handle errors gracefully
✓ Respect rate limits
✓ Cache responses when appropriate
✓ Keep credentials secret
✓ Read the documentation

As API Builder

✓ Use consistent naming
✓ Return helpful error messages
✓ Version your API
✓ Document everything
✓ Validate input

Common Mistakes

1. Ignoring Errors

API returns error → App crashes.

Make sure your app handles error responses.

2. Hardcoding URLs/Keys

API key in source code → Security risk!

Use environment variables.

3. Not Reading Docs

"Why doesn't this work?"

Read. The. Documentation.

4. Ignoring Rate Limits

Hammering API → Get blocked.

Implement backoff and respect limits.

FAQ

Q: API vs website?

Websites are for humans (HTML). APIs are for programs (JSON/XML).

Q: How do I find APIs?

RapidAPI, ProgrammableWeb, or check if the service you need has public API.

Q: Are APIs free?

Some free, some freemium, some paid. Check pricing before building on one.

Q: Can I build my own?

Yes! Any backend can expose an API. It's just HTTP endpoints.


Summary

APIs let software communicate with other software through standardized interfaces.

Key Takeaways:

  • API = standardized way for software to talk
  • Web APIs use HTTP (request/response)
  • Common styles: REST, GraphQL, WebSocket
  • Authentication: API keys, OAuth, JWT
  • Respect rate limits
  • Read documentation!

APIs are the glue connecting modern software!

Related Concepts

Leave a Comment

Comments (0)

Be the first to comment on this concept.

Comments are approved automatically.