Build a chatbot with the AuraRoute API

A streaming chatbot in about thirty lines, using the OpenAI SDK you already know. No provider accounts, no model names.

AuraRoute speaks the OpenAI API, so a chatbot is the OpenAI SDK pointed at a different base URL. You never pick a model: you send model: "auto" and the router chooses the provider.

Set up the client

import OpenAI from "openai";

const client = new OpenAI({
  apiKey: process.env.AI_API_KEY,
  baseURL: "https://auraroute.dev/api",
});

Stream a reply

Streaming makes a chatbot feel fast, because the first words arrive before the answer is finished. Pass stream: true and read the chunks as they come.

const stream = await client.chat.completions.create({
  model: "auto",
  stream: true,
  messages: history,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}

Keep the conversation

The API is stateless. Keep the messages array yourself, append the user message before each call and the assistant reply after it, and send the whole array every time.

  1. Start history with a system message that describes your bot.
  2. Push { role: "user", content } when the user sends a message.
  3. Call the API, collect the streamed text, and push it as { role: "assistant", content }.

Handle failure

Provider outages are handled for you: a request that fails upstream moves to the next provider. What can still reach you is a rate limit on your own key, so check the response status and back off before retrying.