# Next.js api route streaming data

**URL:** <https://discourse.sst.dev/t/next-js-api-route-streaming-data/2979>\
**Category:** General\
**Created:** [September 16, 2024, 7:45pm UTC](https://discourse.sst.dev/t/next-js-api-route-streaming-data/2979 "2024-09-16T19:45:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mm78](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@mm78](https://discourse.sst.dev/u/mm78)\
**Post date:** [September 16, 2024, 7:45pm UTC](https://discourse.sst.dev/t/next-js-api-route-streaming-data/2979/1 "2024-09-16T19:45:29Z")

</div>

Hi, is it possible to stream data from a Next.js api route when my SST stack is deployed to prod? This is my first time using SST, so it’s not clear to me what the limitations are.

I have created a simple Next.js site that contains an api route (app router). The api route accepts a payload via POST with some data. Then it calls the OpenAI api to retrieve a response. I want to stream that response back to my client component (similar to the ChatGPT interface). I have this working when running my SST stack in dev, but once I deploy it to prod, the response is no longer streamed, it just returns the full data once complete. Here is the code that does the stream/response:

```auto
  const headers = new Headers({
    "Content-Type": "text/plain; charset=utf-8",
    "Transfer-Encoding": "chunked",
  });

  const stream = new ReadableStream({
    async start(controller) {
      try {
        const openaiStream = await openai.chat.completions.create({
          model: model.id,
          messages,
          stream: true,
        });

        for await (const chunk of openaiStream) {
          const content = chunk.choices[0]?.delta?.content || "";
          controller.enqueue(new TextEncoder().encode(content));
        }

        controller.close();
      } catch (error) {
        console.error("Error calling OpenAI API:", error);
        controller.error(error);
      }
    },
  });

  return new NextResponse(stream, { headers });

```

---

<div class="post-metadata">

**Author:** ![Tho-Tu](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.sst.dev/tho-tu/32/1620_2.png) [@Tho-Tu](https://discourse.sst.dev/u/Tho-Tu)\
**Post date:** [July 30, 2025, 8:01am UTC](https://discourse.sst.dev/t/next-js-api-route-streaming-data/2979/2 "2025-07-30T08:01:49Z")

</div>

I added “X-Accel-Buffering”: “no” to the headers and it worked for me. I believe this disables cloudfront from buffering.
