Kernelius Academy

Create an API Route

Build the backend API endpoint for streaming chat responses

Create an API Route

Create a route handler, app/api/chat+api.ts and add the following code:

import { streamText, UIMessage, convertToModelMessages } from 'ai';
__PROVIDER_IMPORT__;

export async function POST(req: Request) {
  const { messages }: { messages: UIMessage[] } = await req.json();

  const result = streamText({
    model: __MODEL__,
    messages: await convertToModelMessages(messages),
  });

  return result.toUIMessageStreamResponse({
    headers: {
      'Content-Type': 'application/octet-stream',
      'Content-Encoding': 'none',
    },
  });
}

Let's take a look at what is happening in this code:

  1. Define an asynchronous POST request handler and extract messages from the body of the request. The messages variable contains a history of the conversation between you and the chatbot and provides the chatbot with the necessary context to make the next generation.
  2. Call streamText, which is imported from the ai package. This function accepts a configuration object that contains a model provider (imported from ai) and messages (defined in step 1). You can pass additional settings to further customise the model's behaviour.
  3. The streamText function returns a StreamTextResult. This result object contains the toUIMessageStreamResponse function which converts the result to a streamed response object.
  4. Finally, return the result to the client to stream the response.

This API route creates a POST request endpoint at /api/chat.

On this page