# Amazon API Gateway

> The front door of your API: it receives requests and passes them to your code.

- Level: Intermediate
- Topics: AWS
- URL: https://tidbit.legios.com.ar/amazon-api-gateway/
- In Spanish: [Amazon API Gateway](https://tidbit.legios.com.ar/es/amazon-api-gateway/)

**Amazon API Gateway** gives your API a public address on the internet and takes care of everything that happens before your code runs. You define routes like `GET /orders` or `POST /users`, tell it where each one goes (usually an AWS Lambda function, sometimes a server or another AWS service), and it handles the rest.

'The rest' is a lot: serving over HTTPS, checking API keys or login tokens (it works directly with Amazon Cognito), limiting how many requests per second each client can make, caching responses, logging every call to Amazon CloudWatch, and even keeping WebSocket connections open and turning their messages into events for your code.

Example: a mobile app calls `https://api.example.com/orders`. API Gateway receives the request, verifies the user's token, rejects it if that client already exceeded its quota, and otherwise invokes a Lambda function with the request body. The function returns JSON, and the gateway sends it back to the phone. No server was ever provisioned.

Together with Lambda and Amazon DynamoDB it is the classic recipe for a serverless backend: you pay per request, and it scales from zero to millions of calls without touching anything.

## An analogy

The receptionist of an office building. Visitors do not walk straight to a desk: the receptionist checks their ID, limits how many go up at once, and points each one to the right door.

## Keep in mind

- You define routes and point each one at Lambda, a server or an AWS service.
- Authentication, rate limiting, caching and logging happen before your code runs.
- Two flavors: HTTP APIs (simpler and cheaper) and REST APIs (more features).
- Billed per million requests; nothing to run or pay for while idle.

## Go deeper

- [What is Amazon API Gateway? (official docs)](https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html)
- [Amazon API Gateway product page](https://aws.amazon.com/api-gateway/)

## Related terms

- [API](https://tidbit.legios.com.ar/api/): The menu of things one program lets another program ask for.
- [Internet](https://tidbit.legios.com.ar/internet/): A worldwide network of networks that lets any connected device reach any other.
- [AWS Lambda](https://tidbit.legios.com.ar/aws-lambda/): Run a piece of code only when something happens, with no server to manage.
- [Server](https://tidbit.legios.com.ar/server/): A computer whose job is to answer requests from other computers, all day, every day.
- [HTTPS](https://tidbit.legios.com.ar/https/): The padlock in your browser: web traffic nobody in between can read.
- [API key](https://tidbit.legios.com.ar/api-key/): A secret string that identifies your app when it calls someone else's API.
- [Token](https://tidbit.legios.com.ar/token/): The small chunk of text a language model actually reads.
- [Amazon Cognito](https://tidbit.legios.com.ar/amazon-cognito/): Sign-up, login and passwords for your app, handled by AWS instead of you.
