Cloud & Infrastructure Security

API (Application Programming Interface)

About 5 min read

What Is an API

An API (Application Programming Interface) is a set of rules that allows software applications to communicate and exchange data. Think of a restaurant: the customer (application) reads the menu (API specification), places an order, and the waiter (API) relays it to the kitchen (server) and brings back the dish (data).

Your weather app displays current conditions by fetching data through a weather service API. Some web services combine multiple APIs behind the scenes.

Main Types of APIs

REST API
An API design style. Uses HTTP methods (GET, POST, PUT, DELETE) and URLs to identify resources. Simple and well-understood.
GraphQL
A query language developed by Facebook. Clients specify exactly what data they need, reducing over-fetching that can occur with REST. Advantageous for mobile apps with limited bandwidth.
WebSocket
Enables bidirectional real-time communication between server and client. Used for chat apps, live stock prices, and online games.
gRPC
A high-performance RPC framework by Google using Protocol Buffers for binary serialization (described in the Protocol Buffers documentation as smaller and faster than JSON). Used for internal microservice communication, among other purposes.

API Authentication and Authorization

Secure API usage requires both authentication (who is accessing) and authorization (what they can do).

  • API Keys: A simple method. A fixed string included in request headers. Easy but risky if leaked.
  • OAuth 2.0: An authorization framework (RFC 6749) that lets a third-party application obtain limited access to an HTTP service without sharing passwords directly. Identity checks such as "Sign in with Google" are handled by OpenID Connect, an identity layer on top of OAuth 2.0.
  • JWT (JSON Web Token): Embeds authentication data in a token that can be verified without server-side sessions. Works well with stateless API designs.

Hardcoding API keys in source code or committing them to public repositories can lead to leaks. Use secret management tools and environment variables.

Rate Limiting and API Protection

Rate limiting caps the number of API requests accepted within a time window. Without it, attackers can overwhelm servers with DDoS attacks.

  • Fixed Window: Limits requests per fixed time period (e.g., 100 per minute). Simple but allows bursts at window boundaries.
  • Sliding Window: Continuously tracks requests over the last time period, reducing boundary bursts.
  • Token Bucket: Tokens replenish at a steady rate and are consumed per request. Allows short bursts while controlling long-term averages.

When rate limited, servers return HTTP 429 (Too Many Requests). API consumers should implement exponential backoff retry logic.

API Security in Practice

APIs are building blocks of web services and are also targeted by attackers. The 2023 edition of the OWASP API Security Top 10 ranks broken object level authorization, broken authentication, and broken object property level authorization (which includes the 2019 category of excessive data exposure) as the top three risks.

  • Input Validation: Validate all request parameters to reduce the risk of SQL injection and XSS.
  • HTTPS Enforcement: Always encrypt API traffic with TLS. Sending API keys over plain HTTP exposes them to interception.
  • Least Privilege: Grant API tokens only the minimum required permissions.
  • Logging and Monitoring: Monitor API call patterns to detect anomalous access early.

Common Misconceptions

APIs are only relevant to developers
APIs power smartphone apps, IoT devices, payment systems, and more. Logging into social media, paying with digital wallets, and using ride-sharing apps also involve APIs behind the scenes.
Private APIs are automatically secure
Internal APIs can be exploited if the network is compromised or through supply chain attacks. Zero-trust principles - authentication, authorization, and encryption for internal APIs - are essential.
REST is outdated and being replaced by GraphQL
GraphQL excels in specific use cases, but REST is not inferior in all scenarios. For simple CRUD operations and caching, REST is often more appropriate. Both coexist.
Share

Related Terms

Related Articles