Skip to content

Api

Source: src/AWS/ApiGatewayV2/Api.ts

An Amazon API Gateway v2 API — the root of an HTTP API or WebSocket API.

HTTP APIs are the modern, cheaper, faster front door for Lambda functions (compared to REST v1). WebSocket APIs provide two-way real-time messaging backed by Lambda route handlers. Child resources (Integration, Route, Stage, Authorizer) reference the API by passing api in their props.

For the common “HTTP API in front of a Lambda function” case, prefer the high-level HttpApi helper which wires up the integration, route, stage, and invoke permission in one call.

Minimal HTTP API

import * as ApiGatewayV2 from "alchemy/AWS/ApiGatewayV2";
const api = yield* ApiGatewayV2.Api("Api", {});

HTTP API with CORS

const api = yield* ApiGatewayV2.Api("Api", {
corsConfiguration: {
AllowOrigins: ["https://example.com"],
AllowMethods: ["GET", "POST"],
AllowHeaders: ["content-type"],
MaxAge: 3600,
},
});
const api = yield* ApiGatewayV2.Api("WsApi", {
protocolType: "WEBSOCKET",
routeSelectionExpression: "$request.body.action",
});
const api = yield* ApiGatewayV2.Api("Api", {
disableExecuteApiEndpoint: true,
});