Skip to content
GitHub Discord

AWS API Gateway V1 streaming

An example on how to enable streaming for API Gateway REST API routes.

sst.config.ts
api.route("GET /", {
handler: "index.handler",
streaming: true,
});

The handler uses the native awslambda.streamifyResponse and awslambda.HttpResponseStream.from to stream responses through API Gateway.

index.ts
export const handler = awslambda.streamifyResponse(
async (event, stream) => {
stream = awslambda.HttpResponseStream.from(stream, {
statusCode: 200,
headers: {
"Content-Type": "text/plain; charset=UTF-8",
"X-Content-Type-Options": "nosniff",
},
});
stream.write("Hello ");
await new Promise((resolve) => setTimeout(resolve, 3000));
stream.write("World");
stream.end();
},
);
sst.config.ts
const api = new sst.aws.ApiGatewayV1("MyApi");
api.route("GET /", {
handler: "index.handler",
streaming: true,
});
api.route("GET /hono", {
handler: "hono.handler",
streaming: true,
});
api.deploy();
hono.ts
import { Hono } from "hono";
import { streamText } from "hono/streaming";
import { streamHandle } from "hono/aws-lambda";
const app = new Hono().get("/hono", (c) => {
return streamText(c, async (stream) => {
await stream.writeln("Hello");
await stream.sleep(3000);
await stream.writeln("World");
});
});
export const handler = streamHandle(app);

View the full example.