Sub topic 2
HTTP API or REST API: choosing between them
This chapter assumes you know basic HTTP, can read a Lambda handler, and have seen the idea of routes, integrations and stages from the overview chapter. Here you will compare the two API types that Amazon API Gateway offers for web requests, so you can pick one before you build.
Two products, one service
API Gateway offers more than one API type. Two of them serve ordinary request and response traffic:
- HTTP API is the newer, lighter type. It is designed for low latency and low cost, and it is the type this tutorial uses.
- REST API is the older type. It has a larger feature set, including some features that HTTP APIs do not have.
Both can invoke Lambda functions and both support stages, authorizers and throttling. They differ in feature depth, price, and the shape of the event your function receives.
How they differ
| Area | HTTP API | REST API |
|---|---|---|
| Lambda event format | Payload format 2.0 (1.0 also allowed) | Payload format 1.0 |
| Route definition | Route keys such as `GET /items/{id}` and `$default` | Resources and methods |
| Authorizers | JWT, Lambda, IAM | Cognito user pools, Lambda, IAM |
| CORS | Built-in configuration on the API | Configured per resource, often through mock integrations |
| Request validation and transformation | Not available | Models, validators, mapping templates |
| API keys and usage plans | Not available | Available |
| Caching | Not available | Built-in cache |
| AWS WAF and private endpoints | Not available | Available |
| Price per request | Lower | Higher |
Check the current AWS documentation before you commit. Feature gaps between the two types have changed over time.
Choosing
Start from your requirements, not from habit.
- List the features you need that only a REST API provides, such as API keys with usage plans, response caching, request validation by the gateway, or AWS WAF.
- If any of them is required, use a REST API.
- If none is, use an HTTP API. You get lower cost, lower latency, and simpler CORS and JWT setup.
What this means for your handler
The event shape is the practical difference in code. In a version 2.0 event, the route is at event["requestContext"]["routeKey"] and path parameters are in event["pathParameters"]. A REST API event uses version 1.0, which has fields such as resource and httpMethod at the top level. A handler written for one format will fail on the other, often with a KeyError.
Exercise
A team needs per-customer API keys with usage quotas and wants the lowest possible cost per request. Which API type should they choose, and what is the trade-off?
Show answer
They should choose a REST API, because API keys with usage plans are not available on HTTP APIs. The trade-off is a higher price per request and a more complex setup. If quotas could be enforced inside the backend instead, an HTTP API would be cheaper.