Build a chat app in 5 minutes

Get up and running with infer0 using the OpenAI SDK. The same flow works with the Anthropic SDK and Responses API too.

Prerequisites

  • Node.js 18+ installed
  • A terminal and your favorite editor

Step 1: Create your project

mkdir infer0-chat
cd infer0-chat
npm init -y
npm install openai express

Step 2: Sign in and register an OAuth app

Go to infer0.com and sign in with Google or GitHub. Then go to OAuth Apps and create a new app. Set the redirect URI to http://localhost:3000/callback. Copy your client_id and client_secret.

Step 3: Set environment variables

export CLIENT_ID="your-client-id"
export CLIENT_SECRET="your-client-secret"
export REDIRECT_URI="http://localhost:3000/callback"
export PORT=3000

Step 4: Create the server

Save this as server.mjs:

import express from "express";
import OpenAI from "openai";

const app = express();
const PORT = process.env.PORT;
const CLIENT_ID = process.env.CLIENT_ID;
const CLIENT_SECRET = process.env.CLIENT_SECRET;
const REDIRECT_URI = process.env.REDIRECT_URI;

app.use(express.json());

const tokens = {};

// Step 4a: Redirect user to infer0 to authorize
app.get("/login", (req, res) => {
  const url = "https://infer0.com/oauth/authorize?client_id=" + CLIENT_ID + "&redirect_uri=" + REDIRECT_URI + "&response_type=code";
  res.redirect(url);
});

// Step 4b: OAuth callback — exchange code for tokens
app.get("/callback", async (req, res) => {
  const { code } = req.query;

  const params = new URLSearchParams({
    grant_type: "authorization_code",
    code: code,
    client_id: CLIENT_ID,
    client_secret: CLIENT_SECRET,
    redirect_uri: REDIRECT_URI,
  });

  const tokenRes = await fetch("https://infer0.com/v1/oauth/token", {
    method: "POST",
    headers: { "Content-Type": "application/x-www-form-urlencoded" },
    body: params,
  });

  const data = await tokenRes.json();
  tokens.access = data.access_token;
  tokens.refresh = data.refresh_token;
  res.send("Authorized! You can close this tab and use the chat.");
});

// Step 4c: Non-streaming chat
app.get("/chat", async (req, res) => {
  if (!tokens.access) return res.redirect("/login");

  const client = new OpenAI({
    baseURL: "https://infer0.com/v1",
    apiKey: tokens.access,
  });

  const chat = await client.chat.completions.create({
    model: "ignored",
    messages: [{ role: "user", content: req.query.q || "Hello" }],
  });

  res.json(chat);
});

app.listen(PORT, () => console.log("Server running on http://localhost:" + PORT));

The model field is ignored. infer0 uses the model your user has configured.

Step 5: Run it

node server.mjs
  1. Open http://localhost:3000/login and authorize your app.
  2. Visit http://localhost:3000/chat?q=What+is+the+capital+of+France to see a non-streaming response.

Expected output

{
  "id": "chatcmpl-...",
  "object": "chat.completion",
  "created": 1717000000,
  "model": "gpt-4o-mini",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "The capital of France is Paris."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 14,
    "completion_tokens": 7,
    "total_tokens": 21
  }
}

Troubleshooting

Issue Fix
redirect_uri_mismatch Make sure the redirect URI in your OAuth App settings matches http://localhost:3000/callback exactly.
invalid_client Double-check your CLIENT_ID and CLIENT_SECRET environment variables.
invalid_grant The auth code expired (10 min). Go through /login again to get a fresh code.
No provider configured The user hasn't added an AI provider yet. Have them visit https://infer0.com/providers to add one.
Provider error with 401 The user's provider API key is expired or invalid. They need to update it on AI Providers.
Module not found: openai Run npm install openai and make sure package.json exists.