How Does an API Gateway Work? (13 Core Functions Explained)

Listen to this Post

Featured Image
An API Gateway acts as the central entry point for all client requests in a microservices architecture. Below is a deep dive into its core functionalities, along with practical commands and configurations.

13 Core Functions of an API Gateway

1️⃣ Entry Point

  • Receives all external requests, preventing direct access to internal services.
  • Example (NGINX as API Gateway):
    server {
    listen 80;
    server_name api.example.com;
    location / {
    proxy_pass http://backend_services;
    }
    }
    

2️⃣ Rate Limiting

  • Limits request frequency per client to prevent abuse.
  • Example (Kong API Gateway):
    curl -X POST http://localhost:8001/services/{service}/plugins \
    --data "name=rate-limiting" \
    --data "config.minute=100"
    

3️⃣ Load Balancing

  • Distributes traffic across multiple backend instances.
  • Example (HAProxy):
    backend api_servers
    balance roundrobin
    server api1 192.168.1.1:8000
    server api2 192.168.1.2:8000
    

4️⃣ Routing

  • Directs requests to the appropriate microservice.
  • Example (AWS API Gateway):
    {
    "httpMethod": "GET",
    "path": "/users",
    "integration": "lambda",
    "lambdaFunction": "getUsers"
    }
    

5️⃣ Authentication & Authorization

  • Validates API keys, JWT tokens, or OAuth.
  • Example (Kong JWT Plugin):
    curl -X POST http://localhost:8001/services/{service}/plugins \
    --data "name=jwt"
    

6️⃣ Request/Response Transformations

  • Modifies payloads before forwarding.
  • Example (Apigee Policy):
    <AssignMessage>
    <Set>
    <Payload contentType="application/json">{"new_key":"value"}</Payload>
    </Set>
    </AssignMessage>
    

7️⃣ Caching

  • Stores frequent responses to reduce latency.
  • Example (Redis + API Gateway):
    curl -X POST http://localhost:8001/services/{service}/plugins \
    --data "name=proxy-cache" \
    --data "config.strategy=memory"
    

8️⃣ Versioning

  • Supports multiple API versions simultaneously.
  • Example (URL-based versioning in NGINX):
    location /v1/users {
    proxy_pass http://user_service_v1;
    }
    location /v2/users {
    proxy_pass http://user_service_v2;
    }
    

9️⃣ Observability & Logging

  • Tracks API performance metrics.
  • Example (Prometheus + Grafana for monitoring):
    prometheus.yml
    scrape_configs:</li>
    <li>job_name: 'api_gateway'
    static_configs:</li>
    <li>targets: ['api_gateway:9090']
    

🔟 Security Policies

  • Blocks DDoS, SQLi, and other threats.
  • Example (ModSecurity WAF rules):
    SecRuleEngine On
    SecRule ARGS "@detectSQLi" "id:1,log,deny,status:403"
    

1️⃣1️⃣ Response Aggregation

  • Combines data from multiple microservices.
  • Example (GraphQL with Apollo Gateway):
    const gateway = new ApolloGateway({
    serviceList: [
    { name: 'users', url: 'http://users-service' },
    { name: 'orders', url: 'http://orders-service' }
    ]
    });
    

1️⃣2️⃣ Usage Metrics

  • Tracks API calls, errors, and latency.
  • Example (ELK Stack for logging):
    filebeat.prospectors:</li>
    <li>type: log
    paths: ["/var/log/api-gateway/.log"]
    

1️⃣3️⃣ Error Handling

  • Standardizes error responses.
  • Example (Custom error responses in AWS API Gateway):
    {
    "error": "Not Found",
    "statusCode": 404,
    "message": "Resource not available"
    }
    

You Should Know: Practical API Gateway Commands

Kong API Gateway Setup

docker run -d --name kong \
-e "KONG_DATABASE=postgres" \
-e "KONG_PG_HOST=postgres" \
-p 8000:8000 \
-p 8443:8443 \
kong:latest

Enabling JWT Authentication

curl -X POST http://localhost:8001/consumers \
--data "username=api_user"
curl -X POST http://localhost:8001/consumers/api_user/jwt \
-H "Content-Type: application/json"

Rate Limiting with NGINX

limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s;
server {
location /api/ {
limit_req zone=api_limit burst=20;
proxy_pass http://backend;
}
}

AWS API Gateway Deployment

aws apigateway create-deployment \
--rest-api-id abc123 \
--stage-name prod

What Undercode Say

API Gateways are essential for managing microservices traffic efficiently. They improve security, reduce latency, and simplify API versioning. Proper configuration with tools like Kong, NGINX, or AWS API Gateway ensures scalability and reliability.

Prediction

As microservices grow, AI-driven API Gateways will automate traffic optimization, threat detection, and self-healing routing.

Expected Output:

A fully configured API Gateway handling:

✔ Rate limiting

✔ Authentication

✔ Load balancing

✔ Logging & Monitoring

✔ Request/Response transformations

For further reading:

References:

Reported By: Ninadurann How – Hackers Feeds
Extra Hub: Undercode MoN
Basic Verification: Pass ✅

Join Our Cyber World:

💬 Whatsapp | 💬 Telegram