API Examples: Real-World Use Cases & Code Snippets

Written by

in

An API (Application Programming Interface) is the contract that lets two pieces of software talk without needing to know how each other is built under the hood. Every time you check live weather on your phone, pay via UPI or a card gateway, sign into an app with Google, or watch a delivery rider move across a map, APIs are doing the heavy lifting in the background.


Everyday API Examples You Already Use

Most people interact with dozens of APIs before noon without noticing:

  • Weather & Air Quality APIs: Weather apps and smart dashboards send a location query to a service like OpenWeatherMap or WeatherAPI and receive structured JSON with temperature, humidity, and forecast conditions.
  • Payment Gateways: When you buy something online, your store’s backend sends a payment request to a processor such as Stripe or Razorpay rather than handling raw card or bank credentials directly. The gateway authorises the transaction and returns a confirmation status.
  • Maps & Routing APIs: Ride-hailing and food-delivery apps pass pickup and drop coordinates to mapping APIs to calculate distance, estimated travel time, and turn-by-turn routes.
  • Authentication (OAuth) APIs: Tapping “Continue with Google” lets a third-party app verify your identity through Google’s auth server and receive a scoped access token—without ever seeing your password.
  • AI & Media APIs: Apps that summarise notes, generate images, or transcribe audio often send text or media payloads to a model endpoint and display the generated output in their own interface.

API Examples by Architectural Style

Calling an API is only half the story; how you structure requests and receive updates depends on the pattern.

Style How It Works Concrete Example Best Fit
REST Uses standard URLs and HTTP methods (GET, POST, PUT, DELETE), usually returning JSON. Fetching weather data or creating a payment charge over HTTPS. Predictable resources, CRUD apps, and HTTP caching.
GraphQL Client sends a single query specifying the exact fields needed across nested data. Querying a user profile and only their latest five repositories on GitHub. Mobile apps, dashboards, and avoiding over-fetching.
Webhooks Server pushes an HTTP event to your registered URL when something happens. Receiving a payment-succeeded or payment-failed event from a billing provider. Event-driven workflows without constant polling.
WebSockets Keeps a persistent two-way connection open so either side can push messages anytime. Streaming live driver location updates on a map during an active trip. Real-time feeds, chat, and live tracking.

What a Real Request and Response Looks Like

At its simplest, a REST API call is just a structured message sent over the web and a structured reply sent back.

Request (asking for current weather in Bengaluru in metric units):

GET /data/2.5/weather?q=Bengaluru&units=metric&appid=YOUR_API_KEY HTTP/1.1
Host: api.openweathermap.org

Response (JSON payload returned by the server):

{
  "name": "Bengaluru",
  "main": {
    "temp": 24.5,
    "humidity": 68
  },
  "weather": [
    {
      "description": "scattered clouds"
    }
  ]
}

Your app doesn’t need to know how the weather station collected the readings or which database stored them—it just reads main.temp and renders 24.5°C on screen.


What Separates Toy APIs from Reliable Ones

Seeing a working 200 OK response in a tutorial is easy; keeping integrations reliable in production takes a few habits:

  • Idempotency: Network retries happen. Payment and order-creation endpoints should safely ignore duplicate requests carrying the same idempotency key so a user isn’t charged twice.
  • Graceful failure handling: External APIs time out, rate-limit traffic, or return 5xx errors. Apps need clear fallbacks—cached data, retry backoff, or honest status messages—instead of spinning forever.
  • Least-privilege keys and webhook verification: Keep secret API keys on the server, rotate credentials regularly, and verify cryptographic signatures on incoming webhooks so spoofed payloads can’t trigger actions.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *